tuki2501

findy.cpp

Apr 4th, 2021
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5. #define int long long
  6. #define sqr(x) ((x)*(x))
  7.  
  8. const int N = 1000005;
  9.  
  10. int prime[N];
  11. vector<int> prim;
  12.  
  13. void Init() {
  14.   prime[0] = prime[1] = 1;
  15.   for (int i = 2; i < N; i++) if (!prime[i]) {
  16.     prim.push_back(i);
  17.     for (int j = i * 2; j < N; j += i) prime[j] = 1;
  18.   }
  19. }
  20.  
  21. int check(int n) {
  22.   if (n < 2) return 0;
  23.   int i = 0;
  24.   for (int i = 0; i < prim.size() && n > 1; i++) {
  25.     int cnt = 0;
  26.     while (n % prim[i] == 0) {
  27.       cnt++;
  28.       n /= prim[i];
  29.       if (cnt > 1) return 0;
  30.     }
  31.   }
  32.   return 1;
  33. }
  34.  
  35. void Main() {
  36.   int n; cin >> n;
  37.   int t = max((int)sqrt(n), 2ll);
  38.   int l = t, r = t + 1;
  39.   while (!check(l)) l--;
  40.   while (!check(r)) r++;
  41.   cout << min(abs(n - sqr(l)), abs(n - sqr(r))) << endl;
  42. }
  43.  
  44. signed main() {
  45.   cin.tie(0)->sync_with_stdio(0);
  46.   Init();
  47.   int T; cin >> T;
  48.   while (T--) Main();
  49. }
Advertisement
Add Comment
Please, Sign In to add comment