2 seconds
256 megabytes
standard input
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.
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.
Print a single integer, denoting the minimum possible height of the tallest tower.
1 3
9
3 2
8
5 0
10
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堆成(重复只取一次)。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n,m;
bool judge(int x)
{
if(x / < n || x / < m || x / + x / - x / < m + n)
return ;
return ;
} int main()
{
scanf("%d%d", &n, &m);
int l = ,r = ,mid;
while(r > l)
{
mid = (r + l) / ;
if( judge(mid) )
r = mid; //因为最后就是输出的r,则r一定是满足条件的
else
l = mid + ;
}
printf("%d\n", r);
return ;
}