fork(4) download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <string>
  9. #include <queue>
  10. #include <stack>
  11. #include <algorithm>
  12. #include <iomanip>
  13. #define dibs reserve
  14. #define OVER9000 1234567890
  15. #define patkan 9
  16. #define tisic 47
  17. #define soclose 10e-7
  18. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  19. #define chocolate win
  20. #define ff first
  21. #define ss second
  22. #define abs(x) ((x < 0)?-(x):(x))
  23. // mylittlepony
  24. using namespace std;
  25.  
  26. long long gcd(long long x, long long y) {
  27. if(x > y) swap(x,y);
  28. if(x == 0) return y;
  29. return gcd(y%x,x);}
  30.  
  31. struct frac {
  32. long long n,d;
  33. frac() {}
  34. frac(long long a, long long b) {n =a, d =b;}
  35.  
  36. bool operator<(const frac &A) const {
  37. return n*A.d < d*A.n;}
  38. };
  39.  
  40. int main() {
  41. // freopen("zanzibar.in","r",stdin);
  42. int T;
  43. scanf(" %d",&T);
  44.  
  45. set<frac> P;
  46. queue<frac> q;
  47. P.insert(frac(0,1));
  48. q.push(frac(0,1));
  49.  
  50. while(!q.empty()) {
  51. frac p =q.front();
  52. for(int a =1; a < 13; a++) for(int b =a+1; b <= 13; b++) {
  53. long long x =p.n*b+p.d*a;
  54. long long y =p.d*b;
  55. long long d =gcd(x,y);
  56. x /=d, y /=d;
  57. if(x >= y) continue;
  58. if(P.find(frac(x,y)) == P.end()) q.push(frac(x,y));
  59. P.insert(frac(x,y));}
  60. q.pop();}
  61.  
  62. for(int t =0; t < T; t++) {
  63. long long a,b;
  64. scanf(" %lld %lld",&a,&b);
  65. frac fbest(a,b);
  66.  
  67. if(P.lower_bound(frac(a,b)) != P.end()) {
  68. frac f =*P.lower_bound(frac(a,b));
  69. frac fdif(abs(f.n*b-a*f.d),b*f.d);
  70. long long d =gcd(fdif.n,fdif.d);
  71. fdif.n /=d, fdif.d /=d;
  72. if(fdif < fbest) fbest =fdif;}
  73.  
  74. if(a > 0) {
  75. frac f =*(--P.lower_bound(frac(a,b)));
  76. frac fdif(abs(f.n*b-a*f.d),b*f.d);
  77. long long d =gcd(fdif.n,fdif.d);
  78. fdif.n /=d, fdif.d /=d;
  79. if(fdif < fbest) fbest =fdif;}
  80.  
  81. long long d =gcd(fbest.n,fbest.d);
  82. fbest.n /=d, fbest.d /=d;
  83. printf("Case %d: %lld/%lld\n",t+1,fbest.n,fbest.d);}
  84. return 0;}
  85.  
  86. // look at my code
  87. // my code is amazing
Success #stdin #stdout 0.21s 3748KB
stdin
1
14951 20020
stdout
Case 1: 1/360360