Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 376 Accepted Submission(s): 151
1. 0 < C < D < B, and
2. the error |A/B - C/D| is the minimum over all possible values of C and D, and
3. D is the smallest such positive integer.
1. B is a 32 bit integer strictly greater than 2, and
2. 0 < A < B
#include<stdio.h> #include<string.h> long long gcd1(long long a,long long b,long long &x,long long &y) { if(b == 0) { x = 1; y = 0; return a; } long long d = gcd1(b,a%b,x,y); long long t = x; x = y; y = t - a/b*y; return d; } int main() { int t; scanf("%d",&t); while(t--) { long long a,b; scanf("%lld/%lld",&a,&b); long long x = 0,y = 0; long long p = gcd1(a,b,x,y); //printf("%lld,%lld\n",x,y); //printf("---%lld\n",p); //printf("==%lld %lld\n",a,b); if(p != 1) { printf("%lld/%lld\n",a/p,b/p); continue; } if(a == 1) { printf("1/%lld\n",b-1); continue; } long long x1 = 0,y1 = 0; if(x > 0) { x1 = (a + y)%a; y1 = (b - x)%b; } else { x1 = (a - y)%a; y1 = (b + x)%b; } //printf("%lld %lld %lld %lld\n",x1,y1); printf("%lld/%lld\n",x1,y1); } return 0; }