#include <stdio.h>
#include <algorithm>
#include <math.h>
#define INF 0x7fffffffffffffff
using namespace std;
int T, Cnt;
long long A, B, P, Q, D[1001][2];
double LX, LY, LQ, LI=log(INF);
void gcd(long long X, long long Y) {
if(!Y) {
printf("%lld\n",min(D[Cnt][0],D[Cnt][1]));
return;
}
LX=log(X);
LY=log(Y);
Cnt++;
if(LX-LY+LQ>LI || D[Cnt-1][1]+X/Y*Q<0)
D[Cnt][0]=D[Cnt-1][0]+P;
else
D[Cnt][0]=min(D[Cnt-1][0]+P,D[Cnt-1][1]+X/Y*Q);
if(LX-LY+LQ>LI || D[Cnt-1][0]+X/Y*Q<0)
D[Cnt][1]=INF;
else
D[Cnt][1]=D[Cnt-1][0]+X/Y*Q;
gcd(Y,X%Y);
}
int main(void) {
int i;
scanf("%d",&T);
for(i=1 ; i<=T ; i++) {
scanf("%lld %lld %lld %lld",&A,&B,&P,&Q);
LQ=log(Q);
Cnt=1;
if(A>B) {
D[Cnt][0]=0;
D[Cnt][1]=INF;
}
else if(A<B) {
D[Cnt][0]=P;
D[Cnt][1]=0;
}
else
D[Cnt][0]=D[Cnt][1]=0;
if(A>B)
gcd(A,B);
else
gcd(B,A);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1208 KB |
Output is correct |
2 |
Correct |
0 ms |
1208 KB |
Output is correct |
3 |
Correct |
0 ms |
1208 KB |
Output is correct |
4 |
Correct |
0 ms |
1208 KB |
Output is correct |
5 |
Correct |
0 ms |
1208 KB |
Output is correct |
6 |
Correct |
0 ms |
1208 KB |
Output is correct |
7 |
Correct |
0 ms |
1208 KB |
Output is correct |
8 |
Correct |
0 ms |
1208 KB |
Output is correct |
9 |
Correct |
0 ms |
1208 KB |
Output is correct |
10 |
Correct |
0 ms |
1208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
1208 KB |
Output is correct |
2 |
Correct |
152 ms |
1208 KB |
Output is correct |
3 |
Correct |
153 ms |
1208 KB |
Output is correct |
4 |
Correct |
162 ms |
1208 KB |
Output is correct |
5 |
Correct |
146 ms |
1208 KB |
Output is correct |
6 |
Correct |
142 ms |
1208 KB |
Output is correct |
7 |
Correct |
149 ms |
1208 KB |
Output is correct |
8 |
Correct |
151 ms |
1208 KB |
Output is correct |
9 |
Correct |
144 ms |
1208 KB |
Output is correct |
10 |
Correct |
142 ms |
1208 KB |
Output is correct |
11 |
Correct |
147 ms |
1208 KB |
Output is correct |
12 |
Correct |
146 ms |
1208 KB |
Output is correct |
13 |
Correct |
146 ms |
1208 KB |
Output is correct |
14 |
Correct |
0 ms |
1208 KB |
Output is correct |
15 |
Correct |
1 ms |
1208 KB |
Output is correct |
16 |
Correct |
0 ms |
1208 KB |
Output is correct |
17 |
Correct |
0 ms |
1208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1208 KB |
Output is correct |
2 |
Correct |
2 ms |
1208 KB |
Output is correct |
3 |
Correct |
3 ms |
1208 KB |
Output is correct |
4 |
Correct |
3 ms |
1208 KB |
Output is correct |
5 |
Correct |
4 ms |
1208 KB |
Output is correct |
6 |
Correct |
0 ms |
1208 KB |
Output is correct |
7 |
Correct |
3 ms |
1208 KB |
Output is correct |
8 |
Correct |
3 ms |
1208 KB |
Output is correct |
9 |
Correct |
3 ms |
1208 KB |
Output is correct |
10 |
Correct |
2 ms |
1208 KB |
Output is correct |
11 |
Correct |
3 ms |
1208 KB |
Output is correct |
12 |
Correct |
3 ms |
1208 KB |
Output is correct |
13 |
Correct |
0 ms |
1208 KB |
Output is correct |
14 |
Correct |
2 ms |
1208 KB |
Output is correct |
15 |
Correct |
0 ms |
1208 KB |
Output is correct |
16 |
Correct |
0 ms |
1208 KB |
Output is correct |
17 |
Correct |
2 ms |
1208 KB |
Output is correct |
18 |
Correct |
2 ms |
1208 KB |
Output is correct |
19 |
Correct |
3 ms |
1208 KB |
Output is correct |
20 |
Correct |
2 ms |
1208 KB |
Output is correct |
21 |
Correct |
3 ms |
1208 KB |
Output is correct |
22 |
Correct |
4 ms |
1208 KB |
Output is correct |
23 |
Correct |
4 ms |
1208 KB |
Output is correct |
24 |
Correct |
4 ms |
1208 KB |
Output is correct |
25 |
Correct |
4 ms |
1208 KB |
Output is correct |
26 |
Correct |
0 ms |
1208 KB |
Output is correct |
27 |
Correct |
4 ms |
1208 KB |
Output is correct |
28 |
Correct |
5 ms |
1208 KB |
Output is correct |
29 |
Correct |
4 ms |
1208 KB |
Output is correct |
30 |
Correct |
3 ms |
1208 KB |
Output is correct |
31 |
Correct |
3 ms |
1208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
517 ms |
1208 KB |
Output is correct |
2 |
Correct |
515 ms |
1208 KB |
Output is correct |
3 |
Correct |
518 ms |
1208 KB |
Output is correct |
4 |
Correct |
544 ms |
1208 KB |
Output is correct |
5 |
Correct |
514 ms |
1208 KB |
Output is correct |
6 |
Correct |
526 ms |
1208 KB |
Output is correct |
7 |
Correct |
526 ms |
1208 KB |
Output is correct |
8 |
Correct |
501 ms |
1208 KB |
Output is correct |
9 |
Correct |
523 ms |
1208 KB |
Output is correct |
10 |
Correct |
528 ms |
1208 KB |
Output is correct |
11 |
Correct |
546 ms |
1208 KB |
Output is correct |
12 |
Correct |
520 ms |
1208 KB |
Output is correct |
13 |
Correct |
546 ms |
1208 KB |
Output is correct |
14 |
Correct |
593 ms |
1208 KB |
Output is correct |
15 |
Correct |
591 ms |
1208 KB |
Output is correct |
16 |
Correct |
578 ms |
1208 KB |
Output is correct |
17 |
Correct |
570 ms |
1208 KB |
Output is correct |
18 |
Correct |
590 ms |
1208 KB |
Output is correct |
19 |
Correct |
106 ms |
1208 KB |
Output is correct |
20 |
Correct |
136 ms |
1208 KB |
Output is correct |
21 |
Correct |
194 ms |
1208 KB |
Output is correct |
22 |
Correct |
189 ms |
1208 KB |
Output is correct |
23 |
Correct |
238 ms |
1208 KB |
Output is correct |
24 |
Correct |
240 ms |
1208 KB |
Output is correct |
25 |
Correct |
244 ms |
1208 KB |
Output is correct |
26 |
Correct |
241 ms |
1208 KB |
Output is correct |
27 |
Correct |
261 ms |
1208 KB |
Output is correct |
28 |
Correct |
608 ms |
1208 KB |
Output is correct |
29 |
Correct |
569 ms |
1208 KB |
Output is correct |
30 |
Correct |
606 ms |
1208 KB |
Output is correct |
31 |
Correct |
551 ms |
1208 KB |
Output is correct |
32 |
Correct |
598 ms |
1208 KB |
Output is correct |
33 |
Correct |
589 ms |
1208 KB |
Output is correct |
34 |
Correct |
586 ms |
1208 KB |
Output is correct |
35 |
Correct |
625 ms |
1208 KB |
Output is correct |