BestCoder Round #75 King's Cake 模拟&&优化 || gcd
2017-07-04 14:10
381 查看
King's Cake
Accepts: 967Submissions: 1572
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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 test cases.
For each test case, the first line and the only line contains two positive numbers n,m(1≤n,m≤10000).
Output
For each test case, print a single number as the answer.
Sample Input
Copy
2 2 3 2 5
Sample Output
Copy
3 4 hint: For the first testcase you can divide the into one cake of 2×2 , 2 cakes of 1×1
source
The question is from BC
and hduoj 5640.
My Solution
//A really easy problem, I get a Runtime Error(STACK_OVERFLOW) first, then Time Limit Exceeded
//next Runtime Error (INTEGER_DIVIDE_BY_ZERO), and a WA , Accepted......
//I am really sorry for that.
1、用循环模拟
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int tot; int main() { int T, l, s, t; scanf("%d", &T); while(T--){ scanf("%d%d", &l, &s); if(l < s) swap(l, s); tot = 0; while(true){ if(s == 1) {tot += l; break;} if( s == 0) break; //! t = l/s; l -= t*s; if(l < s) swap(l, s); tot += t; } printf("%d\n", tot); } return 0; }
2、像是求最大公约数。所以每次 gcd 的时候累加答案就可以,复杂度O(logmax(n,m)T)。
Thank you!
相关文章推荐
- BestCoder Round #75 T3 King's Order HDOJ 5642
- BestCoder Round #75 King's Order dp:数位dp
- BestCoder Round #75 King's Cake 模拟&&优化 || gcd
- BestCoder Round #65 ZYB's Game
- BestCoder Round#29 1001 GTY's math problem
- BestCoder Round #70 Jam's store(网络流)
- BestCoder Round #70 1002 Jam's balance(DP)
- BestCoder Round #29——A--GTY&#39;s math problem(快速幂(对数法))、B--GTY&#39;s birthday gift(矩阵快速幂)
- BestCoder Round #75 1003 - King's Order
- HDU5105 Math Problem && BestCoder Round #18 1002
- BestCoder Round #75
- BestCoder Round #68 (div.1) B 矩阵乘法优化DP
- BestCoder Round #75 总结
- 【Best Coder】#29 B GTY's birthday gift(快速幂|mod的时候记得负!)
- BestCoder Round #75 1003 数位DP
- 【HDU5648 BestCoder Round 76 (div1)C】【打表or数位DP】DZY Loves Math nm范围内i和j i或j的gcd之和
- HDU 5069 Harry And Biological Teacher AC自动机 + 线段树优化 (BestCoder Round 14 D)
- hdu 5196 DZY Loves Inversions && BestCoder Round #35
- BestCoder Round #75
- BestCoder Round #75