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
4Hint将蛋糕切成大小分别为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
- HDU 1722 Cake (GCD)
- hdu 1722 Cake(gcd)
- cake (gcd)【HDU】-1722
- HDU 1722 Cake (GCD+数学)
- 杭电-1722 Cake (GCD)
- hdoj 1722 Cake 【GCD】
- Cake (gcd)
- 【GCD(最大公约数)】HDU1722-Cake
- 【杭电oj】1722 - Cake(数论,GCD)
- HDU 1722 Cake(思维题)
- HDU-1722 Cake
- hdu 1722 Cake
- hdu 1722 Cake
- HDU 1722 Cake
- hdu 1722 Cake
- HDU 1722 Cake
- hdu 1722 Cake 数学题
- hdu 1722 Cake
- Linux 下使用杀毒软件clamav扫描木马病毒
- 使用go build 进行条件编译
- 2015_3G新浪微博模拟登陆
- Eclipse web工程创建步骤及两种部署方法
- const常量引用的使用方法
- HDU 1722 Cake (GCD)
- 第3章 SQL基础
- poj.3641 Pseudoprime numbers【快速幂取模】 20141127
- activiti
- 关于Java中使用Comparator比较器的记录
- UDP
- 《Machine Learning(Tom M. Mitchell)》读书笔记——10、第九章
- ruby正则表达式的基本应用
- 【水题】 HDOJ 4666 Hyperspace