Advertisement
Guest User

Sherlock and Physics

a guest
Jun 23rd, 2015
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <algorithm>
  2. #include <string>
  3. #include <vector>
  4. #include <queue>
  5. #include <iostream>
  6. #include <cmath>
  7. #include <sstream>
  8. #include <map>
  9. #include <set>
  10. #include <numeric>
  11. #include <memory.h>
  12. #include <cstdio>
  13. #include <assert.h>
  14.  
  15. using namespace std;
  16.  
  17. #define pb push_back
  18. #define INF 1011111111
  19. #define FOR(i, a, b) for (int _n(b), i(a); i < _n; i++)
  20. #define rep(i, n) FOR(i, 0, n)
  21. #define CL(a, v) memset((a), (v), sizeof(a))
  22. #define mp make_pair
  23. #define X first
  24. #define Y second
  25. #define all(c) (c).begin(), (c).end()
  26. #define SORT(c) sort(all(c))
  27.  
  28. typedef long long ll;
  29. typedef vector<int> VI;
  30. typedef pair<int, int> pii;
  31.  
  32. /*** TEMPLATE CODE ENDS HERE */
  33.  
  34. ll gcd(ll a, ll b) { return a == 0 ? b : gcd(b % a, a); }
  35.  
  36. void solve() {
  37.   ll R, S;
  38.   cin >> R >> S;
  39.  
  40.   // R >= S
  41.   ll T = 0;
  42.  
  43.   if (R >= S) {
  44.     T = ll(R / S) * S;
  45.     R %= S;
  46.   }
  47.  
  48.   if (4 * R > S) {
  49.     cout << T + S << " " << 0 << "/" << 1 << endl;
  50.     return;
  51.   }
  52.  
  53.   T += R;
  54.  
  55.   R = R * R;
  56.   ll g = gcd(R, S);
  57.  
  58.   R /= g;
  59.   S /= g;
  60.  
  61.   if (R == 0) S = 1;
  62.  
  63.   if (R) S *= 2;
  64.  
  65.   cout << T << " " << R << "/" << S << endl;
  66. }
  67.  
  68. int main() {
  69. #ifdef LOCAL_HOST
  70.   freopen("input.txt", "r", stdin);
  71. // freopen("output.txt","w",stdout);
  72. #endif
  73.  
  74.   ios_base::sync_with_stdio(false);
  75.  
  76.   int T;
  77.   cin >> T;
  78.  
  79.   while (T--) {
  80.     solve();
  81.   }
  82.  
  83. #ifdef LOCAL_HOST
  84.   printf("TIME: %.3lf\n", double(clock()) / CLOCKS_PER_SEC);
  85. #endif
  86.  
  87.   return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement