ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)
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
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)的更多相关文章
- ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)
问题 E: (ds:图)公路村村通 时间限制: 1 Sec 内存限制: 128 MB 提交: 9 解决: 5 题目描述 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本, ...
- ACM程序设计选修课——Problem F:(ds:图)旅游规划(优先队列+SPFA)
问题 F: (ds:图)旅游规划 时间限制: 1 Sec 内存限制: 128 MB 提交: 14 解决: 4 题目描述 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路 ...
- ACM程序设计选修课——Problem E:(ds:图)公路村村通(优先队列或sort+克鲁斯卡尔+并查集优化)
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- ACM程序设计选修课——1018: Common Subsequence(DP)
问题 L: Common Subsequence 时间限制: 1 Sec 内存限制: 32 MB 提交: 70 解决: 40 [提交][状态][讨论版] 题目描述 A subsequence of ...
- ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)
Problem D: (ds:树)合并果子 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 80 Solved: 4 [Submit][Status][ ...
- ACM程序设计选修课——1076汇编语言(重定向+模拟)
1076: 汇编语言 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 34 Solved: 4 [Submit][Status][Web Board] ...
- ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)
1057: Beautiful Garden Time Limit: 5 Sec Memory Limit: 128 MB Submit: 25 Solved: 12 [Submit][Statu ...
- ACM程序设计选修课——1065: Operations on Grids(暴力字符串)
1065: Operations on Grids Time Limit: 3 Sec Memory Limit: 128 MB Submit: 17 Solved: 4 [Submit][Sta ...
- 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 ...
随机推荐
- GWTDesigner_v5.1.0破解码
GWTDesigner_v5.1.0_win32_x86.exe破解码,双击运行keygeno.jar,然后输入用户名.网卡MAC,然后单击Generate,将生成的文件放在C:\Documents ...
- [视觉识别]OpenCV + CNN 大神符识别
数据集 Mnist数据集:http://yann.lecun.com/exdb/mnist/ 训练 import numpy as np from keras.datasets import mnis ...
- Ab initio methods|Evidence-based methods|maximum-likelihood|branch-site|H1|H0|GO|dS/dN ratio
(Gene prediction and comparison) 使用基于基因组序列的从头预测方法(Ab initio methods)(同时分别使用头预测软件( GENSCAN和 AUGUSTUS) ...
- java基础—object类
一.Object类介绍
- Maven搭建Struts2+Spring3+Hibernate4框架
做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...
- bug汇总
bug 2018年8月23日 bug 1:散点图画不出来. plt.scatter(validation_examples["longitude"], validation_exa ...
- 【思维题 费用流 技巧】bzoj5403: marshland
主要还是网络流拆点建图一类技巧吧 Description JudgeOnline/upload/201806/1(4).pdf 题目分析 第一眼看到这题时候只会把每个点拆成4个方向:再强制定向连边防止 ...
- 03大端和小端(Big endian and Little endian)
1.大端和小端的问题 对于整型.长整型等数据类型,Big endian 认为第一个字节是最高位字节(按照从低地址到高地址的顺序存放数据的高位字节到低位字节),而 Little endian 则相反 ...
- mysql Plugin ‘InnoDB’ init function returned error
问题描述: 非正常关闭mysql,同时更改了my.cnf 导致启动时不支持innodb,出现如下错误: [ERROR] Plugin ‘InnoDB’ init function returned ...
- ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study
262144K Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...