【思维+暴力】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 000n + 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 36, and 9blocks. The tallest tower has a height of 9 blocks.

In the second case, the students can make towers of heights 24, 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.n个学生以两块砖头为一个单元叠砖,m个学生以3块砖头为一个单元叠砖)
2.n + m个学生使用的砖块总数没有相同的(高度各不相同)
3.找到某一种叠法,求所有n + m个学生叠的砖块最高高度的最小值。
//warning:以下分析与网上不一样,可能是错的,但是能AC
分析:
2 4 6 8 10
3 6 9 12 15
这样的顺序。
仔细观察:
满足两个条件:
2N<=MAX 
3M<=MAX(只有一个等于号成立)
可知
MAX/2 >= N   ①
MAX/3 >=M    ②
两个式子相加减去公倍数(重复):MAX/2+MAX/3-MAX/6 >= N+M③(答案应该具有的性质)
用1-3这样判断就行了。
既然是最大值的最小值。那么可以从小到大进行枚举。(第一个符合题目条件的结果绝对是最小值)
#include <iostream>
using namespace std;
int main()
{
  int a, b, i = 1;
  cin >> a >> b;
  while (i++)
  {
    if (i / 2 >= a)
      if (i / 3 >= b)
        if (i / 2 + i / 3 - i / 6 >= a + b)
        {
          cout << i << endl;
          break;
        }
  }
  return 0;
}

代码挺短的,,闲着没事做了个更短的版本。

#include <iostream>
using namespace std;
int main()
{
  int a, b, i = 1;cin >> a >> b;
  while (i++) if (i / 2 >= a && i / 3 >= b && i / 2 + i / 3 - i / 6 >= a + b) { cout << i << endl; break; }return 0;
}

 

 

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注

3 × 3 =