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

一次生日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;
}
时间: 2024-10-13 12:19:52

HDU 1722 Cake (GCD)的相关文章

HDU 1722 Cake (数论 gcd)(Java版)

Big Number 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1722 --每天在线,欢迎留言谈论. 题目大意: 给你两个数 n1,n2 . 然后你有一块蛋糕,提前切好,使得不管来 n1 还是 n2 个人都能够当场平均分配. 求 "提前切好" 的最小蛋糕块数. 知识点: (请无视)公式:N = a + b + gcd(a, b) : 思路: (勿无视)先份成p块,然后再拼到一起,再从原来开始的地方,将蛋糕再分成q份,中间肯定会出现完

hdu 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

HDU 1695(GCD)

GCD Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u [Submit]   [Go Back]   [Status] Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common

[ACM] hdu 1260 Tickets (动态规划)

Tickets Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 4   Accepted Submission(s) : 2 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Jesus, what a great movie! Thou

[编写高质量iOS代码的52个有效方法](十)Grand Central Dispatch(GCD)

[编写高质量iOS代码的52个有效方法](十)Grand Central Dispatch(GCD) 参考书籍:<Effective Objective-C 2.0> [英] Matt Galloway 先睹为快 41.多用派发队列,少用同步锁 42.多用GCD,少用performSelector系列方法 43.掌握GCD及操作队列的使用时机 44.通过Dispatch Group机制,根据系统资源状况来执行任务 45.使用dispatch_once来执行只需要运行一次的线程安全代码 46.不

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spherical deep-sea submersible which was unpowered and lowered into the ocean on a cable, and was used to conduct a series of dives under the sea. The Bathys

hdu 1494 跑跑卡丁车(动态规划)

Problem Description 跑跑卡丁车是时下一款流行的网络休闲游戏,你可以在这虚拟的世界里体验驾驶的乐趣.这款游戏的特别之处是你可以通过漂移来获得一种加速卡,用这种加速卡可以在有限的时间里提高你的速度.为了使问题简单化,我们假设一个赛道分为L段,并且给你通过每段赛道的普通耗时Ai和用加速卡的耗时Bi.加速卡的获得机制是:普通行驶的情况下,每通过1段赛道,可以获得20%的能量(N2O).能量集满后获得一个加速卡(同时能量清0).加速卡最多可以储存2个,也就是说当你有2个加速卡而能量再次

HDU 5826 physics(物理)

HDU 5826 physics(物理) Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Description 题目描述 There are n balls on a smooth horizontal straight track. The track can be considered to be a number line. The balls can be con