ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)
2016-03-23 17:38
417 查看
1040: Alex and Asd fight for two pieces of cake
Time Limit: 1 Sec Memory Limit:128 MB
Submit: 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 30 7 8 11 11
Sample Output
3 -1 0
题意:两个人的一定要分到相等的蛋糕,否则输出-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]]++; } else break; } } for (int i=0; i<3; i++)//Asd分解 { while (fenmub>=list[i]) { if(fenmub%list[i]==0) { fenmub/=list[i]; listb[list[i]]++; } else break; } } if(fenmua==1&&fenmub==1) cout<<lista[2]+lista[3]+lista[5]+listb[2]+listb[3]+listb[5]<<endl;//输出操作次数(Alex+Asd) else cout<<-1<<endl; } } return 0; }
相关文章推荐
- 链表的基本操作
- Setting下增加触摸笔功能(TP)
- linux 中更改用户权限和用户组的命令chmod,chgrp实例
- 来段代码提提神-Java实现EXCEL模板化导出:ajax+springMVC或者Struts为例
- Java在企业应用的日臻完善
- Arrylist三种遍历iterator,for,增强for循环效率测试
- iOS 归档宏
- 算法 - 递归 - 算法综合
- Android应用安全开发之浅谈加密算法的坑
- java解析html之HTMLparser初次尝试
- myFile
- js 模块化开发
- 仿小米增加短信的收藏功能
- 断言(了解即可)
- 英文版的VC6.0,在 “Find in Files”中查找了中文引起崩溃
- Linux资源控制(LXC控制CPU,内存)
- 对于java的一些想法!
- java IO复习(三)
- Google官方BottomNavigationBar底部导航的使用
- Server 2012 网卡teaming 和 cisco 交换机配置, network card teaming