This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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();
    }
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |