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<stdio.h>
#include<algorithm>
using namespace std;
typedef long long lld;
lld a, b, p, q, dy[155][2];
void gcd(lld ix, lld x, lld y){
if(x<y)swap(x,y);
if(y==0){
dy[ix][0]=dy[ix][1]=0;
return;
}
gcd(ix+1, y, x%y);
if((p+dy[ix+1][0]-dy[ix+1][1])/q >= x/y)dy[ix][0]=q*(x/y)+dy[ix+1][1];
else dy[ix][0]=p+dy[ix+1][0];
if((p+dy[ix][0]-dy[ix+1][0])/q >= x/y)dy[ix][1]=q*(x/y)+dy[ix+1][0];
else dy[ix][1]=p+dy[ix][0];
}
int main(){
int t;
for(scanf("%d",&t); t--;){
scanf("%lld%lld%lld%lld", &a, &b, &p, &q);
gcd(0,a,b);
printf("%lld\n", dy[0][a<b]);
}
return 0;
}
# | 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... |