Submission #20144

# Submission time Handle Problem Language Result Execution time Memory
20144 2016-02-28T00:21:29 Z hongjun7 Min-cost GCD (GA9_mcg) C++14
100 / 100
265 ms 1088 KB
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long ll;
static char _buffer[1024];
static int _currentChar = 0;
static int _charsNumber = 0;
static inline int _read() {
	if (_charsNumber < 0) {
		exit(1);
	}
	if (!_charsNumber || _currentChar == _charsNumber) {
		_charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), stdin);
		_currentChar = 0;
	}
	if (_charsNumber <= 0) {
		return -1;
	}
	return _buffer[_currentChar++];
}
static inline ll _readll() {
	int c, s; ll x;
	c = _read();
	while (c <= 32) c = _read();
	x = 0;
	s = 1;
	if (c == '-') {
		s = -1;
		c = _read();
	}
	while (c > 32) {
		x *= 10;
		x += c - '0';
		c = _read();
	}
	if (s < 0) x = -x;
	return x;
}
ll a, b, p, q, d[222][2];
void f(ll x, ll y, int z) {
	if (x < y) swap(x, y);
	if (x == 0 || y == 0) {
		d[z][0] = d[z][1] = 0;
		return;
	}
	f(y, x%y, z + 1);
	if ((p + d[z + 1][0] - d[z + 1][1]) / q >= x / y) d[z][0] = q*(x / y) + d[z + 1][1];
	else d[z][0] = p + d[z + 1][0];
	if ((p + d[z][0] - d[z + 1][0]) / q >= x / y) d[z][1] = q*(x / y) + d[z + 1][0];
	else d[z][1] = p + d[z][0];
}
int main() {
	int T;
	for (scanf("%d", &T); T--; ) {
		a = _readll(); b = _readll(); p = _readll(); q = _readll();
		f(a, b, 0);
		printf("%lld\n", d[0][a < b]);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1088 KB Output is correct
2 Correct 0 ms 1088 KB Output is correct
3 Correct 0 ms 1088 KB Output is correct
4 Correct 0 ms 1088 KB Output is correct
5 Correct 0 ms 1088 KB Output is correct
6 Correct 0 ms 1088 KB Output is correct
7 Correct 0 ms 1088 KB Output is correct
8 Correct 0 ms 1088 KB Output is correct
9 Correct 0 ms 1088 KB Output is correct
10 Correct 0 ms 1088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1088 KB Output is correct
2 Correct 0 ms 1088 KB Output is correct
3 Correct 0 ms 1088 KB Output is correct
4 Correct 0 ms 1088 KB Output is correct
5 Correct 37 ms 1088 KB Output is correct
6 Correct 67 ms 1088 KB Output is correct
7 Correct 53 ms 1088 KB Output is correct
8 Correct 66 ms 1088 KB Output is correct
9 Correct 38 ms 1088 KB Output is correct
10 Correct 55 ms 1088 KB Output is correct
11 Correct 0 ms 1088 KB Output is correct
12 Correct 48 ms 1088 KB Output is correct
13 Correct 0 ms 1088 KB Output is correct
14 Correct 0 ms 1088 KB Output is correct
15 Correct 0 ms 1088 KB Output is correct
16 Correct 0 ms 1088 KB Output is correct
17 Correct 0 ms 1088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1088 KB Output is correct
2 Correct 0 ms 1088 KB Output is correct
3 Correct 0 ms 1088 KB Output is correct
4 Correct 0 ms 1088 KB Output is correct
5 Correct 0 ms 1088 KB Output is correct
6 Correct 0 ms 1088 KB Output is correct
7 Correct 0 ms 1088 KB Output is correct
8 Correct 0 ms 1088 KB Output is correct
9 Correct 0 ms 1088 KB Output is correct
10 Correct 0 ms 1088 KB Output is correct
11 Correct 0 ms 1088 KB Output is correct
12 Correct 0 ms 1088 KB Output is correct
13 Correct 0 ms 1088 KB Output is correct
14 Correct 0 ms 1088 KB Output is correct
15 Correct 0 ms 1088 KB Output is correct
16 Correct 0 ms 1088 KB Output is correct
17 Correct 1 ms 1088 KB Output is correct
18 Correct 0 ms 1088 KB Output is correct
19 Correct 0 ms 1088 KB Output is correct
20 Correct 0 ms 1088 KB Output is correct
21 Correct 0 ms 1088 KB Output is correct
22 Correct 0 ms 1088 KB Output is correct
23 Correct 0 ms 1088 KB Output is correct
24 Correct 0 ms 1088 KB Output is correct
25 Correct 0 ms 1088 KB Output is correct
26 Correct 0 ms 1088 KB Output is correct
27 Correct 0 ms 1088 KB Output is correct
28 Correct 0 ms 1088 KB Output is correct
29 Correct 0 ms 1088 KB Output is correct
30 Correct 0 ms 1088 KB Output is correct
31 Correct 0 ms 1088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 1088 KB Output is correct
2 Correct 265 ms 1088 KB Output is correct
3 Correct 221 ms 1088 KB Output is correct
4 Correct 206 ms 1088 KB Output is correct
5 Correct 194 ms 1088 KB Output is correct
6 Correct 255 ms 1088 KB Output is correct
7 Correct 199 ms 1088 KB Output is correct
8 Correct 193 ms 1088 KB Output is correct
9 Correct 209 ms 1088 KB Output is correct
10 Correct 213 ms 1088 KB Output is correct
11 Correct 221 ms 1088 KB Output is correct
12 Correct 206 ms 1088 KB Output is correct
13 Correct 219 ms 1088 KB Output is correct
14 Correct 91 ms 1088 KB Output is correct
15 Correct 27 ms 1088 KB Output is correct
16 Correct 97 ms 1088 KB Output is correct
17 Correct 217 ms 1088 KB Output is correct
18 Correct 176 ms 1088 KB Output is correct
19 Correct 82 ms 1088 KB Output is correct
20 Correct 0 ms 1088 KB Output is correct
21 Correct 63 ms 1088 KB Output is correct
22 Correct 107 ms 1088 KB Output is correct
23 Correct 0 ms 1088 KB Output is correct
24 Correct 86 ms 1088 KB Output is correct
25 Correct 103 ms 1088 KB Output is correct
26 Correct 0 ms 1088 KB Output is correct
27 Correct 110 ms 1088 KB Output is correct
28 Correct 101 ms 1088 KB Output is correct
29 Correct 158 ms 1088 KB Output is correct
30 Correct 202 ms 1088 KB Output is correct
31 Correct 0 ms 1088 KB Output is correct
32 Correct 218 ms 1088 KB Output is correct
33 Correct 224 ms 1088 KB Output is correct
34 Correct 228 ms 1088 KB Output is correct
35 Correct 41 ms 1088 KB Output is correct