Submission #12126

# Submission time Handle Problem Language Result Execution time Memory
12126 2014-12-21T07:44:08 Z kriii Min-cost GCD (GA9_mcg) C++14
57 / 100
1000 ms 1576 KB
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

long long a,b,p,q,l;

vector<pair<long long, long long> > u;

void gcd(long long a, long long b)
{
	if (a == 0 || b == 0);
	else if(a > b) gcd(b,a%b);
	else gcd(a,b%a);
	if (a < b) swap(a,b);

	u.push_back(make_pair(a,b));
	if (a != b) u.push_back(make_pair(b,a));
}

int find(vector<pair<long long, long long> > &e, pair<long long, long long> p)
{
	for (int i=0;i<e.size();i++) if (e[i] == p) return i;
	return -1;
}

int main()
{
	int T; scanf ("%d",&T); while (T--){
		scanf ("%lld %lld %lld %lld",&a,&b,&p,&q);
		u.clear(); l = 1e18 / q;
		gcd(a,b);
		vector<long long> d(u.size());
		for (int i=0;i<u.size();i++){
			if (u[i].first == 0 || u[i].second == 0) d[i] = 0;
			else if (u[i].first == u[i].second) d[i] = p < q ? p : q;
			else{
				long long na = u[i].first, nb = u[i].second;
				d[i] = p + d[find(u,make_pair(nb,na%nb))];
				if (na > nb){
					long long nd = na / nb, ne = q * nd;
					if (nd <= l) d[i] = min(d[i], ne+d[find(u,make_pair(na-nb*nd,nb))]);
				}
				else{
					long long nd = nb / na, ne = q * nd;
					if (nd <= l) d[i] = min(d[i], ne+d[find(u,make_pair(na,nb-na*nd))]);
				}
			}
		}
		printf ("%lld\n",d[find(u,make_pair(a,b))]);
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1576 KB Output is correct
2 Correct 0 ms 1576 KB Output is correct
3 Correct 0 ms 1576 KB Output is correct
4 Correct 0 ms 1576 KB Output is correct
5 Correct 0 ms 1576 KB Output is correct
6 Correct 0 ms 1576 KB Output is correct
7 Correct 0 ms 1576 KB Output is correct
8 Correct 0 ms 1576 KB Output is correct
9 Correct 0 ms 1576 KB Output is correct
10 Correct 0 ms 1576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 1576 KB Output is correct
2 Correct 160 ms 1576 KB Output is correct
3 Correct 145 ms 1576 KB Output is correct
4 Correct 147 ms 1576 KB Output is correct
5 Correct 146 ms 1576 KB Output is correct
6 Correct 168 ms 1576 KB Output is correct
7 Correct 155 ms 1576 KB Output is correct
8 Correct 156 ms 1576 KB Output is correct
9 Correct 159 ms 1576 KB Output is correct
10 Correct 153 ms 1576 KB Output is correct
11 Correct 158 ms 1576 KB Output is correct
12 Correct 144 ms 1576 KB Output is correct
13 Correct 150 ms 1576 KB Output is correct
14 Correct 0 ms 1576 KB Output is correct
15 Correct 0 ms 1576 KB Output is correct
16 Correct 1 ms 1576 KB Output is correct
17 Correct 0 ms 1576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1576 KB Output is correct
2 Correct 4 ms 1576 KB Output is correct
3 Correct 3 ms 1576 KB Output is correct
4 Correct 6 ms 1576 KB Output is correct
5 Correct 4 ms 1576 KB Output is correct
6 Correct 4 ms 1576 KB Output is correct
7 Correct 4 ms 1576 KB Output is correct
8 Correct 4 ms 1576 KB Output is correct
9 Correct 4 ms 1576 KB Output is correct
10 Correct 4 ms 1576 KB Output is correct
11 Correct 0 ms 1576 KB Output is correct
12 Correct 4 ms 1576 KB Output is correct
13 Correct 0 ms 1576 KB Output is correct
14 Correct 0 ms 1576 KB Output is correct
15 Correct 2 ms 1576 KB Output is correct
16 Correct 0 ms 1576 KB Output is correct
17 Correct 2 ms 1576 KB Output is correct
18 Correct 0 ms 1576 KB Output is correct
19 Correct 0 ms 1576 KB Output is correct
20 Correct 6 ms 1576 KB Output is correct
21 Correct 6 ms 1576 KB Output is correct
22 Correct 6 ms 1576 KB Output is correct
23 Correct 6 ms 1576 KB Output is correct
24 Correct 0 ms 1576 KB Output is correct
25 Correct 0 ms 1576 KB Output is correct
26 Correct 6 ms 1576 KB Output is correct
27 Correct 3 ms 1576 KB Output is correct
28 Correct 2 ms 1576 KB Output is correct
29 Correct 4 ms 1576 KB Output is correct
30 Correct 6 ms 1576 KB Output is correct
31 Correct 5 ms 1576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 832 ms 1576 KB Output is correct
2 Correct 858 ms 1576 KB Output is correct
3 Correct 833 ms 1576 KB Output is correct
4 Correct 851 ms 1576 KB Output is correct
5 Correct 847 ms 1576 KB Output is correct
6 Correct 848 ms 1576 KB Output is correct
7 Correct 833 ms 1576 KB Output is correct
8 Correct 850 ms 1576 KB Output is correct
9 Correct 866 ms 1576 KB Output is correct
10 Correct 845 ms 1576 KB Output is correct
11 Correct 842 ms 1576 KB Output is correct
12 Correct 854 ms 1576 KB Output is correct
13 Correct 859 ms 1576 KB Output is correct
14 Execution timed out 1000 ms 1576 KB Program timed out
15 Halted 0 ms 0 KB -