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;
}

ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)的更多相关文章

  1. ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)

    问题 E: (ds:图)公路村村通 时间限制: 1 Sec  内存限制: 128 MB 提交: 9  解决: 5 题目描述 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本, ...

  2. ACM程序设计选修课——Problem F:(ds:图)旅游规划(优先队列+SPFA)

    问题 F: (ds:图)旅游规划 时间限制: 1 Sec  内存限制: 128 MB 提交: 14  解决: 4 题目描述 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路 ...

  3. ACM程序设计选修课——Problem E:(ds:图)公路村村通(优先队列或sort+克鲁斯卡尔+并查集优化)

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. ACM程序设计选修课——1018: Common Subsequence(DP)

    问题 L: Common Subsequence 时间限制: 1 Sec  内存限制: 32 MB 提交: 70  解决: 40 [提交][状态][讨论版] 题目描述 A subsequence of ...

  5. ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)

    Problem D: (ds:树)合并果子 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 80  Solved: 4 [Submit][Status][ ...

  6. ACM程序设计选修课——1076汇编语言(重定向+模拟)

    1076: 汇编语言 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 34  Solved: 4 [Submit][Status][Web Board] ...

  7. ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)

    1057: Beautiful Garden Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 25  Solved: 12 [Submit][Statu ...

  8. ACM程序设计选修课——1065: Operations on Grids(暴力字符串)

    1065: Operations on Grids Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 17  Solved: 4 [Submit][Sta ...

  9. ACM程序设计选修课——1041: XX's easy problem(神烦的多次字符串重定向处理)

    1041: XX's easy problem Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 41  Solved: 7 [Submit][Statu ...

随机推荐

  1. DIV在另一个DIV里面垂直居中,水平居中

    HTML: <div class="parent"> <div class="children"> <div class=&quo ...

  2. Ubuntu系统Apache 2部署SSL证书

    几天前用Apache 2部署了一个静态网页,但通过域名访问时Google提示“不安全”,经了解,原来是缺少证书. 什么是SSL证书? SSL 是指安全套接字层,简而言之,它是一项标准技术,可确保互联网 ...

  3. 查看numpy的类型

    查看一个变量的类型:type(img) 查看array中的数据值的类型:img.dtype 查看array的形状:img.shape

  4. SAP HANA

    DROP PROCEDURE ""."ZCONCAT_EKKO_EBN"; CREATE PROCEDURE ""."ZCONCA ...

  5. Unity基础-脚本的优化

    脚本的优化 object pool 避免频繁的内存分配和gc噩梦(字符串相加?) 是否有必要都写在update里?分帧? 需要的只取一次 使用editor内赋值,而不是find 复杂的物理 复杂的数学 ...

  6. Linux基础学习-使用vsftpd服务传输文件

    使用vsftpd服务传输文件 1 安装vsftpd [root@qdlinux ~]# yum install vsftpd Loaded plugins: product-id, search-di ...

  7. 【Spring】事务的实现方式

    1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:转账. 场景设定: 用户名 余额 A 1000 B 1000 操作: A通过支付宝给B转账200块,做这件事情会进行两个操作. 1:A账号- ...

  8. 【Hadoop/Hive/mapreduce】系列之使用union all 命令之后如何对hive表格使用python进行去重

    业务场景大概是这样的,这里由两个hive表格,tableA 和 tableB, 格式内容都是这样的: uid cate1 cate2 在hive QL中,我们知道union有着自动去重的功能,但是那是 ...

  9. Python学习笔记:PyInstaller(exe程序打包)

    PyInstaller可以将Python程序打包成一个exe程序来独立运行,用户使用时只需要执行这个exe文件即可,不需要在机器上再安装Python及其他包就可运行了.另外,PyInstaller相较 ...

  10. 循环字典进行操作时出现:RuntimeError: dictionary changed size during iteration的解决方案

    在做对员工信息增删改查这个作业时,有一个需求是通过用户输入的id删除用户信息.我把用户信息从文件提取出来储存在了字典里,其中key是用户id,value是用户的其他信息.在循环字典的时候,当用户id和 ...