8VC Venture Cup 2016 - Elimination Round C. 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 andm 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 height4, and the students using three-block pieces can make towers of height3, 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 heights3 and 6 with three-block pieces, for a maximum height of8 blocks.


题意:有n个人在用2的倍数,有m个人在用3的倍数,所有人的数都要求不一样,你要使得最大数最小,然后问你这个答案是多少


思路:二分讨论一下2的倍数,3的倍数,6的倍数就好了,注意边界


ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#define MAXN 1010000
#define LL long long
#define ll __int64
#include<iostream>
#include<algorithm>
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int n,m;
int check(int x)
{
	if(x/2<n||x/3<m||x/2+x/3-x/6<n+m)
	return 0;
	return 1;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int l=0,r=10000000;
		while(r>l+1)
		{
			int mid=(l+r)/2;
			if(check(mid))
			r=mid;
			else
			l=mid;
		}
		printf("%d\n",r);
	}
	return 0;
}


你可能感兴趣的:(venture,cup,2016,8VC)