King's Cake
Accepts: 967
Submissions: 1572
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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)。