HOJ cake

来源:互联网 发布:.net软件开发工程师 编辑:程序博客网 时间:2024/06/01 22:37

Cake

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3685 Accepted Submission(s): 1753
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
 
#include <iostream>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <map>#include <algorithm>#include <vector>#include <string>#include <cstring>#include <sstream>using namespace std;long long  p,q;long long gcd(long long a,long long b){    if(b==0) return a;    return gcd(b,a%b);}int main(){    while(cin>>p>>q)    {        long long GCD=gcd(p,q);        long long LCM=p/GCD*q;        long long ans=(p-1)+(q-1)-(GCD-1);        ans++;        cout<<ans<<endl;    }    return 0;}


Source
HZIEE 2007 Programming Contest
 

Recommend
lcy

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=4


分蛋糕问题

利用数轴上面的点来代替蛋糕思考划分。




0 0
原创粉丝点击