Fox Dividing Cheese [CF-371B]
来源:互联网 发布:ibeacon 三边定位算法 编辑:程序博客网 时间:2024/06/04 18:23
http://codeforces.com/contest/371/problem/B
暴力一点的写法:bfs搜索(可以过,但是有更优的做法),共六种拓展方向,优化一点是:吃大的。
代码:
#include<iostream>#include<cstdio>#include<cstring> #include<cmath>#include<queue>#define LL long longusing namespace std;int a0,b0,ans=100000000;struct H{ int a; int b; int step=0;};queue <H> q;int flag=0,f=0;void change1(H l){ H p; if(l.a%2==0){ p=l; p.a/=2;p.step++; if(p.a==p.b){flag=1;ans=min(ans,p.step);} q.push(p); } if(l.a%3==0){ p=l; p.a=p.a/3;p.step++; if(p.a==p.b){flag=1;ans=min(ans,p.step);} q.push(p); } if(l.a%5==0){ p=l; p.a=p.a/5;p.step++; if(p.a==p.b){flag=1;ans=min(ans,p.step);} q.push(p); }}void change2(H l){ H p; if(l.b%2==0){ p=l; p.b=p.b/2;p.step++; if(p.a==p.b){flag=1;ans=min(ans,p.step);} q.push(p); } if(l.b%3==0){ p=l; p.b=p.b/3;p.step++; if(p.a==p.b){flag=1;ans=min(ans,p.step);} q.push(p); } if(l.b%5==0){ p=l; p.b=p.b/5;p.step++; if(p.a==p.b){flag=1;ans=min(ans,p.step);} q.push(p); }}int main(){ scanf("%d%d",&a0,&b0); if(a0==b0){printf("0\n");return 0;} H l; l.a=a0;l.b=b0; q.push(l); while(!q.empty()) { f=0; l=q.front(); q.pop(); if(l.a==l.b) break; if(l.a>l.b) change1(l); else change2(l); if(flag) break; } if(flag)printf("%d\n",ans); else printf("-1\n"); return 0;}
第二种做法:根据题意可知,两块蛋糕最后一定变为a,b的最大公约数c,结果就是a/c,b/c对于2,3,5分解质因数能分解几次的次数之和,若不能分解为1,就是-1
#include<iostream>#include<cstdio>#include<cstring> #include<cmath>#include<queue>#define LL long longusing namespace std;int a,b,c;int gcd(int x,int y){ if(x<y) swap(x,y); if(x%y==0) return y; return gcd(y,x%y);}int tear(int x){ int y=0; while(x%2==0) y++,x/=2; while(x%3==0) y++,x/=3; while(x%5==0) y++,x/=5; if(x!=1)return -1; return y;}int main(){ scanf("%d%d",&a,&b); if(a==b){printf("0\n");return 0;} c=gcd(a,b); int a1=tear(a/c);int a2=tear(b/c); if(a1>=0&&a2>=0) printf("%d\n",a1+a2);//注意判断条件:应该是都不是-1 else printf("-1\n"); return 0;}
阅读全文
1 0
- CF 371B - Fox Dividing Cheese
- Fox Dividing Cheese [CF-371B]
- B. Fox Dividing Cheese
- B. Fox Dividing Cheese
- codeforces 371B - Fox Dividing Cheese
- B. Fox Dividing Cheese----GCD/因子问题
- [Codeforces]Fox Dividing Cheese
- Codeforces Round #218 (Div. 2) / 371B Fox Dividing Cheese (想法题)
- Codeforces #218 (Div. 2) B. Fox Dividing Cheese
- Codeforces #218 (Div. 2) Fox Dividing Cheese
- Codeforces Round #218 (Div. 2) B. Fox Dividing Cheese(思维 数学)
- CF 389B:Fox and Cross
- 思维题-CodeForces 371BFox Dividing Cheese
- CF - 389 - B. Fox and Cross(贪心)
- CF 510B(Fox And Two Dots-图上找环)
- CF 好题集锦 PART 0 & #388 B Fox and Minimal path 题解
- CF C. Fox And Names
- CF D. Fox And Jumping
- 精通CSS(parallax&Image replacement)
- p-16
- 自定义控件
- python map reduce filter
- g-07
- Fox Dividing Cheese [CF-371B]
- H5基础知识第十课时(JS函数定义和分类)
- 小熊吃蛋糕
- mac redis 3.2 安装
- Android Studio的logcat不输出信息
- H-08
- C 语言返璞归真: 指针篇(2)
- 如何在网站中加入qq咨询
- 进阶markdown语法