Submission #12159

# Submission time Handle Problem Language Result Execution time Memory
12159 2014-12-21T12:34:23 Z xhae Min-cost GCD (GA9_mcg) C++14
41 / 100
1000 ms 1528 KB
#include <set>
#include <map>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

long long dp[200][200];

long long a, b, p, q;
const long long INF = 1000000ll * 1000000ll * 500000ll;

set<long long> nodes;
map<long long, int> hash_;

void preproc(long long a, long long b)
{
	nodes.insert(a);
	nodes.insert(b);
	if(a == 0 || b == 0) return;
	preproc(b, a % b);
}

long long getAns(long long a, long long b)
{
	if(a == 0 || b == 0) return 0;

	int i1 = hash_[a], i2 = hash_[b];
	long long &ret = dp[i1][i2];
	if(ret != -1) return ret;
	
	ret = p + getAns(b, a % b);

	if(a >= b)
	{
		long long howMany = a / b;
		if(howMany <= ret / (double)q)
		{
			long long cand = howMany * q + getAns(a % b, b);
			ret = min(cand, ret);
		}
	}
	else
	{
		long long howMany = b / a;
		if(howMany <= ret / (double)q)
		{
			long long cand = howMany * q + getAns(a, b % a);
			ret = min(cand, ret);
		}
	}

	return ret;
}

int main(void)
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%lld %lld %lld %lld", &a, &b, &p, &q);
		nodes.clear();
		preproc(a, b);
		int ind = 0;
		hash_.clear();
		for(auto v: nodes) hash_[v] = ind++;

		memset(dp, -1, sizeof(dp));
		printf("%lld\n", getAns(a, b));
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1528 KB Output is correct
2 Correct 0 ms 1528 KB Output is correct
3 Correct 2 ms 1528 KB Output is correct
4 Correct 2 ms 1528 KB Output is correct
5 Correct 2 ms 1528 KB Output is correct
6 Correct 2 ms 1528 KB Output is correct
7 Correct 0 ms 1528 KB Output is correct
8 Correct 2 ms 1528 KB Output is correct
9 Correct 0 ms 1528 KB Output is correct
10 Correct 0 ms 1528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1000 ms 1528 KB Program timed out
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1528 KB Output is correct
2 Correct 25 ms 1528 KB Output is correct
3 Correct 26 ms 1528 KB Output is correct
4 Correct 24 ms 1528 KB Output is correct
5 Correct 24 ms 1528 KB Output is correct
6 Correct 24 ms 1528 KB Output is correct
7 Correct 25 ms 1528 KB Output is correct
8 Correct 24 ms 1528 KB Output is correct
9 Correct 17 ms 1528 KB Output is correct
10 Correct 24 ms 1528 KB Output is correct
11 Correct 22 ms 1528 KB Output is correct
12 Correct 24 ms 1528 KB Output is correct
13 Correct 25 ms 1528 KB Output is correct
14 Correct 21 ms 1528 KB Output is correct
15 Correct 20 ms 1528 KB Output is correct
16 Correct 18 ms 1528 KB Output is correct
17 Correct 16 ms 1528 KB Output is correct
18 Correct 17 ms 1528 KB Output is correct
19 Correct 29 ms 1528 KB Output is correct
20 Correct 29 ms 1528 KB Output is correct
21 Correct 28 ms 1528 KB Output is correct
22 Correct 27 ms 1528 KB Output is correct
23 Correct 27 ms 1528 KB Output is correct
24 Correct 27 ms 1528 KB Output is correct
25 Correct 27 ms 1528 KB Output is correct
26 Correct 24 ms 1528 KB Output is correct
27 Correct 29 ms 1528 KB Output is correct
28 Correct 29 ms 1528 KB Output is correct
29 Correct 27 ms 1528 KB Output is correct
30 Correct 28 ms 1528 KB Output is correct
31 Correct 28 ms 1528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1000 ms 1528 KB Program timed out
2 Halted 0 ms 0 KB -