#include<stdio.h>
#include<memory.h>
typedef long long lld;
lld a, b, p, q, dy[70][2], chk[70][2];
lld gcd(int ix, lld a, lld b){
if(!a || !b)return 0;
if(a<b){
if(chk[ix][0])return dy[ix][0];
chk[ix][0]=1, dy[ix][0]=gcd(ix+1,a,b%a)+2*p;
if(b/a <= (2*p)/q)dy[ix][0]=gcd(ix+1,a,b%a)+(b/a)*q;
return dy[ix][0];
}
else{
if(chk[ix][1])return dy[ix][1];
chk[ix][1]=1, dy[ix][1]=gcd(ix+1,b,a%b)+p;
lld tmp=dy[ix][1]-gcd(ix+1,a%b,b);
if(a/b <= tmp/q)dy[ix][1]-=tmp-(a/b)*q;
return dy[ix][1];
}
return 0;
}
int main(){
int t;
for(scanf("%d",&t);t--;){
scanf("%lld%lld%lld%lld", &a, &b, &p, &q);
memset(chk,0,sizeof(chk));
printf("%lld\n", gcd(0,a,b));
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
1084 KB |
Output is correct |
2 |
Correct |
81 ms |
1084 KB |
Output is correct |
3 |
Correct |
116 ms |
1084 KB |
Output is correct |
4 |
Correct |
104 ms |
1084 KB |
Output is correct |
5 |
Correct |
91 ms |
1084 KB |
Output is correct |
6 |
Correct |
109 ms |
1084 KB |
Output is correct |
7 |
Correct |
89 ms |
1084 KB |
Output is correct |
8 |
Correct |
107 ms |
1084 KB |
Output is correct |
9 |
Correct |
111 ms |
1084 KB |
Output is correct |
10 |
Correct |
103 ms |
1084 KB |
Output is correct |
11 |
Correct |
92 ms |
1084 KB |
Output is correct |
12 |
Correct |
99 ms |
1084 KB |
Output is correct |
13 |
Correct |
111 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1084 KB |
Output is correct |
2 |
Correct |
3 ms |
1084 KB |
Output is correct |
3 |
Correct |
3 ms |
1084 KB |
Output is correct |
4 |
Correct |
2 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 |
11 |
Correct |
3 ms |
1084 KB |
Output is correct |
12 |
Correct |
2 ms |
1084 KB |
Output is correct |
13 |
Correct |
0 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 |
0 ms |
1084 KB |
Output is correct |
22 |
Correct |
3 ms |
1084 KB |
Output is correct |
23 |
Correct |
0 ms |
1084 KB |
Output is correct |
24 |
Correct |
3 ms |
1084 KB |
Output is correct |
25 |
Correct |
0 ms |
1084 KB |
Output is correct |
26 |
Correct |
0 ms |
1084 KB |
Output is correct |
27 |
Correct |
0 ms |
1084 KB |
Output is correct |
28 |
Correct |
0 ms |
1084 KB |
Output is correct |
29 |
Correct |
2 ms |
1084 KB |
Output is correct |
30 |
Correct |
0 ms |
1084 KB |
Output is correct |
31 |
Correct |
0 ms |
1084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
363 ms |
1084 KB |
Output is correct |
2 |
Correct |
324 ms |
1084 KB |
Output is correct |
3 |
Correct |
370 ms |
1084 KB |
Output is correct |
4 |
Correct |
356 ms |
1084 KB |
Output is correct |
5 |
Correct |
347 ms |
1084 KB |
Output is correct |
6 |
Correct |
338 ms |
1084 KB |
Output is correct |
7 |
Correct |
364 ms |
1084 KB |
Output is correct |
8 |
Correct |
377 ms |
1084 KB |
Output is correct |
9 |
Correct |
332 ms |
1084 KB |
Output is correct |
10 |
Correct |
354 ms |
1084 KB |
Output is correct |
11 |
Correct |
374 ms |
1084 KB |
Output is correct |
12 |
Correct |
375 ms |
1084 KB |
Output is correct |
13 |
Correct |
355 ms |
1084 KB |
Output is correct |
14 |
Incorrect |
428 ms |
1084 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |