#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long lld;
lld a, b, p, q, dy[155][2];
void gcd(lld ix, lld x, lld y){
if(x<y)swap(x,y);
if(y==0){
dy[ix][0]=dy[ix][1]=0;
return;
}
gcd(ix+1, y, x%y);
if((p+dy[ix+1][0]-dy[ix+1][1])/q >= x/y)dy[ix][0]=q*(x/y)+dy[ix+1][1];
else dy[ix][0]=p+dy[ix+1][0];
if((p+dy[ix][0]-dy[ix+1][0])/q >= x/y)dy[ix][1]=q*(x/y)+dy[ix+1][0];
else dy[ix][1]=p+dy[ix][0];
}
int main(){
int t;
for(scanf("%d",&t); t--;){
scanf("%lld%lld%lld%lld", &a, &b, &p, &q);
gcd(0,a,b);
printf("%lld\n", dy[0][a<b]);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
1084 KB |
Output is correct |
2 |
Correct |
91 ms |
1084 KB |
Output is correct |
3 |
Correct |
96 ms |
1084 KB |
Output is correct |
4 |
Correct |
88 ms |
1084 KB |
Output is correct |
5 |
Correct |
81 ms |
1084 KB |
Output is correct |
6 |
Correct |
87 ms |
1084 KB |
Output is correct |
7 |
Correct |
95 ms |
1084 KB |
Output is correct |
8 |
Correct |
88 ms |
1084 KB |
Output is correct |
9 |
Correct |
79 ms |
1084 KB |
Output is correct |
10 |
Correct |
84 ms |
1084 KB |
Output is correct |
11 |
Correct |
63 ms |
1084 KB |
Output is correct |
12 |
Correct |
56 ms |
1084 KB |
Output is correct |
13 |
Correct |
80 ms |
1084 KB |
Output is correct |
14 |
Correct |
0 ms |
1084 KB |
Output is correct |
15 |
Correct |
0 ms |
1084 KB |
Output is correct |
16 |
Correct |
0 ms |
1084 KB |
Output is correct |
17 |
Correct |
0 ms |
1084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1084 KB |
Output is correct |
2 |
Correct |
1 ms |
1084 KB |
Output is correct |
3 |
Correct |
0 ms |
1084 KB |
Output is correct |
4 |
Correct |
1 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 |
2 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 |
11 |
Correct |
0 ms |
1084 KB |
Output is correct |
12 |
Correct |
0 ms |
1084 KB |
Output is correct |
13 |
Correct |
2 ms |
1084 KB |
Output is correct |
14 |
Correct |
0 ms |
1084 KB |
Output is correct |
15 |
Correct |
0 ms |
1084 KB |
Output is correct |
16 |
Correct |
0 ms |
1084 KB |
Output is correct |
17 |
Correct |
0 ms |
1084 KB |
Output is correct |
18 |
Correct |
0 ms |
1084 KB |
Output is correct |
19 |
Correct |
2 ms |
1084 KB |
Output is correct |
20 |
Correct |
0 ms |
1084 KB |
Output is correct |
21 |
Correct |
2 ms |
1084 KB |
Output is correct |
22 |
Correct |
2 ms |
1084 KB |
Output is correct |
23 |
Correct |
0 ms |
1084 KB |
Output is correct |
24 |
Correct |
0 ms |
1084 KB |
Output is correct |
25 |
Correct |
2 ms |
1084 KB |
Output is correct |
26 |
Correct |
0 ms |
1084 KB |
Output is correct |
27 |
Correct |
2 ms |
1084 KB |
Output is correct |
28 |
Correct |
0 ms |
1084 KB |
Output is correct |
29 |
Correct |
0 ms |
1084 KB |
Output is correct |
30 |
Correct |
0 ms |
1084 KB |
Output is correct |
31 |
Correct |
2 ms |
1084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
1084 KB |
Output is correct |
2 |
Correct |
253 ms |
1084 KB |
Output is correct |
3 |
Correct |
245 ms |
1084 KB |
Output is correct |
4 |
Correct |
265 ms |
1084 KB |
Output is correct |
5 |
Correct |
280 ms |
1084 KB |
Output is correct |
6 |
Correct |
104 ms |
1084 KB |
Output is correct |
7 |
Correct |
272 ms |
1084 KB |
Output is correct |
8 |
Correct |
269 ms |
1084 KB |
Output is correct |
9 |
Correct |
263 ms |
1084 KB |
Output is correct |
10 |
Correct |
267 ms |
1084 KB |
Output is correct |
11 |
Correct |
273 ms |
1084 KB |
Output is correct |
12 |
Correct |
259 ms |
1084 KB |
Output is correct |
13 |
Correct |
269 ms |
1084 KB |
Output is correct |
14 |
Correct |
279 ms |
1084 KB |
Output is correct |
15 |
Correct |
268 ms |
1084 KB |
Output is correct |
16 |
Correct |
260 ms |
1084 KB |
Output is correct |
17 |
Correct |
275 ms |
1084 KB |
Output is correct |
18 |
Correct |
253 ms |
1084 KB |
Output is correct |
19 |
Correct |
119 ms |
1084 KB |
Output is correct |
20 |
Correct |
108 ms |
1084 KB |
Output is correct |
21 |
Correct |
140 ms |
1084 KB |
Output is correct |
22 |
Correct |
128 ms |
1084 KB |
Output is correct |
23 |
Correct |
140 ms |
1084 KB |
Output is correct |
24 |
Correct |
157 ms |
1084 KB |
Output is correct |
25 |
Correct |
147 ms |
1084 KB |
Output is correct |
26 |
Correct |
158 ms |
1084 KB |
Output is correct |
27 |
Correct |
155 ms |
1084 KB |
Output is correct |
28 |
Correct |
273 ms |
1084 KB |
Output is correct |
29 |
Correct |
247 ms |
1084 KB |
Output is correct |
30 |
Correct |
276 ms |
1084 KB |
Output is correct |
31 |
Correct |
254 ms |
1084 KB |
Output is correct |
32 |
Correct |
271 ms |
1084 KB |
Output is correct |
33 |
Correct |
255 ms |
1084 KB |
Output is correct |
34 |
Correct |
280 ms |
1084 KB |
Output is correct |
35 |
Correct |
276 ms |
1084 KB |
Output is correct |