Submission #12125

#TimeUsernameProblemLanguageResultExecution timeMemory
12125gs14004Min-cost GCD (GA9_mcg)C++98
57 / 100
1000 ms1324 KiB
#include <cstdio> #include <cmath> #include <algorithm> #include <map> using namespace std; typedef long long lint; typedef pair<lint,lint> pi; map<pi,lint> mp; lint a,b,p,q; bool calc_valid(lint x, lint y){ double t = log10(x)+log10(y); return t < 18; } lint f(lint x, lint y){ if(x == 0 || y == 0) return 0; if(mp.find(pi(x,y)) != mp.end()) return mp[pi(x,y)]; if(x >= y){ if(!calc_valid(q,x/y)) return mp[pi(x,y)] = f(y,x%y) + p; else return mp[pi(x,y)] = min(f(y,x%y) + p,f(x%y,y) + q*(x/y)); } else{ if(!calc_valid(q,y/x)) return 5e18; else return f(x,y%x) + q * (y/x); } } void solve(){ mp.clear(); scanf("%lld %lld %lld %lld",&a,&b,&p,&q); if(a < b) printf("%lld\n",min(p + f(b,a),f(a,b))); else printf("%lld\n",f(a,b)); } int main(){ int t; scanf("%d",&t); while (t--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...