Ankit_132

C

Nov 29th, 2023
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8.  
  9. int main()
  10. {
  11.     int N = 1e6 + 1;
  12.     vector<int> isPrime(N, 1);
  13.  
  14.     isPrime[1] = 0;
  15.  
  16.     for(int i=2; i<N; i++)
  17.     {
  18.         if(!isPrime[i])     continue;
  19.  
  20.         for(int j=2*i; j<N; j+=i)
  21.             isPrime[j] = 0;
  22.     }
  23.  
  24.     _test
  25.     {
  26.         int n;
  27.         cin>>n;
  28.  
  29.         int curr = 1;
  30.         int ans = 0;
  31.  
  32.         while(n > 0)
  33.         {
  34.             ans++;
  35.             if(isPrime[n])
  36.             {
  37.                 n = 0;
  38.                 break;
  39.             }
  40.  
  41.             n -= curr;
  42.             curr *= 2;
  43.         }
  44.  
  45.         if(n == 0)      cout<<ans<<"\n";
  46.         else            cout<<-1<<"\n";
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment