Block Towers (思维实现)

个人心得:这题没怎么看,题意难懂。后面比完再看的时候发现很好做但是怎么卡时间是个问题。

题意:就是有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 }

时间: 2024-08-13 22:14:42

Block Towers (思维实现)的相关文章

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

CF626C Block Towers

链接:https://www.luogu.org/problemnew/show/CF626C 题目描述 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. nn of the students use pieces made of two blocks and mm of

【CodeForces 626C】Block Towers

题意 给你n,m,如果 n个2的倍数和m个3的倍数,这n+m个数各不相同,那么求最大的数的最小值. 分析 方法1:枚举最大值为i,直到 i/2+i/3-i/6(不重复的2或3的倍数)≥n+m,并且要i/2(2的倍数)≥n,i/3(3的倍数)≥m. 方法2:枚举重复的数字i,i最小为6,每次增加6,设置两个结尾初始值为2*n,3*m,当两个结尾都比i大时,那就是还有重复.然后加在比较短的结尾.直到不再有重复. 代码 方法1 #include<cstdio> #include<algorit

Codeforces 626C Block Towers「贪心」「二分」「数学规律」

题意: 一堆人用方块盖塔,有n个人每次只能加两块方块,有m个人每次只能加三块方块.要求每个人盖的塔的高度都不一样,保证所用方块数最少,求最高的塔的高度. 0<=n+m  0<=n,m<=1e6 思路: 根据容斥原理,n和m个人如果都按照等差为2或者3的序列盖塔的话那么重复的个数应该是塔高较小的那组除以6,然后....一开始顺着这个思路想把自己坑了... 其实可能的塔高是有规律的 2 3 4 6 8 9 10 12...每六个中有三个,所以干脆先打表了,那么知道n和m之后,至少需要n+m种

CF 626C [Block Towers] 贪心

题目链接:http://codeforces.com/problemset/problem/626/C 题目大意: 有n个人用高度为2的砖往上搭,有m个人用高度为3的砖往上搭.每个人可用的砖块数是无限的,但是要求每个人搭的塔的高度,求所有可行情况中塔的最高高度的最小值. 关键思想:贪心,当且仅当高度为6的倍数时,他们的高度会相同.这时必须有一个人搭更高的,哪个人呢?此时就贪心:另外一种思想是数学的,首先答案x一定是大于等于2n也是大于等于3m的(有6的倍数取等号)对吧,而且m+n<=(x/2+x

CodeForces 327D Block Tower(DFS)

题意  给你一个城市的地图  你可以在地图上的 . 上建房子#上不能建房子  红房子可以装200个人  蓝房子可以装100个人  只有相邻位置有蓝房子时才能建红房子  你也可以拆掉已经建成的房子  拆掉后该点又变成  . 这题想到了就很容易了  因为没有限制要步数最少  可以先把左右的地方都建成蓝房子  然后就变成求连通块的题了  每个蓝房子连通块内依次拆掉建红房子  最终就只剩下一个蓝房子了 #include <bits/stdc++.h> using namespace std; cons

8VC Venture Cup 2016 - Elimination Round

在家补补题   模拟 A - Robot Sequence #include <bits/stdc++.h> char str[202]; void move(int &x, int &y, char ch) { if (ch == 'U') x--; if (ch == 'D') x++; if (ch == 'L') y--; if (ch == 'R') y++; } int main(void) { int n; scanf ("%d", &

CF div2 318 D

D. Bear and Blocks Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will rep

二分算法的一些思考

二分算法的思想: 通过不断减小问题规模,从边界条件出发求解问题.(通常是单调性问题,判定形式较为简单) 二分算法的优点: 1.把n的时间复杂度优化到logn 2.将一个问题转化为判定性质问题求解 代码: while(l<=r) { if(check(mid) { ans = mid; r = mid-1; } else ans = mid+1; } 例题: block towers n个人选择n个不同2的倍数的数,m个人选择m个3的倍数的数,选择的最大数字最小!