ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)
来源:互联网 发布:怪物猎人 知乎 编辑:程序博客网 时间:2024/05/28 20:19
1040: Alex and Asd fight for two pieces of cake
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 27 Solved: 12
[Submit][Status][Web Board]
Description
Alex and Asd have found two pieces of cake on table of weight a and b grams.They are so greedy that they all want the larger piece. A fight may happenes.
Now the smart person-Radical comes in and starts the dialog: "Stupid people, wait a little, I will make your pieces equal"
"Wow,you are so amazing, how are you going to do that?", Alex and Asd ask. Radical says"Ok,listen to me,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".
Alougth they are not som smart , they got it.So they agrees to his proposal, but on one condition: Radical should make the pieces equal as quickly as possible.
Find the minimum number of operations Radical needs to make pieces equal.
Input
The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109).
Output
If it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cake are initially equal, the required number is 0.
Sample Input
36 307 811 11
Sample Output
3-10
题意:两个人的一定要分到相等的蛋糕,否则输出-1,若初始值就相等, 输出0。跟狐狸给两只熊分饼一个道理,每次吃掉1/2或2/3或4/5。
那么此题就可以理解为每次将初始值乘以1/2或1/3或1/5,Alex和Asd乘以这几个数的次数可以不一样,每次乘的值也可以不一样,求最少的次数让这两个人相等。
首先感觉是贪心,但是后来感觉2、3、5都是质数,2^a和3^b和5^c次的公因数都是1,应该不是贪心。
比如例一、36与30,gcd为6,6/36=1/6,6/30=1/5。
再进一步,题意就成了用1/2,1/3,1/5来凑gcd(Alex,Asd)/Alex(或Asd,可行状态下这两个假分数肯定相等且最简式分子为1)且项数最少。
再进一步,就是求上述分母分解为2、3、5的个数(感觉由于三个数互质,只有唯一解,不存在最大最小的问题。)
代码:
#include<iostream>#include<algorithm>#include<cstdlib>#include<sstream>#include<cstring>#include<cstdio>#include<string>#include<deque>#include<cmath>#include<queue>#include<set>#include<map>using namespace std;typedef long long LL;LL list[3]={5,3,2};//为了循环方便用个数组LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}int main (void){LL a,b;while (cin>>a>>b){LL g=gcd(a,b);LL ca,cb,ag,bg,fenzia,fenzib,fenmua,fenmub;if(a==b){cout<<0<<endl;continue;}else{map<LL,LL>lista;//记录Alex分母的分解情况map<LL,LL>listb;//记录Asd分母的分解情况ag=gcd(a,g);bg=gcd(b,g);fenmua=a/ag;//得到Alex最简分式的分母fenmub=b/bg;//得到Asd最简分式的分母for (int i=0; i<3; i++)//Alex分解{while (fenmua>=list[i]){if(fenmua%list[i]==0){fenmua/=list[i];lista[list[i]]++;}elsebreak;}}for (int i=0; i<3; i++)//Asd分解{while (fenmub>=list[i]){if(fenmub%list[i]==0){fenmub/=list[i];listb[list[i]]++;}elsebreak;}}if(fenmua==1&&fenmub==1)cout<<lista[2]+lista[3]+lista[5]+listb[2]+listb[3]+listb[5]<<endl;//输出操作次数(Alex+Asd)elsecout<<-1<<endl;}}return 0;}
0 0
- ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)
- ACM程序设计选修课——1051: Glamor Sequence(YY+求和公式)
- ACM程序设计选修课——1043: Radical loves integer sequences(YY)
- ACM程序设计选修课——1036: Hungar的菜鸟赛季(YY)
- ACM程序设计选修课——1049: Efface Numbers(贪心)
- ACM程序设计选修课——1058: Lucky Sequence(思考)
- ACM程序设计选修课——1081: 堆(BFS)
- ACM程序设计选修课——1018: Common Subsequence
- ACM程序设计选修课——1030: Hungar的时尚球场(水题+耐心)
- ACM程序设计选修课——1044: (ds:队列)打印队列(queue模拟)
- ACM程序设计选修课——1065: Operations on Grids(暴力字符串)
- ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)
- ACM程序设计选修课——1076汇编语言(重定向+模拟)
- ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)
- ACM程序设计选修课——Problem F:(ds:图)旅游规划(优先队列+SPFA)
- ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)
- ACM程序设计选修课——1024: 末位零(求末尾0的方法+可有可无的快速幂)
- ACM程序设计选修课——1031: Hungar的得分问题(二)(杨辉三角+二进制转换)
- 断言(了解即可)
- 南阳题目100-1的个数
- java解析html之HTMLparser初次尝试
- 深入理解android事件分发
- JavaScript的三种弹出框
- ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)
- linux 中更改用户权限和用户组的命令chmod,chgrp实例
- JAVA组合字符串,长度不足用0补齐
- CentOS 6.5 安装 php7 教程
- Setting下增加触摸笔功能(TP)
- Android对图片的各种平移,缩放,拖动
- Android开发中在一个VideoView的情况下多个视频按顺序循环播放
- 关于Servlet找不到路径的问题(Servlet HTTP Status 404 )
- Android Activity之间跳转与传值