首页 > 代码库 > Codeforces 371BB. Fox Dividing Cheese

Codeforces 371BB. Fox Dividing Cheese

Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That‘s where the fox comes in and starts the dialog: "Little bears, wait a little, I want to make your pieces equal" "Come off it fox, how are you going to do that?", the curious bears asked. "It‘s easy", said the fox. "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I‘ll eat a little here and there and make the pieces equal".

The little bears realize that the fox‘s proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal.

Input

The first line contains two space-separated integers a and b (1?≤?a,?b?≤?109).

Output

If the fox is lying to the little bears and it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cheese are initially equal, the required number is 0.

Examples
input
15 20
output
3
input
14 8
output
-1
input
6 6
output
0

 其实这就是一道数学题,题意可以看成“给两个数a,b,让它们除以2,3,5,最后相等,最少除几次”。

  那么我们先求出他们的最大公约数c(因为要出的次数尽可能小),再看从原数到C分别用几步,或者能否除到C。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio> 
#include<queue>
#include<math.h>
using namespace std;
int a,b,ans;
int maxyin;
void chu(int a,int b)
{
    int aa=a%b;
    if(aa==0)    maxyin=b;
    else chu(b,aa);
}
int main()
{
    cin>>a>>b;    
    int c;
    if(b>a)
    {
        c=a;
        a=b;
        b=c;
    }
    
    if(a==b)//特判    
    {
        cout<<0;
        return 0; 
    }
    if(a%b==0)//特潘 
    {
        c=a/b;        
        while(c%5==0)
            c/=5,ans++;
        while(c%3==0)
            c/=3,ans++;
        while(c%2==0)
            c/=2,ans++;
        if(c==1)
            cout<<ans;
        else cout<<-1;
        return 0;
    }
    chu(a,b);
    c=a/maxyin;int d =b/maxyin;
    if(((c%5)&&(c%3)&&(c%2))||((d%5)&&(d%3)&&(d%2)))    
    {
        cout<<-1;
        return 0;
    }
    ans=0;
    while(c%5==0)
        c/=5,ans++;
    while(c%3==0)
        c/=3,ans++;
    while(c%2==0)
        c/=2,ans++;
    while(d%5==0)
        d/=5,ans++;
    while(d%3==0)
        d/=3,ans++;
    while(d%2==0)
        d/=2,ans++;
    cout<<ans;
    return 0;
}

 来个好看的代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio> 
#include<queue>
#include<math.h>
using namespace std;
int a,b;
int a2,a3,a5,b2,b3,b5;
int x;
void gcd(int a,int b)
{
    int aa=a%b;
    if(aa==0)    {x=b;return ;} 
    else gcd(b,aa); 
}
int main()
{
    cin>>a>>b;
    if(a==b)
    {
        cout<<0;
        return 0;
    }
    gcd(a,b);
    int m=a/x,n=b/x;
    while(m%3==0)    m/=3,a3++;
    while(m%2==0)    m/=2,a2++;
    while(m%5==0)    m/=5,a5++;
    while(n%3==0)    n/=3,b3++;
    while(n%2==0)    n/=2,b2++;
    while(n%5==0)    n/=5,b5++;
    if(m*n==1)
    {
        cout<<(a3+a2+a5+b2+b3+b5);
        return 0;
    }
    cout<<-1;
    return 0;
}

 

Codeforces 371BB. Fox Dividing Cheese