zoukankan      html  css  js  c++  java
  • 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

    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;
    }
  • 相关阅读:
    233. Number of Digit One
    232. Implement Queue using Stacks
    231. Power of Two
    230. Kth Smallest Element in a BST
    229. Majority Element II
    228. Summary Ranges
    227. Basic Calculator II
    ArcGIS 网络分析[3] 发布NAServer到ArcGIS for Server(以Server 10.4为例)
    iView的使用【小白向】
    Vuejs环境安装与工程建立【小白Windows向】
  • 原文地址:https://www.cnblogs.com/Blackops/p/5356398.html
Copyright © 2011-2022 走看看