Codeforces Round #279 (Div. 2) D. Chocolate

D. Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 is5 × 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.

Sample test(s)
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

题意:给你两个矩形的长和宽分别标记为a,b和c,d。面积记为s1,s2。每次能对两个矩形进行如下操作:切掉1/3或者切掉1、2,让你求使s1=s2的最小步数,如不能则输出-1.

做法:先算出s1,s2的最大公约数,最后相等面积必定是最大公约数的倍数。然后对s1/gcd(s1,s2)和s2/gcd(s1,s2)进行如下操作,首先判断其是否能被3整除,能就把其改成2,也就是除以3乘以2,然后ans++,s/3*2;然后判断能否被2整除,能就s/2,ans++。最后判断是否s1==s2==1,如果是则输出ans,不是则输出-1.

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL long long
#define inf 0x0f0f0f0f
using namespace std;
__int64 gcd(__int64 da,__int64 xiao)
{
    if(xiao==0)
        return da;
    else if(da%xiao==0)
        return xiao;
    else
        return gcd(xiao,da%xiao);

}
int main()
{
    __int64 a,b,c,d,tt;
    __int64 s1,s2,ans;
    while(scanf("%I64d%I64d",&a,&b)!=EOF)
    {
        ans=0;
        scanf("%I64d%I64d",&c,&d);
        s1=a*b;
        s2=c*d;
        tt=gcd(s1,s2);
        s1=s1/tt;
        s2=s2/tt;
        while(s1%3==0)
        {
            ans++;
            s1=s1/3*2;
            if(a%3==0)
                a=a/3*2;
            else
                b=b/3*2;
        }

        while(s2%3==0)
        {
            ans++;
            s2=s2/3*2;
            if(c%3==0)
                c=c/3*2;
            else
                d=d/3*2;
        }
        tt=gcd(s1,s2);
        s1=s1/tt;
        s2=s2/tt;
        while(s1%2==0)
        {
            ans++;
            s1=s1/2;
            if(a%2==0)
                a=a/2;
            else
                b=b/2;
        }
        while(s2%2==0)
        {
            ans++;
            s2=s2/2;
            if(c%2==0)
                c=c/2;
            else
                d=d/2;
        }
        if(s1==1&&s2==1)
        {
            printf("%I64d\n",ans);
            printf("%I64d %I64d\n%I64d %I64d\n",a,b,c,d);
        }
        else
            printf("-1\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值