제출 #20134

#제출 시각아이디문제언어결과실행 시간메모리
20134hongjun7Min-cost GCD (GA9_mcg)C++14
30 / 100
1000 ms1720 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...