Codeforces 626C. Block Towers

C. Block Towers

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 4, 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.

思路:分别给你的是p个2和q个3,那么这个要求的最小的数T肯定>=max(2*p,3*q);那么从max(2*q,3*q)开始,暴力循环判断当前数是否符合要求。

当数为K的时候,K/2是2的倍数,K/3是3的倍数,K/6是6的倍数,其中,6的倍数是必须要符合的,因为6的倍数是由2,或3得到的,那么(K/2-K/6)是2的倍数且不能除3;

同理(K/3-K/6)是3的倍数但不能整除2;x=(p-(K/2-K/6))就是能去补给6的个数,结果<0的话就x=0;这表示2的个数连那些单独只能被2整除的数都补齐,所以给6的就为0,当

然可以存在2的个数连那些单独只能被2整除的数都补齐的情况,就像p=0的情况(可以这样是因为2,3是独立的),同理x1=(p-(K/3-K/6));如果x1+x==K/6,那么就是K;

 1 #include<set>
 2 #include<stack>
 3 #include<queue>
 4 #include<stdio.h>
 5 #include<iostream>
 6 #include<stdlib.h>
 7 #include<string.h>
 8 #include<math.h>
 9 #include<algorithm>
10 #include<map>
11 using namespace std;
12 int main(void)
13 {
14     int i,j,k,p,q;
15     while(scanf("%d %d",&p,&q)!=EOF)
16     {
17         int cc=2*p;
18         int dd=3*q;
19         int yy=max(cc,dd);
20         int flag=0;
21         for(i=yy;; i++)
22         {
23             if(i%2==0||i%3==0)
24             {
25                 int uu=i/2;
26                 int kk=i/3;
27                 int zz=i/6;
28                 int x1=(uu-zz);
29                 int x2=(kk-zz);
30                 x1=p-x1;
31                 x2=q-x2;
32                 if(x1<0)
33                 {
34                     x1=0;
35                 }
36                 if(x2<0)
37                 {
38                     x2=0;
39                 };
40                 if(x1+x2==zz)
41                 {
42                     break;
43                 }
44             }
45         }
46         printf("%d\n",i);
47 
48     }
49 }
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 int er(int n,int m,int a,int b);
 5 using namespace std;
 6 int main()
 7 {
 8     int a,b;
 9     scanf("%d%d",&a,&b);
10     int r=10000000;
11     int k=er(0,r,a,b);
12     printf("%d\n",k);
13     return 0;
14 }
15 
16 int er(int n,int m,int a,int b)
17 {
18     int mid=(n+m)/2;
19     if(n>m)
20     {
21         return n;
22     }
23     int cnt=max(a-mid/2+mid/6,0)+max(b-mid/3+mid/6,0);
24     if(mid/6>=cnt)
25     {
26         return er(n,mid-1,a,b);
27     }
28     else
29         return er(mid+1,m,a,b);
30 }
 1 while(l<=r)
 2     {
 3         int mid=(l+r)>>1;
 4         int ret=max(a-mid/2+mid/6,0)+max(b-mid/3+mid/6,0);
 5         if(mid/6==ret)
 6         {
 7           break;
 8         }else if(mid/6>ret)
 9         {
10             r=mid-1;
11         }
12         else l=mid+1;
13     } 

 

posted @ 2016-02-14 20:16  sCjTyC  阅读(740)  评论(0编辑  收藏  举报