#include <cstdio>
#include <cmath>
typedef long long lint;
lint a,b,p,q;
bool calc_valid(lint x, lint y){
double t = log10(x)+log10(y);
return t < 17;
}
lint f(lint x, lint y){
if(x == 0 || y == 0) return 0;
lint c1 = f(y,x%y) + p; // 결코 답안은 10^17을 넘지 않는다
lint c2;
if(x >= y){
if(!calc_valid(q,x/y)) return c1;
else c2 = f(x%y,y) + q*(x/y);
}
else{
if(!calc_valid(q,y/x)) return c1;
else c2 = f(x,y%x) + q*(y/x);
}
return c1 < c2 ? c1 : c2;
}
void solve(){
scanf("%lld %lld %lld %lld",&a,&b,&p,&q);
printf("%lld\n",f(a,b));
}
int main(){
int t;
scanf("%d",&t);
while (t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1196 KB |
Output is correct |
2 |
Correct |
0 ms |
1196 KB |
Output is correct |
3 |
Correct |
0 ms |
1196 KB |
Output is correct |
4 |
Correct |
0 ms |
1196 KB |
Output is correct |
5 |
Correct |
0 ms |
1196 KB |
Output is correct |
6 |
Correct |
0 ms |
1196 KB |
Output is correct |
7 |
Correct |
0 ms |
1196 KB |
Output is correct |
8 |
Correct |
1 ms |
1196 KB |
Output is correct |
9 |
Correct |
0 ms |
1196 KB |
Output is correct |
10 |
Correct |
0 ms |
1196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1000 ms |
1196 KB |
Program timed out |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1000 ms |
1196 KB |
Program timed out |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1000 ms |
1192 KB |
Program timed out |
2 |
Halted |
0 ms |
0 KB |
- |