# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
199842 | human1234 | Min-cost GCD (GA9_mcg) | C++14 | 162 ms | 8440 KiB |
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 <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)
# | 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... |