Submission #199842

#TimeUsernameProblemLanguageResultExecution timeMemory
199842human1234Min-cost GCD (GA9_mcg)C++14
0 / 100
162 ms8440 KiB
#include <stdio.h> #include <vector> using namespace std; using i128 = __int128; using ll = long long; using pll = pair<ll, ll>; using vpll = vector<pll>; const ll LIMIT = 1e19; inline ll conv(i128 x) { if (x > (i128)LIMIT) return LIMIT; return (ll)x; } int main() { int t; vpll v; scanf("%d", &t); v.resize(300); while (t--) { ll a, b, p, q; scanf("%lld%lld%lld%lld", &a, &b, &p, &q); if (a > b) { v[0] = {0, -1}; } else { ll t = a; a = b; b = t; v[0] = {p, 0}; } int c = 0; while (a && b) { ll k = a / b, r = a % b; pll past = v[c], next; if (past.second == -1) { next = {conv((i128)p), conv((i128)k * (i128)q)}; } else { ll f, s; f = min(conv((i128)past.first + (i128)p), conv((i128)past.second + (i128)k * (i128)q)); s = min(conv((i128)past.first + (i128)k * (i128)q), conv((i128)past.second + (i128)p + (i128)k * (i128)q)); next = {f, s}; } c++; v[c] = next; a = b; b = r; } printf("%lld\n", min(v[c].first, v[c].second)); } }

Compilation message (stderr)

mcg.cpp:11:18: warning: overflow in implicit constant conversion [-Woverflow]
 const ll LIMIT = 1e19;
                  ^~~~
mcg.cpp: In function 'int main()':
mcg.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &t);
     ~~~~~^~~~~~~~~~
mcg.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld%lld%lld%lld", &a, &b, &p, &q);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...