This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma warning(disable:4996)
#include<stdio.h>
#include<algorithm>
#include <map>
using namespace std;
long long a, b, P, Q, C1, C2, TC1, TC2;
typedef long long ll;
const ll INF = (ll)1e18 + 5;
typedef pair<ll,ll> pll;
map<pll, ll> tmp[100005];
map<pll, ll> *ans;
ll mul (ll a, ll b) {
if((double)INF / a < b) return INF;
return a * b;
}
long long gcd_cost_quite_fast (long long a, long long b) {
if(a == 0 || b == 0)return 0;
if(ans->count(pll(a,b)) == 1) return (*ans)[pll(a,b)];
long long ret = 0;
if(a >= b) {
//correct
ret = gcd_cost_quite_fast(b, a%b) + P;
ret = min(ret, gcd_cost_quite_fast(a%b, b) + mul(Q, a/b));
}else {
ret = gcd_cost_quite_fast(a, b%a) + min(P+P, mul(Q, (b/a)));
}
return (*ans)[pll(a,b)] = ret;
}
int main()
{
int T;
scanf("%d", &T);
while (T--){
scanf("%lld%lld", &a, &b);
scanf("%lld%lld", &P, &Q);
ans = tmp+T;//.clear();
printf("%lld\n",gcd_cost_quite_fast(a, b));
}
}
# | 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... |