HDU 5640 King's Cake
2016-03-27 21:42
323 查看
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 the rectangle cake into two pieces, one of which should be a square cake.. Since he loves squares , he will cut the biggest square cake. He will continue
to do that until all the pieces are square. Now can you tell him how many pieces he can get when he finishes.
Input
The first line contains a number T(T≤1000),
the number of the testcases.
For each testcase, the first line and the only line contains two positive numbers n,m(1≤n,m≤10000).
Output
For each testcase, print a single number as the answer.
Sample Input
2
2 3
2 5
Sample Output
3
4
hint:
For the first testcase you can divide the into one cake of $2\times2$ , 2 cakes of $1\times 1$
其实就是过程等同于求最大公约数。
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 the rectangle cake into two pieces, one of which should be a square cake.. Since he loves squares , he will cut the biggest square cake. He will continue
to do that until all the pieces are square. Now can you tell him how many pieces he can get when he finishes.
Input
The first line contains a number T(T≤1000),
the number of the testcases.
For each testcase, the first line and the only line contains two positive numbers n,m(1≤n,m≤10000).
Output
For each testcase, print a single number as the answer.
Sample Input
2
2 3
2 5
Sample Output
3
4
hint:
For the first testcase you can divide the into one cake of $2\times2$ , 2 cakes of $1\times 1$
其实就是过程等同于求最大公约数。
#include<cstdio> #include<vector> #include<queue> #include<string> #include<map> #include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int low(int x){ return x&-x; } const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int maxn = 1e3 + 10; int T, n, m, sum; void gcd(int x, int y) { sum += x / y; if (x%y) gcd(y, x%y); } int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); sum = 0; gcd(n, m); printf("%d\n", sum); } return 0; }
相关文章推荐
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- 2015-11-11 hdu新生赛 A题(AC)
- 2015-11-11 hdu新生赛 C题(结束后一发AC)