Submission #12072

# Submission time Handle Problem Language Result Execution time Memory
12072 2014-12-20T20:59:52 Z ainta Min-cost GCD (GA9_mcg) C++
30 / 100
165 ms 1084 KB
#include<stdio.h>
#include<algorithm>
#define INF 999999999999999999LL
using namespace std;
long long a, b, sub, mod, C1, C2, TC1, TC2;
int main()
{
	int T;
	scanf("%d", &T);
	while (T--){
		scanf("%lld%lld", &a, &b);
		scanf("%lld%lld", &mod, &sub);
		C1 = 0, C2 = INF;
		while (a && b){
			if (a == b){
				C1 = C1 + min(sub, mod);
				C2 = C2 + min(sub, mod);
				a -= b;
				continue;
			}
			if (a < b){
				if (C2 > C1 + mod)C2 = C1 + mod;
				TC1 = min(C2 + mod, C1 + (b / a)* sub);
				TC2 = min(C2 + (b / a)*sub, TC1 + mod);
				b = b%a;
			}
			else{
				if (C1 > C2 + mod)C1 = C2 + mod;
				TC2 = min(C1 + mod, C2 + (a / b)* sub); 
				TC1 = min(C1 + (a / b)*sub, TC2 + mod);
				a = a%b;
			}
			C1 = TC1, C2 = TC2;
		}
		printf("%lld\n", min(C1, C2));
	}
}
# Verdict Execution time Memory 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
# Verdict Execution time Memory Grader output
1 Correct 60 ms 1084 KB Output is correct
2 Correct 82 ms 1084 KB Output is correct
3 Correct 75 ms 1084 KB Output is correct
4 Correct 73 ms 1084 KB Output is correct
5 Correct 48 ms 1084 KB Output is correct
6 Correct 68 ms 1084 KB Output is correct
7 Correct 66 ms 1084 KB Output is correct
8 Correct 69 ms 1084 KB Output is correct
9 Correct 76 ms 1084 KB Output is correct
10 Correct 71 ms 1084 KB Output is correct
11 Correct 72 ms 1084 KB Output is correct
12 Correct 75 ms 1084 KB Output is correct
13 Correct 71 ms 1084 KB Output is correct
14 Correct 1 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1084 KB Output is correct
2 Incorrect 1 ms 1084 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 165 ms 1084 KB Output isn't correct
2 Halted 0 ms 0 KB -