Codeforces Round #279 (Div. 2) 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 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.
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).
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.
2 6
2 3
1
1 6
2 3
36 5
10 16
3
16 5
5 16
3 5
2 1
-1
思路: 直接递归处理,加点剪支就好了
#include<iostream> #include<cstdio> #include<cstring> #include<map> #include<set> #define LL long long #define maxn 100010 #define INF 0x3f3f3f3f using namespace std ; map<LL,LL>mymap; map<LL,int>mm; map<LL,int>ss; LL x,y,ans1,ans2; const LL yy = 10000000000LL; void dfs(LL a,LL b,int cnt ) { // cout<<a<<" " << b << endl; LL num=a*b ; if(ss.find(a*yy+b)==ss.end()) { ss[a*yy+b]=cnt; } else if(ss[a*yy+b]>cnt) ss[a*yy+b]=cnt; else return ; if(mm.find(num)==mm.end()) { mm[num]=cnt; num=a*yy+b; mymap[a*b]=num; } else { if(mm[num]>cnt) { mm[num]=cnt; num=a*yy+b; mymap[a*b]=num; } } if(a%2==0)dfs(a/2,b,cnt+1) ; if(a%3==0)dfs(a/3*2,b,cnt+1) ; if(b%2==0)dfs(a,b/2,cnt+1) ; if(b%3==0)dfs(a,b/3*2,cnt+1) ; } void dfs2(LL a,LL b,int cnt ) { if(ss.find(a*yy+b)==ss.end()) { ss[a*yy+b]=cnt; } else if(ss[a*yy+b]>cnt) ss[a*yy+b]=cnt; else return ; LL num=a*b ; if(mm.find(num)==mm.end()) { } else { if(cnt+mm[num]<ans2) { ans2=cnt+mm[num] ; ans1=mymap[num]; x=a; y=b; } } if(a%2==0)dfs2(a/2,b,cnt+1) ; if(a%3==0)dfs2(a/3*2,b,cnt+1) ; if(b%2==0)dfs2(a,b/2,cnt+1) ; if(b%3==0)dfs2(a,b/3*2,cnt+1) ; } int main() { LL a1,a2,b1,b2; int n,m,i,j,ans; while(scanf("%I64d%I64d",&a1,&b1) != EOF) { mm.clear(); mymap.clear(); ss.clear(); scanf("%I64d%I64d",&a2,&b2); if(a1==a2&&b1==b2) { puts("0") ; cout<<a1<<" "<<b1<<endl; cout<<a2<<" "<<b2<<endl; continue; } ans2=INF; ans1=-1; dfs(a1,b1,0) ; ss.clear(); dfs2(a2,b2,0); if(ans2==INF) { puts("-1") ; continue ; } cout<<ans2<<endl; a1=ans1/yy; a2=ans1%yy; cout<<a1<<" "<<a2<<endl; cout<<x<<" "<<y<<endl; } return 0 ; }