#include<stdio.h>
unsigned long long int find_r(unsigned long long int x, unsigned long long int a, unsigned long long int b)
{
unsigned long long int l,r,mid;
l=1; r=b;
while(l<r)
{
mid = (l+r)/2;
if((x + a*mid)/b < mid) r = mid;
else l=mid+1;
}
return l-1;
}
unsigned long long int gcd(unsigned long long int p, unsigned long long int q)
{
while(p%q){
unsigned long long int t=p%q;
p=q;
q=t;
}
return q;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
unsigned long long int a,b,c,d;
unsigned long long int k,s;
scanf("%llu %llu %llu %llu",&a,&b,&c,&d);
unsigned long long int x,y;
unsigned long long int p,q,r;
unsigned long long int g;
g = gcd(a,b);
a/=g;
b/=g;
g = gcd(c,d);
c/=g;
d/=g;
k = a/b;
if((c-1)/d > k)
{
printf("%llu 1\n",k+1);
}
else
{
a %= b;
c %= d;
if(c==0)c=d;
y=1;
while(1)
{
p=d+(a*y/b)*d;
q=c*y;
if(p<q)break;
r = a*y%b;
s = find_r(r,a,b);
// printf("%llu %llu %llu %llu %llu %llu %llu\n",y, p,q,p-q,r,s, (p+(d-c)*s-q)/c);
y += s+(p+(d-c)*s-q)/c+1;
}
x = a*y/b+1+k*y;
printf("%llu %llu\n",x,y);
}
}
}
Compilation message
fractions.cpp: In function 'int main()':
fractions.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&t);
~~~~~^~~~~~~~~
fractions.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%llu %llu %llu %llu",&a,&b,&c,&d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
252 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
34 ms |
1016 KB |
Output is correct |
18 |
Correct |
34 ms |
1016 KB |
Output is correct |
19 |
Correct |
34 ms |
1016 KB |
Output is correct |
20 |
Correct |
34 ms |
1144 KB |
Output is correct |
21 |
Correct |
35 ms |
1016 KB |
Output is correct |
22 |
Correct |
34 ms |
1140 KB |
Output is correct |
23 |
Correct |
34 ms |
1016 KB |
Output is correct |
24 |
Correct |
34 ms |
1016 KB |
Output is correct |
25 |
Correct |
4 ms |
376 KB |
Output is correct |
26 |
Correct |
54 ms |
2808 KB |
Output is correct |
27 |
Correct |
55 ms |
2808 KB |
Output is correct |
28 |
Correct |
57 ms |
2808 KB |
Output is correct |
29 |
Correct |
56 ms |
2780 KB |
Output is correct |
30 |
Correct |
56 ms |
2808 KB |
Output is correct |
31 |
Correct |
53 ms |
2808 KB |
Output is correct |
32 |
Correct |
57 ms |
2936 KB |
Output is correct |
33 |
Correct |
61 ms |
3064 KB |
Output is correct |
34 |
Correct |
8 ms |
508 KB |
Output is correct |
35 |
Correct |
81 ms |
2424 KB |
Output is correct |
36 |
Correct |
81 ms |
2508 KB |
Output is correct |
37 |
Correct |
81 ms |
2552 KB |
Output is correct |
38 |
Correct |
81 ms |
2424 KB |
Output is correct |
39 |
Correct |
81 ms |
2424 KB |
Output is correct |
40 |
Correct |
81 ms |
2412 KB |
Output is correct |
41 |
Correct |
81 ms |
2552 KB |
Output is correct |
42 |
Correct |
80 ms |
2424 KB |
Output is correct |
43 |
Correct |
82 ms |
2424 KB |
Output is correct |
44 |
Correct |
81 ms |
2580 KB |
Output is correct |
45 |
Correct |
81 ms |
2424 KB |
Output is correct |
46 |
Correct |
81 ms |
2424 KB |
Output is correct |
47 |
Correct |
81 ms |
2552 KB |
Output is correct |
48 |
Correct |
81 ms |
2364 KB |
Output is correct |
49 |
Correct |
81 ms |
2424 KB |
Output is correct |
50 |
Correct |
81 ms |
2424 KB |
Output is correct |