Submission #13224

#TimeUsernameProblemLanguageResultExecution timeMemory
13224yourIDMin-cost GCD (GA9_mcg)C++14
57 / 100
1000 ms1212 KiB
#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> 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.clear(); printf("%lld\n",gcd_cost_quite_fast(a, b)); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...