B. Fox Dividing Cheese

B. Fox Dividing Cheese
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
15 20
output
3
input
14 8
output
-1
input
6 6
output
0

 
 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

struct part
{
	int qq;
int q1,q2,q3;
};


part get_part(int a)
{
	part temp;
int tmp=0;
while(a%2==0) 
{
	tmp++;
	a=a/2;
}
temp.q1=tmp;
tmp=0;
while(a%3==0) 
{
	tmp++;
	a=a/3;
}
temp.q2=tmp;

 tmp=0;

while(a%5==0) 
{
	tmp++;
	a=a/5;
} 
temp.q3=tmp;
temp.qq=a;

	return temp;
}

int main()
{
	int a,b;
	 
scanf("%d%d",&a,&b);
part aa=get_part(a);
part bb=get_part(b);

if (aa.qq!=bb.qq) 
{
	printf("-1\n");
 
}
else
{
	printf("%d\n",abs(aa.q1-bb.q1)+abs(aa.q2-bb.q2)+abs(aa.q3-bb.q3));
}

/*	printf("%d\n",abs(aa.q1-bb.q2) );
		printf("%d\n",abs(aa.q2-bb.q2));
				printf("%d\n",abs(aa.q3-bb.q3));
	 */


	return 0;
	
} 

理解了就很好做了。。。可惜之前没理解。。解析   据说还可以“

第2题BFS,搜索规模取决于a和b含有的2,3,5的个数,不超过32个。


”一时想不出

371B - Fox Dividing Cheese

It is easy to see that the fox can do three type of operations: divide by 2, divide by 3 and divide by 5. Let’s write both given numbers in form a = x·2a2·3a3·5a5b = y·2b2·3b3·5b5, where x and y are not dibisible by 2, 3 and 5. If x ≠ y the fox can’t make numbers equal and program should print -1. If x = y then soluion exists. The answer equals to |a2 - b2| + |a3 - b3| + |a5 - b5|, because |a2 - b2| is the minimal number of operations to have 2 in the same power in both numbers, |a3 - b3| is the minimal number of operations to have 3 in the same power in both numbers, and|a5 - b5| is the same for 5.

你可能感兴趣的:(B. Fox Dividing Cheese)