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>
#include <map>
using namespace std;
long long a,b,p,q;
std::map<pair<long long, long long>, long long> chk;
long long go(long long a, long long b)
{
if (a == 0 || b == 0) return 0;
if (a == b) return p < q ? p : q;
if (chk.count(make_pair(a,b))) return chk[make_pair(a,b)];
long long &r = chk[make_pair(a,b)];
r = p + go(b,a%b);
if (a > b){
long long d = a / b;
r = min(r, q * d + go(a-b*d,b));
}
else{
long long d = b / a;
r = min(r, q * d + go(a,b-a*d));
}
return r;
}
int main()
{
int T; scanf ("%d",&T); while (T--){
scanf ("%lld %lld %lld %lld",&a,&b,&p,&q);
chk.clear();
printf ("%lld\n",go(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... |