-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10253.cpp
More file actions
52 lines (46 loc) · 681 Bytes
/
Copy path10253.cpp
File metadata and controls
52 lines (46 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <algorithm>
using namespace std;
int gcd(int a, int b) {
int x, y, r;
x = max(a, b);
y = min(a, b);
r = x % y;
while (r > 0) {
x = y;
y = r;
r = x % y;
}
return y;
}
int main() {
int n;
int a, b;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
for (int j = 2, g;; j++) {
if (b == j) {
if (a != 1)
continue;
else {
printf("%d\n", b);
break;
}
}
if ((double)a / b>1.0 / j) {
g = gcd(b, j);
a = (a * j) / g - b / g;
b = b * j / g;
g = gcd(a, b);
a /= g;
b /= g;
if (a == 1) {
printf("%d\n", b);
break;
}
}
}
}
return 0;
}