Codeforces Round #279 (Div. 2) D 暴力

来源:互联网 发布:ubuntu 启动过程 黑屏 编辑:程序博客网 时间:2024/06/04 23:34



链接:戳这里


D. Chocolate
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is a1 × b1 segments large and the second one is a2 × b2 segments large.

Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus's mind and Paraskevi's beauty are equally matched, so the two bars must have the same number of squares.

To make the bars have the same number of squares, Polycarpus eats a little piece of chocolate each minute. Each minute he does the following:

he either breaks one bar exactly in half (vertically or horizontally) and eats exactly a half of the bar,
or he chips of exactly one third of a bar (vertically or horizontally) and eats exactly a third of the bar.
In the first case he is left with a half, of the bar and in the second case he is left with two thirds of the bar.

Both variants aren't always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16 × 23, then Polycarpus can chip off a half, but not a third. If the bar is 20 × 18, then Polycarpus can chip off both a half and a third. If the bar is 5 × 7, then Polycarpus cannot chip off a half nor a third.

What is the minimum number of minutes Polycarpus needs to make two bars consist of the same number of squares? Find not only the required minimum number of minutes, but also the possible sizes of the bars after the process.

Input
The first line of the input contains integers a1, b1 (1 ≤ a1, b1 ≤ 109) — the initial sizes of the first chocolate bar. The second line of the input contains integers a2, b2 (1 ≤ a2, b2 ≤ 109) — the initial sizes of the second bar.

You can use the data of type int64 (in Pascal), long long (in С++), long (in Java) to process large integers (exceeding 231 - 1).

Output
In the first line print m — the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in m minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line must correspond to the first bar and the third line must correspond to the second bar. If there are multiple solutions, print any of them.

If there is no solution, print a single line with integer -1.

Examples
input
2 6
2 3
output
1
1 6
2 3
input
36 5
10 16
output
3
16 5
5 16
input
3 5
2 1
output
-1


题意:

给出两块a1*b1、a2*b2大小的巧克力,每次可以切一块巧克力的2/3或者1/2

问最后能不能切成两块大小一样的巧克力。如果能,两块巧克力切完之后的大小。否则NO


思路:

假设两块巧克力是切到不能切为止,也就是不能分三份或者两份。

现在要想切成两块一样大小的巧克力。那么切到不能切为止的时候两块巧克力大小必定一样。

那么我们还需要计算切去的多余的部分。也就是共同切去的1/2或2/3

统计切了多少次1/2和2/3,然后取min补上去就可以了


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;map<int ,int > mp1;map<int ,int > mp2;ll a1,b1,a2,b2;int main(){    scanf("%I64d%I64d",&a1,&b1);    scanf("%I64d%I64d",&a2,&b2);    ll a=a1*b1,b=a2*b2;    while(a){        if(a%3==0) {            a=a/3*2;            mp1[3]++;        }        if(a%2==0){            a/=2;            mp1[2]++;        }        if(a%3!=0 && a%2!=0) break;    }    while(b){        if(b%3==0) {            b=b/3*2;            mp2[3]++;        }        if(b%2==0){            b/=2;            mp2[2]++;        }        if(b%3!=0 && b%2!=0) break;    }    ll t;    if(a==b) t=a;    else {        cout<<-1<<endl;        return 0;    }    for(int i=1;i<=min(mp1[2],mp2[2]);i++) t*=2;    for(int i=1;i<=min(mp1[3],mp2[3]);i++) t=t*3/2;    printf("%d\n",max(mp1[2],mp2[2])-min(mp1[2],mp2[2])+max(mp1[3],mp2[3])-min(mp1[3],mp2[3]));    int A1=0,B1=0,A2=0,B2=0;    if(mp1[2]>mp2[2]) A1=mp1[2]-mp2[2];    else A2=mp2[2]-mp1[2];    if(mp1[3]>mp2[3]) B1=mp1[3]-mp2[3];    else B2=mp2[3]-mp1[3];    //cout<<A1<<" "<<B1<<" "<<A2<<" "<<B2<<endl;    //cout<<t<<endl;    while(a1*b1>t || A1 || B1){        if(A1){            if(a1%2==0) a1/=2,A1--;            else if(b1%2==0) b1/=2,A1--;        }        if(B1){            if(a1%3==0) a1=a1/3*2,B1--;            else if(b1%3==0) b1=b1/3*2,B1--;        }    }    while(a2*b2>t || A2 || B2){        if(A2){            if(a2%2==0) a2/=2,A2--;            else if(b2%2==0) b2/=2,A2--;        }        if(B2){            if(a2%3==0) a2=a2/3*2,B2--;            else if(b2%3==0) b2=b2/3*2,B2--;        }    }    cout<<a1<<" "<<b1<<endl;    cout<<a2<<" "<<b2<<endl;    return 0;}


0 0
原创粉丝点击