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>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a, b, p, q;
map <pair<ll, ll>, ll> d;
ll f(ll x, ll y) {
if (x == 0 || y == 0) return 0;
if (d.count({ x, y })) return d[{x, y}];
ll d1 = f(y, x%y) + p;
ll d2 = 0;
if (x >= y) d2 = f(x % y, y) + (x / y)*q;
else d2 = f(x, y % x) + (y / x)*q;
ll D = min(d1, d2);
d[{x, y}] = D;
return D;
}
int main() {
int T;
for (cin >> T; T--; ) {
cin >> a >> b >> p >> q;
d.clear();
cout << f(a, b) << "\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... |