DuongNhi99

PRIMECOUNT

Mar 4th, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int64_t long long
  3. using namespace std;
  4.  
  5. const int N = 1050;
  6.  
  7. int64_t segmentedSieveNoPreGen(int64_t L, int64_t R) {
  8.     vector<bool> isPrime(R - L + 1, true);
  9.     int64_t lim = sqrt(R);
  10.     int64_t ans = 0;
  11.     for(int64_t i = 2; i <= lim; ++i)
  12.         for(int64_t j = max(i * i, (L + i - 1) / i * i); j <= R; j += i)
  13.             if(isPrime[j - L])
  14.                 isPrime[j - L] = false,
  15.                 ans++;
  16.  
  17.     if(L == 1)
  18.         isPrime[0] = false,
  19.         ans++;
  20.  
  21.     ans = (R - L + 1) - ans;
  22.     return ans;
  23. }
  24.  
  25. int t;
  26. int64_t L, R;
  27.  
  28. int main() {
  29. #ifdef LOCAL
  30.     freopen("in.txt", "r", stdin);
  31. #else
  32.     freopen("PRIMECOUNT.inp", "r", stdin);
  33.     freopen("PRIMECOUNT.out", "w", stdout);
  34. #endif
  35.     ios_base::sync_with_stdio(false);
  36.     cin.tie(NULL); cout.tie(NULL);
  37.  
  38.     cin >> t;
  39.     while(t--) {
  40.         cin >> L >> R;
  41.         cout << segmentedSieveNoPreGen(L, R) << '\n';
  42.     }
  43.  
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment