HDU 1722 Cake (GCD)

来源:互联网 发布:momentum in ear 知乎 编辑:程序博客网 时间:2024/05/23 20:07

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 0x7fffffffint 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;}





0 0
原创粉丝点击