1. 程式人生 > >Block Towers

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;
}