codeforce626C.Block Towers(二分)

C. Block Towers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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 2, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. 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.

第二个样例,应为6可以使2的倍数也可以是3的倍数,所以归为2和3都是可以,这里归为3,因为归为2的话,3就要选择9了

 

题意:n个人用高度为2的木板,m个人用高度为3的木板堆塔,并且每个人做成的塔的高度不能相同。就是求2的倍数,3的倍数,如果同时是2的倍数和3的倍数,只能属于一个数

 分析:二分枚举求解,如果x / 2 < n,x此时一定小于所求值,如果x / 3 < m,x也小于所求值,如果 x / 2 + x / 3 - x / 6 < n + m,也小于所求值,因为n + m是堆成的塔的总个数,他等于长度为2堆成的加上长度为三堆成的减去长度为6堆成(重复只取一次)。 

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 int n,m;
 7 bool judge(int x)
 8 {
 9     if(x / 2 < n || x / 3 < m || x / 2 + x / 3 - x / 6 < m + n)
10         return 0;
11     return 1;
12 }
13 
14 int main()
15 {
16     scanf("%d%d", &n, &m);
17     int l = 0,r = 10000000,mid;
18     while(r > l)
19     {
20         mid = (r + l) / 2;
21         if( judge(mid) )
22             r = mid;  //因为最后就是输出的r,则r一定是满足条件的
23         else
24             l = mid + 1;
25     }
26     printf("%d\n", r);
27     return 0;
28 }
View Code

 

 

posted @ 2016-02-16 22:40  zhaop  阅读(313)  评论(0编辑  收藏  举报