码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1722 Cake (GCD)

时间:2014-11-27 22:07:39      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

Cake

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2609 Accepted Submission(s): 1253
 
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块。
 
Author
LL
 
Source
HZIEE 2007 Programming Contest




解题思路:先份成p块,然后再拼到一起,再从原来开始的地方,将蛋糕再分成q份,中间肯定会出现完全重合的块数为k,则此是需要分的块数就是 p + q - k.

现在只需要求出k即可,其实k是什么呢,就是GCD(p,q),就是p和q的最大公约数。




AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7fffffff

int gcd(int a, int b){
    return !b ? a : gcd(b, a%b);
}

int main()
{
    #ifdef sxk
        freopen("in.txt","r",stdin);
    #endif
    int p, q;
    while(scanf("%d%d",&p, &q)!=EOF)
    {
        printf("%d\n", p + q - gcd(p, q));
    }
    return 0;
}





HDU 1722 Cake (GCD)

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://blog.csdn.net/u013446688/article/details/41553893

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!