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. ubuntu开放端口

    1.安装iptables(一般情况,ubuntu安装好的时候,iptables会被安装上),使用以下命令: $apt-get update $apt-get install iptables 2.安装 ...

  2. python基础一 day14 生成器函数进阶(1)

  3. python判断平衡二叉树

    题目:输入一棵二叉树,判断该二叉树是否是平衡二叉树.若左右子树深度差不超过1则为一颗平衡二叉树. 思路: 使用获取二叉树深度的方法来获取左右子树的深度 左右深度相减,若大于1返回False 通过递归对 ...

  4. ADO 输入输出文本及获取指定字符串

    ---恢复内容开始--- 1.获取文本:声明别量,指定文本路径,获取文本内容. string Text=System.IO.File.ReadAllText(@"C:\xxx\xxx\xxx ...

  5. css实现页面文字不换行、自动换行、强制换行

    强制不换行 div{ white-space:nowrap; } 自动换行 div{ word-wrap: break-word; word-break: normal; } 强制英文单词断行 div ...

  6. NOIP模拟赛 无线通讯网

    [题目描述] 国防部计划用无线网络连接若干个边防哨所.2种不同的通讯技术用来搭建无线网络:每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都 ...

  7. pandas中层次化索引与切片

    Pandas层次化索引 1. 创建多层索引 隐式索引: 常见的方式是给dataframe构造函数的index参数传递两个或是多个数组 Series也可以创建多层索引 Series多层索引 B =Ser ...

  8. Linux - NodeJS安装

    1> 去NodeJS官网 https://nodejs.org/en/ 或 中文网 http://nodejs.cn/download/ 拷贝相应版本的安装文件,如下图: 2> 执行 wg ...

  9. Python爬虫系列-Requests库详解

    Requests基于urllib,比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求. 实例引入 import requests response = requests.get( ...

  10. Java-JFrame可视化开发

    Java-JFrame可视化开发的一般步骤 JFrame可以做出类似于QQ登录功能的窗体,通过JFrame可以利用Java代码实现窗体功能,一般用于CS项目的C(客户端)的开发: 利用JFrame可以 ...