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

题目:http://codeforces.com/problemset/problem/490/D


D. Chocolate


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.

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



题意:给你两个长方形,你每次可以(横着或竖着)切除(任意一个)1/2或1/3,问最少且几次能使两个长方形面积相等。如果不可能相等,输出-1。如果可能,输出最小次数并输出此时两个长方形的长与宽。


思路:先分析给你的操作:1.切1/2就是把面积变成原来的1/2(相当于将质因子2转为1)

                                           2.切1/3就是把面积变为原来的2/3(相当于将质因子3转为2)

最终面积相等,所以两个最终面积的质因子完全相同。因为只能改变2,3,所以大于2,3的质因子是我们掌控不了的,所以s1(第一个长方形面积),s2(第二个长方形面积)除去他们的2,3质因子后应该是相等的,不相等就直接输出-1.

如果除去2,3质因子后他们的面积是相等的,那就说明一定有答案的,剩下的就是质因子3到2,2到1之间的转化,使最终的s1,s2含有的2,3个数相等。



代码:

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>

using namespace std;

int main(){
    long long a,b,c,d;
    long long s1,s2;
    cin>>a>>b>>c>>d;
    s1=a*b;
    s2=c*d;
    int prime1[5],prime2[5];
    memset(prime1,0,sizeof(prime1));
    memset(prime2,0,sizeof(prime2));
    while(s1%2==0){
        prime1[2]++;s1/=2;
    }
    while(s1%3==0){
        prime1[3]++;s1/=3;
    }
    while(s2%2==0){
        prime2[2]++;s2/=2;
    }
    while(s2%3==0){
        prime2[3]++;s2/=3;
    }
    if(s1!=s2)
        printf("NO\n");
    else{
        int n=0;
        if(prime1[3]>prime2[3]){
            while(prime1[3]>prime2[3]){
                if(a%3==0)
                    a=a/3*2;
                else
                    b=b/3*2;
                prime1[3]--,prime1[2]++,n++;
            }
        }
        else{
            while(prime2[3]>prime1[3]){
                if(c%3==0)
                    c=c/3*2;
                else
                    d=d/3*2;
                prime2[3]--,prime2[2]++,n++;
            }
        }
        if(prime1[2]>prime2[2]){
            while(prime1[2]>prime2[2]){
                if(a%2==0)
                    a/=2;
                else
                    b/=2;
                prime1[2]--;
            }
        }
        else{
            while(prime2[2]>prime1[2]){
                if(c%2==0)
                    c/=2;
                else
                    d/=2;
                prime2[2]--;
            }
        }

        printf("YES\n%I64d %I64d\n%I64d %I64d\n",a,b,c,d);

    }



    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值