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. 你会如何给全局对象添加toString()方法

    首先,在讨论如何给所有方法window对象添加toString方法的时候,我们先来说说window的对象继承与对象实例,以及构造函数的this指针,还有变量的提升与方法的调用方式,最终一探window ...

  2. 一把剪刀看懂git reset 和它的三个参数

    都说git 命令难记且难懂,但是如果从立体的角度看待git与git管理的版本,那么一切都会明朗许多. 大多数的学习教程为了理解git,会绘制几个圆圈的串联,每个圆圈代表一个commit的版本,也就是从 ...

  3. UVA225 Golygons 黄金图形(dfs+回溯)

    剪枝1:在同一个维度上的点具有相同的奇偶性,如果奇数数量只有奇数个那么一定不能返回原点. 剪枝2:当前位置怎么也走不回去. 3:沿途判断障碍即可. 在oj上提交0.347s,最快的0.012s,应该有 ...

  4. C#textbox允许换行

    要让一个TextBox显示多行文本就得把它的Multiline属性设置为true就行

  5. vue 修改端口

  6. nginx访问日志(access_log)

    一.nginx访问日志介绍 nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由ngx_http_log_module模块负责,对应的官方地址 ...

  7. Python数据分析【炼数成金15周完整课程】

    点击了解更多Python课程>>> Python数据分析[炼数成金15周完整课程] 课程简介: Python是一种面向对象.直译式计算机程序设计语言.也是一种功能强大而完善的通用型语 ...

  8. TB平台搭建之一

    最近在搭建公司的testbench,主要有一下总结: 1.TB主要有两部分:部分一,软件部分主要用C写的,她的作用是写硬件的驱动(其实就是让核的外围设备可以正常工作或工作到特定的环境上)甚至有可能写整 ...

  9. python入门学习笔记1:Python与C的简单区别

    转载于:https://www.cnblogs.com/mlgjb/p/7892130.html 并做适当修改 一:简单比较   C语言 python 执行速度 快 慢 跨平台 不可以 可以 用途 操 ...

  10. cpu位图

    SMP处理器中要用到cpu位图,用来维护系统内CPU的状态信息,具有代表性的有: cpu_possible_map.cpu_online_map.cpu_present_map. static DEC ...