Codeforces 490 D Chocolate

Source: Internet
Author: User

Test instructions: Give A1*B1 and a2*b2 two pieces of chocolate, each time you can multiply any one of these four numbers by 1/2 or 2/3, if you want to be divisible by 2 or 3, ask for the minimum number of times to let the a1*b1=a2*b2, and find the last size of the four numbers.


Practice: Obviously only with 2 and 3 related, so S1=A1*B1,S2=A2*B2,S1/=GCD (S1,S2), S2/=GCD (S1,S2), and if S1 and S2 of the quality Factor are 2 and 3, then there is a solution, then the violent mess is good.


#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;typedef long Long ll;ll gcd (ll A,ll b) {return b==0?a:gcd (b,a%b);} BOOL Work (ll X,int *cnt) {while (x%2==0) {x/=2;cnt[2]++;} while (x%3==0) {x/=3;cnt[3]++;} return x==1;} void CG (int &a,int &b,int val,int num) {while (a%val==0&&num>0) {num--;a/=val;if (val==3) a*=2;} while (b%val==0&&num>0) {num--;b/=val;if (val==3) b*=2;}} int Num[2][4];int a[2],b[2];ll S[2];int Main () {for (int i=0;i<2;i++) {cin>>a[i]>>b[i];s[i]=ll (a[i]) *b[ I];} ll T=GCD (s[0],s[1]); S[0]/=t;s[1]/=t;if (!work (s[0],num[0)) | |! Work (S[1],num[1])) {puts ("-1"); return 0;} int ans=0;for (int i=3;i>1;i--) {int sub=abs (num[0][i]-num[1][i]); ans+=sub;if (Num[0][i]>num[1][i]) {NUM[0][I-1]+=SUB;CG (a[0],b[0],i,sub);} ELSE{NUM[1][I-1]+=SUB;CG (a[1],b[1],i,sub);}} printf ("%d\n%d%d\n%d%d", ans,a[0],b[0],a[1],b[1]);}


D. Chocolatetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Polycarpus likes giving presents to Paraskevi. He has bought and chocolate bars, each of the them have the shape of a segmented rectangle. The first bar is a1? x? b 1 Segments large and the second one is a2? x? b 2 Segments large.

Polycarpus wants to give Paraskevi one of the bars in the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus's mind and Paraskevi ' s beauty is equally matched, so the the bars must has the Same number of squares.

To Make the bars has 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 was left with A half , of the bar a nd in the second case he's left With two thirds & Nbsp;of the bar.

Bot H variants aren ' t always possible, and sometimes polycarpus cannot chip off a half nor a third. For example, if the bar Is 16?x?23 , then Polycarpus can chip off a half, but not a third. If the bar Is 20?x?18 , then Polycarpus can chip off both a half and a third. If the bar Is 5?x?7 , then Polycarpus cannot chip off a half nor a third.

What is the minimum number of minutes Polycarpus needs to make and the bars consist of the. 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,? b 1 (1?≤? a 1,? b 1. ≤?109 )-the initial sizes of the first chocolate bar. The second line of the input contains integers a2,? b 2 (1?≤? a 2,? b 2. ≤?109 )-the Initial sizes of the second bar.

You can use the data of type Int64 (in Pascal), a long long (inс++), long (in Java) to process L Arge integers (exceeding 2?-? 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 is leveled In  m  minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) on any order. The second line must correspond to the first bar and the third line must correspond to the second bar. If There is multiple solutions, print any of them.

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

Sample Test (s) input
2 62 3
Output
11 62 3
Input
36 510 16
Output
316 55 16
Input
3 52 1
Output
-1


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 490 D Chocolate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.