이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <algorithm>
#include <map>
using namespace std;
long long a,b,p,q,l;
std::map<int, long long> chk;
long long go(long long a, long long b)
{
	if (a == 0 || b == 0) return 0;
	if (a == b) return p < q ? p : q;
	int h = a*b+a*3+b;
	long long &r = chk[h];
	if (r) return r;
	r = p + go(b,a%b);
	if (a > b){
		long long d = a / b, e = q * d;
		if (d <= l) r = min(r, e+go(a-b*d,b));
	}
	else{
		long long d = b / a, e = q * d;
		if (d <= l) r = min(r, e+go(a,b-a*d));
	}
	return r;
}
int main()
{
	int T; scanf ("%d",&T); while (T--){
		scanf ("%lld %lld %lld %lld",&a,&b,&p,&q);
		chk.clear(); l = 1e18 / q;
		printf ("%lld\n",go(a,b));
	}
	return 0;
}
| # | 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... |