标签: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 |
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; }
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/u013446688/article/details/41553893