이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |