题意:给出a1*b1和a2*b2两块巧克力,每次可以将这四个数中的任意一个数乘以1/2或者2/3,前提是要能够被2或者3整除,要求最小的次数让a1*b1=a2*b2,并求出这四个数最后的大小。
做法:很显然只跟2跟3有关,所以s1=a1*b1,s2=a2*b2,s1/=gcd(s1,s2),s2/=gcd(s1,s2),然后若s1跟s2的质因子都是2跟3,那么就有解,之后暴力乱搞就好了。
#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]);
}