FoxDividingCheesecodeforces371b

很容易想到的一个思路是暴力,就是对多的蛋糕吃,进行bfs();
一旦两种蛋糕相等,就是最小步数的,很容易写。注释掉的代码就是
还有更好的方法,很容易看出,最后的解一定是它们的最大公约数,然后把他们的最大公约数去掉,看看能不能吃成1,记下步骤,也可以看能不能吃成最大公约数,是一样的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#define ll long long
using namespace std;
int a,b;
struct date{
int a;int b;int t;
};
/*int bfs(int a,int b){

queue <date> q;
date w;
w.a=a;
w.b=b;
w.t=0;
q.push(w);
date p;
while(!q.empty()){
p=q.front();
q.pop();
if(p.a==p.b)
{
printf("%d",p.t);
return 0;
}
else if(p.a>p.b)//找大的蛋糕吃
{//a
if(p.a%2==0) //吃1/2
{
p.a/=2;p.t++;
q.push(p);
p.t--;p.a*=2;
}
if(p.a%3==0) //吃2/3
{
p.a/=3;p.t++;
q.push(p);
p.t--;p.a*=3;
}
if(p.a%5==0) //吃4/5
{
p.a/=5;p.t++;
q.push(p);
p.t--;p.a*=5;
}
}
else {
if(p.b%2==0)
{
p.b/=2;p.t++;
q.push(p);
p.t--;p.b*=2;
}
if(p.b%3==0)
{
p.b/=3;p.t++;
q.push(p);
p.t--;p.b*=3;
}
if(p.b%5==0)
{
p.b/=5;p.t++;
q.push(p);
p.t--;p.b*=5;
}
}//与a同理

}
return 1;//没结果就是骗他们了
}*/
int ans=0;
int gcd(int x,int y)
{
if(x%y==0)
return y;
return gcd(y,x%y);
}
int made(int x,int y){
while(x%y==0){
x=x/y;
ans++;
}
return x;
}
int main(){
scanf("%d%d",&a,&b);
int c=gcd(a,b);//最大公约数
a/=c;
b/=c;//除掉
a=made(a,2);
a=made(a,3);
a=made(a,5);
b=made(b,2);
b=made(b,3);
b=made(b,5);//吃完
if(a==1&&b==1)
printf("%d",ans);//都能吃完,说明狐狸没骗他们
else printf("-1");//骗了
/*if(bfs(a,b))//若果骗他们了
printf("-1");*/

}

最近的文章

bzoj3713

斐波那契亚数列,直接暴力就行了,我是预处理出来,在输出,类似于桶排12345678910111213141516171819202122232425262728293031323334#include&lt;cstdio&gt;#include&lt;iostream&gt;#include&lt; …

于  暴力 继续阅读
更早的文章

汉堡

贪心的方法做,现找出用原始材料能做的汉堡,在对多出来的材料进行贪心,把少的用钱买上,如果没钱了,就输出,如果有钱就没有多出来的了,可以直接买了 123456789101112131415161718192021222324252627282930313233343536373839404142434 …

于  二分答案贪心 继续阅读