狐狸坑蛋糕

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.

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

自己翻译

如果想不出正解的话可以直接搜索,BFS也能过,但写起来太麻烦

数论做法:

如果能达到要求的话,最后一定要吃成a,b的最大公约数

那就一口一口的吃,先吃1/2,再吃2/3,再吃4/5,如果最后吃不成一样的,就无法完成,如果可以,就输出所有动作的和,即

cout<<p2[0]+p2[1]+p3[0]+p3[1]+p5[0]+p5[1];

完整代码(bfs版)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct zhw{
	int a;
	int b;
	int tot;
};
int cnt=123456789,flag;
void bfs(int a,int b)
{
	queue <zhw> q;
	zhw que;
	que.a=a;
	que.b=b;
	que.tot=0;
	q.push(que);
	while(!q.empty())
	{
		que=q.front();
		q.pop();
		if(que.a==que.b)
		{
			cnt=que.tot;
			flag=1;
		}
		if(que.a>que.b)
		{
			if(que.a%2==0)
		    {
				zhw Tot;
				T.a=que.a/2;
				T.b=que.b;
				T.tot=que.tot+1;
				q.push(T);
		    }
			if(que.a%3==0)
			{
			    zhw Tot;
				T.a=que.a/3;
				T.b=que.b;
				T.tot=que.tot+1;
				q.push(T);
			
			}
			if(que.a%5==0)
			{
			    zhw T;
				T.a=que.a/5;
				T.b=que.b;
				T.tot=que.tot+1;
				q.push(T);
			
			} 
		}
	    if(que.a<que.b)
	    {
	    	if(que.b%2==0)
			{
				zhw T;
				T.a=que.a;
				T.b=que.b/2;
				T.tot=que.tot+1;
				q.push(T);
			}
			if(que.b%3==0)
			{
				zhw T;
				T.a=que.a;
				T.b=que.b/3;
				T.tot=que.tot+1;
				q.push(T);
			}
		    if(que.b%5==0)
			{
			    zhw T;
				T.a=que.a;
				T.b=que.b/5;
				T.tot=que.tot+1;
				q.push(T);
			}
	    } 
	
	}
}
int main()
{
	int x,y;
	cin>>x>>y;
	bfs(x,y);
	if(flag==0)
		printf("-1");
	else
		printf("%d",cnt);
	return 0;
}

  数论版

#include <iostream>
#include <cmath>
using namespace std;
int pw(int &a,int b)
{
    int t=0;
    while(a%b==0)
    {
        a=a/b;
        t++;
    }
    return t;
}
int gcd(int m,int n)
{
    if(n==0)
        return m;
    else
        return gcd(n,m%n);
}
int main()
{
    int p2[2],p3[2],p5[2];
    int a,b;
    int z;
    cin>>a>>b;
    z=gcd(a,b);
    a=a/z,b=b/z;
    p2[0]=pw(a,2);
    p2[1]=pw(b,2);
    p3[0]=pw(a,3);
    p3[1]=pw(b,3);
    p5[0]=pw(a,5);
    p5[1]=pw(b,5);
    if(a!=b)
    {
        cout<<"-1";
        return 0;
    }
    else
        cout<<p2[0]+p2[1]+p3[0]+p3[1]+p5[0]+p5[1];
    return 0;
} 
原文地址:https://www.cnblogs.com/oiersyp/p/7043548.html