답안 #12786

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
12786 2015-01-03T13:12:46 Z Qwaz Min-cost GCD (GA9_mcg) C++
100 / 100
621 ms 1084 KB
#include <cstdio>
#include <algorithm>

using namespace std;
typedef long long ll;

const int MAX = 200;
const ll INF = 5123456789012345678LL;

int maxLevel;
ll p, q, memo[MAX][2];

ll minGcd(ll a, ll b, int level){
	if(a == 0 || b == 0) return 0;

	ll &now = a >= b ? memo[level][0] : memo[level][1];
	maxLevel = max(maxLevel, level);

	if(now != 0) return now;

	if(a >= b){
		now = minGcd(b, a%b, level+1)+p;

		ll count = (a - (a%b))/b;
		if((INF - minGcd(a%b, b, level+1))/count > q)
			now = min(now, minGcd(a%b, b, level+1) + q*count);
	} else {
		now = minGcd(b, a, level)+p;

		ll count = (b - (b%a))/a;
		if((INF - minGcd(a, b%a, level+1))/count > q)
			now = min(now, minGcd(a, b%a, level+1) + q*count);
	}

	return now;
}

void reset(){
	for(int i = 0; i <= maxLevel; i++)
		memo[i][0] = memo[i][1] = 0;
}

int main(){
	int t;
	for(scanf("%d", &t); t--;){
		ll a, b;
		scanf("%lld%lld%lld%lld", &a, &b, &p, &q);

		printf("%lld\n", minGcd(a, b, 0));

		reset();
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1084 KB Output is correct
2 Correct 0 ms 1084 KB Output is correct
3 Correct 0 ms 1084 KB Output is correct
4 Correct 0 ms 1084 KB Output is correct
5 Correct 0 ms 1084 KB Output is correct
6 Correct 0 ms 1084 KB Output is correct
7 Correct 0 ms 1084 KB Output is correct
8 Correct 0 ms 1084 KB Output is correct
9 Correct 0 ms 1084 KB Output is correct
10 Correct 0 ms 1084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 1084 KB Output is correct
2 Correct 140 ms 1084 KB Output is correct
3 Correct 117 ms 1084 KB Output is correct
4 Correct 133 ms 1084 KB Output is correct
5 Correct 123 ms 1084 KB Output is correct
6 Correct 145 ms 1084 KB Output is correct
7 Correct 136 ms 1084 KB Output is correct
8 Correct 127 ms 1084 KB Output is correct
9 Correct 136 ms 1084 KB Output is correct
10 Correct 131 ms 1084 KB Output is correct
11 Correct 137 ms 1084 KB Output is correct
12 Correct 139 ms 1084 KB Output is correct
13 Correct 124 ms 1084 KB Output is correct
14 Correct 0 ms 1084 KB Output is correct
15 Correct 0 ms 1084 KB Output is correct
16 Correct 1 ms 1084 KB Output is correct
17 Correct 0 ms 1084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1084 KB Output is correct
2 Correct 3 ms 1084 KB Output is correct
3 Correct 0 ms 1084 KB Output is correct
4 Correct 0 ms 1084 KB Output is correct
5 Correct 0 ms 1084 KB Output is correct
6 Correct 0 ms 1084 KB Output is correct
7 Correct 3 ms 1084 KB Output is correct
8 Correct 0 ms 1084 KB Output is correct
9 Correct 0 ms 1084 KB Output is correct
10 Correct 3 ms 1084 KB Output is correct
11 Correct 3 ms 1084 KB Output is correct
12 Correct 3 ms 1084 KB Output is correct
13 Correct 4 ms 1084 KB Output is correct
14 Correct 0 ms 1084 KB Output is correct
15 Correct 0 ms 1084 KB Output is correct
16 Correct 1 ms 1084 KB Output is correct
17 Correct 1 ms 1084 KB Output is correct
18 Correct 2 ms 1084 KB Output is correct
19 Correct 3 ms 1084 KB Output is correct
20 Correct 4 ms 1084 KB Output is correct
21 Correct 3 ms 1084 KB Output is correct
22 Correct 3 ms 1084 KB Output is correct
23 Correct 3 ms 1084 KB Output is correct
24 Correct 4 ms 1084 KB Output is correct
25 Correct 4 ms 1084 KB Output is correct
26 Correct 4 ms 1084 KB Output is correct
27 Correct 3 ms 1084 KB Output is correct
28 Correct 3 ms 1084 KB Output is correct
29 Correct 2 ms 1084 KB Output is correct
30 Correct 2 ms 1084 KB Output is correct
31 Correct 3 ms 1084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 509 ms 1084 KB Output is correct
2 Correct 506 ms 1084 KB Output is correct
3 Correct 512 ms 1084 KB Output is correct
4 Correct 498 ms 1084 KB Output is correct
5 Correct 503 ms 1084 KB Output is correct
6 Correct 498 ms 1084 KB Output is correct
7 Correct 505 ms 1084 KB Output is correct
8 Correct 509 ms 1084 KB Output is correct
9 Correct 508 ms 1084 KB Output is correct
10 Correct 504 ms 1084 KB Output is correct
11 Correct 512 ms 1084 KB Output is correct
12 Correct 491 ms 1084 KB Output is correct
13 Correct 512 ms 1084 KB Output is correct
14 Correct 595 ms 1084 KB Output is correct
15 Correct 594 ms 1084 KB Output is correct
16 Correct 607 ms 1084 KB Output is correct
17 Correct 603 ms 1084 KB Output is correct
18 Correct 596 ms 1084 KB Output is correct
19 Correct 200 ms 1084 KB Output is correct
20 Correct 177 ms 1084 KB Output is correct
21 Correct 204 ms 1084 KB Output is correct
22 Correct 189 ms 1084 KB Output is correct
23 Correct 239 ms 1084 KB Output is correct
24 Correct 247 ms 1084 KB Output is correct
25 Correct 240 ms 1084 KB Output is correct
26 Correct 237 ms 1084 KB Output is correct
27 Correct 220 ms 1084 KB Output is correct
28 Correct 603 ms 1084 KB Output is correct
29 Correct 586 ms 1084 KB Output is correct
30 Correct 590 ms 1084 KB Output is correct
31 Correct 547 ms 1084 KB Output is correct
32 Correct 568 ms 1084 KB Output is correct
33 Correct 621 ms 1084 KB Output is correct
34 Correct 607 ms 1084 KB Output is correct
35 Correct 621 ms 1084 KB Output is correct