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 <iostream>
#include <vector>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
ll a, b, p, q;
vpll v;
cin >> a >> b >> p >> q;
v.resize(100);
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 = {p, k * q};
} else {
ll f, s;
f = min(past.first + p, past.second + k * q);
s = min(past.first + k * q, past.second + p + k * q);
next = {f, s};
}
c++;
v[c] = next;
a = b;
b = r;
}
cout << min(v[c].first, v[c].second) << "\n";
}
}
# | 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... |