您的位置:首页 > 其它

Codeforces 490D Chocolate【思维+暴搜】

2017-06-12 19:54 661 查看
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 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


题目大意:

现在有两块巧克力,我们现在想要将两块巧克力变成面积一样大的。

每次操作可以有两种:

将一块巧克力的某一边除以2(必须是2的倍数);

将一块巧克力的某一边除以3再乘以2(必须是3的倍数);

问最少将两块巧克力操作多少次,能够使得两个面积一样大。

思路:

1、首先我们知道,对于第一块巧克力来讲,其两个边最多有4种操作。看似暴搜会超时。2176782336

我们不妨拿出一个数2176782336作为一条边:6^12=2^12*3^12来说话,其可以同时进行两种操作的情况有12次,也就是2^12..那么对于两条边来说,也就是2^24.

约等于操作数为1e7.

那么我们暴搜存第一块巧克力的递减操作方案是可行的。

2、然后我们再暴搜一下第二个巧克力,遇到面积相同的情况的时候,我们维护一个最小步数花费即可。

剩余的部分就是考验代码能力的部分了= =

Ac代码:

#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
#define ll __int64
struct node
{
int x,y,step;
};
map<pair<int ,int >,int>vis;
map<ll,int>s;
map<ll ,pair<int ,int > >ans;
int aa,bb,xx,yy;
int ans1,ans2,ans3,ans4;
int output;
void Dfs(int a,int b,int sum)
{
if(vis[make_pair(a,b)]==1)return ;
vis[make_pair(a,b)]=1;
ll area=(ll)a*(ll)b;
if(s[area]==0)
{
s[area]=sum;
ans[area]=make_pair(a,b);
}
else
{
if(sum<s[area])
{
s[area]=sum;
ans[area]=make_pair(a,b);
}
}
if(a%2==0)Dfs(a/2,b,sum+1);
if(a%3==0)Dfs(a/3*2,b,sum+1);
if(b%2==0)Dfs(a,b/2,sum+1);
if(b%3==0)Dfs(a,b/3*2,sum+1);
}
void Dfs2(int a,int b,int sum)
{
if(vis[make_pair(a,b)]==1)return ;
vis[make_pair(a,b)]=1;
ll area=(ll)a*(ll)b;
ll tmppp=(ll)aa*(ll)bb;
if(tmppp==area)
{
if(sum<output)
{
output=sum;
pair<int ,int >tmp=ans[area];
ans1=tmp.first;
ans2=tmp.second;
ans3=a;
ans4=b;
}
}
if(s[area]>0)
{
if(s[area]+sum<output)
{
output=sum+s[area];
pair<int ,int >tmp=ans[area];
ans1=tmp.first;
ans2=tmp.second;
ans3=a;
ans4=b;
}
}
if(a%2==0)Dfs2(a/2,b,sum+1);
if(a%3==0)Dfs2(a/3*2,b,sum+1);
if(b%2==0)Dfs2(a,b/2,sum+1);
if(b%3==0)Dfs2(a,b/3*2,sum+1);
}
int main()
{
while(~scanf("%d%d",&aa,&bb))
{
vis.clear();
s.clear();
ans.clear();
Dfs(aa,bb,0);
vis.clear();
output=0x3f3f3f3f;
scanf("%d%d",&xx,&yy);
if((ll)aa*(ll)bb==(ll)xx*(ll)yy)
{
printf("0\n");
printf("%d %d\n%d %d\n",aa,bb,xx,yy);
continue;
}
Dfs2(xx,yy,0);
if(output==0x3f3f3f3f)
{
printf("-1\n");
continue;
}
printf("%d\n",output);
printf("%d %d\n%d %d\n",ans1,ans2,ans3,ans4);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 490D