Codeforces #218 (Div. 2) Fox Dividing Cheese

Description

Two little greedy bears have found two pieces of cheese in the forest of weighta 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 andb (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.

Sample Input

Input

15 20

Output

3

Input

14 8

Output

-1

Input

6 6

Output

0

题意:

输入a,b每次可以把a或b变为原来的二分之一、三分之一或者五分之一,求使二者相等时需要操作的次数,

分析:

a=15,b=20;

则可以写作:a=3*5;b=2*2*5;二者有最大公约数5,除去5还有三个数,因此需要做三次变动。
a=36,b=30;

a=2*2*3*3;b=2*3*5;二者有最大公约数2*3,除去2*3还有三个数,因此需要做三次变动。

a=x*pow(2,n1)*pow(3,m1)*pow(5,o1);
b=y*pow(2,n2)*pow(3,m2)*pow(5,o2);
若x!=y则必有a,b不能化为相等
代码如下:

#include <stdio.h>  
#include <stdlib.h>  
int main()
{  
    int a, b;  
    while(~scanf("%d%d",&a,&b)){  
        int count=0;  
        if (a == b){   //如果两块重量相等,则不用操作,直接输出0
         printf("0\n");  
         continue;  
        }  
        int c[4]={0},d[4]={0};  
        while (!(a % 2)) a /= 2, c[0]++;  //a能被2整除,记录有多少个因子为2
        while (!(a % 3)) a /= 3, c[1]++;  //a能被3整除
        while (!(a % 5)) a /= 5, c[2]++;  //a能被5整除
        while (!(b % 2)) b /= 2, d[0]++;  
        while (!(b % 3)) b /= 3, d[1]++;  
        while (!(b % 5)) b /= 5, d[2]++;  
        if (a != b){  //如果经过以上操作不能达到a=b,则说明这两块不可能变道相同,输出-1 
         printf("-1\n");  
         continue;  
        }  
        for (int i=0; i<3; i++)  
         count += abs(c[i] - d[i]);  //剩余的因子个数即为变动的次数
        printf("%d\n",count);  
    }  
} 

点赞