zoukankan      html  css  js  c++  java
  • 杭电 1772 cake

    Cake

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4617    Accepted Submission(s): 2299


    Problem Description
    一次生日Party可能有p人或者q人参加,现准备有一个大蛋糕.问最少要将蛋糕切成多少块(每块大小不一定相等),才能使p人或者q人出席的任何一种情况,都能平均将蛋糕分食. 
     
    Input
    每行有两个数p和q.
     
    Output
    输出最少要将蛋糕切成多少块.
     
    Sample Input
    2 3
     
    Sample Output
    4
    Hint
    将蛋糕切成大小分别为1/3,1/3,1/6,1/6的四块即满足要求. 当2个人来时,每人可以吃1/3+1/6=1/2 , 1/2块。 当3个人来时,每人可以吃1/6+1/6=1/3 , 1/3, 1/3块。

     

    思路:想像一个蛋糕,然后先按p个人来分,然后再根据p来分的刀痕,再来分q的份,那就会有gcd(p,q)的刀痕是重叠的,也就是说,最后的刀痕数目就是p+q-gcd(p,q).
    附上代码:
    #include <iostream>
    #include<math.h>
    #include <iomanip>
    #include<cstdio>
    #include<string>
    #include<map>
    #include<vector>
    #include<list>
    #include<algorithm>
    #include<stdlib.h>
    #include<iterator>
    #include<sstream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    
    long long gcd(long long a,long long b)
    {
        long long  big=a;
        long long  smal=b;
        long long  ck;
        if(big<smal)
        {
            ck=a;
            a=b;
            b=a;
        }
        long long temp;
        while(smal>0)
        {
            temp=big%smal;
            big=smal;
            smal=temp;
        }
        return big;
    }
    
    long long gys(long long a,long long b)
    {
        long long  kk;
        long long ak=a;
        long long bk=b;
        kk=ak*bk/gcd(ak,bk);
        return kk;
    }
    
    
    
    int main()
    {
        int m,n;
        while(cin>>m>>n)
        {
            int temp=gcd(m,n);
            cout<<m+n-temp<<endl;
        }
        return 0;
    }
  • 相关阅读:
    bbb SOCKET CAN
    在BBB上使用CCS5开发环境
    BBB的PRU模块
    垃圾邮件分类
    yzoj 2377 颂芬梭哈 题解
    yzoj 2372 小B的数字 题解
    yzoj P2371 爬山 题解
    hdu 1007 Quoit Design 题解
    手写堆
    yzoj P1126 塔 题解
  • 原文地址:https://www.cnblogs.com/William-xh/p/7203398.html
Copyright © 2011-2022 走看看