Block Towers

Description

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≤1000000, 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.

Sample Input

1 3

Sample Output

9

HINT

 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.

题意:m个同学用2米材料搭积木,n个同学用3米材料搭积木,但每个同学所搭积木高度不相等,求最高积木的最小高度。

思路:

分析案例:1 3,搭2米积木的同学高度为2,搭3米积木的同学高度为3,6,9

这个案例不够复杂看不出最小公倍数的意义,在此不做分析了;

我们来看这个案例:4 4 

搭2米积木的同学高度分别为2,4,6,8,搭3米积木的同学高度分别为3,6,9,12(无约束情况)

n=2*n=8;

m=3*m=12

(取两种同学的最高积木);

实际上:6重复,所以我们以最小公倍数6为区间查找,ans初始值为6,每一次当ans=6<min(n,m)

说明n.m中存在重复高度

判断n,m大小:

1.n>m则让m+=3;(去除重复高度,让最高积木的高度+3)

2.n<=m,则n+=2(去除重复高度,让最高积木的高度+3)

可以好好想想这样做的原因:既去重了,同时又让最高积木的高度尽可能最小(特别是n==m时是,n+=2,而不是让m+=3)

PS:每去重1次,ans+=6,最小公倍数为6(6为查找区间)

输出结果即为出循环后max(n,m),即两种同学所搭的那个最高积木(高度已经控制为最小)

/...菜鸡学习成果,如有错误还请大佬们指正.../

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#define mod 998244353;
#define Max 0x3f3f3f3f;
#define Min 0xc0c0c0c0;
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn=100005;
int main(){
    ios::sync_with_stdio(false);
    int n,m;
    while(cin>>n>>m){
        n*=2;               
        m*=3;
        int ans=6;
        while(ans<=min(n,m)){
            if(n>m){
                m+=3;   //2米积木高则加3米积木,尽可能的让高度最小
            }
            else{
                n+=2;   //相等也加2米积木,高度控制你懂的...
            }
            ans+=6;     //进入下一个最小公倍数查找区间
        }
        cout<<max(n,m)<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/82775483