Niloy007

G. Pairs Forming GCD

May 14th, 2021
584
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #define Niloy
  3. #define int int64_t
  4. #define mx (int) 1e7 + 123
  5. #define MOD (int) 1e9 + 7
  6. #define pb push_back
  7. #define pairs pair<int, int>
  8. #define vi vector<int>
  9. #define vb vector<bool>
  10. #define vii vector<pairs>
  11. #define lb lower_bound
  12. #define ub upper_bound
  13. #define endl '\n'
  14. #define llu unsigned long long
  15. using namespace std;
  16. /* ----------------------------------------------------------------------------------- */
  17.  
  18. // Input/Output
  19. #define fastInput ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  20. #define all(x) x.begin(), x.end()
  21. #define square(a) (a * a)
  22. #define mem(a, b) memset(a, b, sizeof(a))
  23.  
  24. // Fractional Number
  25. #define fraction()        cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed, ios::floatfield);
  26.  
  27. // Direction Array
  28. int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
  29. int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
  30.  
  31. // File I/O
  32. #define read(x)  freopen(x, "r", stdin);
  33. #define write(x) freopen(x, "w", stdout);
  34.  
  35. // Loops
  36. #define rep(i, a, n) for (int i = a; i < n; i++)
  37. #define REP(i, a, n) for (int i = a; i <= n; i++)
  38. #define rev(i, n, a) for (int i = n - 1; i >= a; i--)
  39. #define REV(i, n, a) for (int i = n; i >= a; i--)
  40.  
  41. /* ----------------------------------------------------------------------------------- */
  42.  
  43. #define Cases  cout << "Case " << ++Case << ": ";
  44. #define __test int tt; int Case=0; cin >> tt; while(tt--)
  45. #define showTime cerr << "time = " << (clock() / CLOCKS_PER_SEC) << " sec" << '\n';
  46.  
  47. #define dbgA2(A, n, m) {cout<<"--> "<<#A<<" = \n";rep(i, 0, n){rep(j, 0, m){cout<<A[i][j]<<"";}cout<<"\n";}cout<<"\n";}
  48. #define dbgA(A, n) {cout<<" --> "<<#A<<" = (";rep(i, 0, n)cout<<A[i]<<" ";cout<<")\n";}
  49. #define dbg(args...) {string sss(#args);sss+=',';cout<<" --> ";debugger::call(all(sss), args);cout<<"\n";}
  50.  
  51. /* ----------------------------------------------------------------------------------- */
  52.  
  53. int gcd(int n, int m) { return m ? gcd(m, n % m) : n; }
  54. int lcm(int n, int m) { return n / gcd(n, m) * m; }
  55.  
  56. struct debugger {
  57.     typedef string::iterator si;
  58.     static void call(si it, si ed) {}
  59.     template<typename T, typename ... aT>
  60.     static void call(si it, si ed, T a, aT... rest) {
  61.         string b;
  62.         for(; *it!=','; ++it)
  63.             if(*it!=' ')
  64.                 b+=*it;
  65.         cout << b << "=" << a << " ";
  66.         call(++it, ed, rest...);
  67.     }
  68. };
  69.  
  70. /* ----------------------------------------------------------------------------------- */
  71. void input() {
  72. #ifdef Niloy
  73.     read("input.txt");  
  74.     write("output.txt");
  75. #endif
  76. }
  77.  
  78. /* ----------------------------------------------------------------------------------- */
  79.  
  80. int phi[mx];
  81.  
  82. void generatePhi(int n) {
  83.     REP(i, 1, n) {
  84.         phi[i] = i;
  85.     }
  86.  
  87.     REP(i, 2, n) {
  88.         if (phi[i] == i) {
  89.             for (int j = i; j <= n; j += i) {
  90.                 phi[j] *= (i - 1);
  91.                 phi[j] /= i;
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. void solve() {
  98.     int n, p;
  99.     cin >> n >> p;
  100.     int x = -1, l = 1, r = n;
  101.     while (l <= r) {
  102.         int mid = (l + r) >> 1;
  103.         if (phi[mid] >= p) {
  104.             x = mid;
  105.             r = mid - 1;
  106.         } else {
  107.             l = mid + 1;
  108.         }
  109.     }
  110.  
  111.     int ans = -1;
  112.     if (x != -1) {
  113.         ans = n / x;
  114.     }
  115.     cout << ans << endl;
  116. }
  117.  
  118. int32_t main() {
  119.     fastInput;
  120.     // solve();
  121.     int lim = 1e7;
  122.     generatePhi(lim);
  123.  
  124.     REP(i, 1, lim) {
  125.         phi[i] += phi[i - 1];
  126.     }
  127.  
  128.     __test {
  129.         Cases;
  130.         solve();
  131.     }
  132.  
  133.     return 0;
  134. }
RAW Paste Data