HDU 1722 Cake (GCD)
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 a birthday Party may contain p or q people. A big cake is prepared. ask how many pieces of cake should be cut into at least (each piece is not necessarily the same size) so that the cake can be evenly divided in any situation where p or q people are present. |
Each Input row has two numbers p and q. |
Output The minimum number of pieces of cake to be cut. |
Sample Input2 3 |
Sample Output4HintCut the cake into 1/3, 1/3, 1/6, and 1/6 pieces to meet the requirements. When two people come, each person can eat 1/3 + 1/6 = 1/2, 1/2 pieces. When three people come, each person can eat 1/6 + 1/6 = 1/3, 1/3, 1/3. |
AuthorLL |
SourceHZIEE 2007 Programming Contest |
Solution: first, make a p block, and then combine it. Then, from the original place, divide the cake into q parts, and the number of completely overlapped parts will surely appear in the middle, the number of blocks to be split is p + q-k.
Now, you only need to find k. Actually, what is k, that is, GCD (p, q), which is the maximum approximate number of p and q.
AC code:
#include
#include
#include
#include #include
#include
#include
#include
#include
#include
#include
#include
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;}