1. 程式人生 > >Block Towers (思維實現)

Block Towers (思維實現)

while open ins ssa ase iomanip making algo ons

個人心得:這題沒怎麽看,題意難懂。後面比完再看的時候發現很好做但是怎麽卡時間是個問題。

題意:就是有m個可以用2層積木的,n個可以用三層積木的,但是他們不允許重復所以可以無限添加。

比如 3 2

一開始是2層的開始2,然後 3,然後 4,此時再添加都一樣了,為了保證最小高度所以3+3=6,此時的2層的就要添加2個才不重樣

網上大神多,這個代碼我服,按照函數關系倆著重復的地點都有規律,所以只要找到此時最大m*2,n*3然後碰到一次相同就讓最小的最大值增加就可以了

題目:

Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n

of the students use pieces made of two blocks and m of the students use pieces made of three blocks.

The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students‘ towers.

Input

The first line of the input contains two space-separated integers n and m (0?≤?n,?m?≤?1?000?000, n?+?m?>?0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively.

Output

Print a single integer, denoting the minimum possible height of the tallest tower.

Example

Input
1 3
Output
9
Input
3 2
Output
8
Input
5 0
Output
10

Note

In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9blocks. The tallest tower has a height of 9 blocks.

In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.

技術分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iomanip>
 6 #include<algorithm>
 7 using namespace std;
 8 const int maxn=1005;
 9 int main()
10 {
11      int a,b;
12      int m,n;
13      while(cin>>m>>n){
14         int a=m*2,b=n*3;
15         for(int i=6;i<=min(a,b);i+=6){
16               if(a<=b) a+=2;
17               else b+=3;
18         }
19         cout<<max(a,b)<<endl;
20      }
21     return 0;
22 }
View Code

Block Towers (思維實現)