DuongNhi99

PNUMBER (Sàng phân đoạn)

Apr 29th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5.  
  6. int main() {
  7. //#ifdef LOCAL
  8. //    freopen("in.txt", "r", stdin);
  9. //#else
  10. //    freopen("pnumber.inp", "r", stdin);
  11. //    freopen("pnumber.out", "w", stdout);
  12. //#endif
  13.     ios_base::sync_with_stdio(false);
  14.     cin.tie(nullptr);
  15.  
  16.     int L, R; cin >> L >> R;
  17.     vector<bool> isPrime(R - L + 1, true);
  18.  
  19.     if (1 >= L) isPrime[1 - L] = false; // Xét riêng trường hợp số 1
  20.  
  21.     for (ll i = 2; i * i <= R; ++i)
  22.         for (ll j = max(i * i, (L + i - 1) / i * i); j <= R; j += i)
  23.             isPrime[j - L] = false;
  24.  
  25.     for (ll x = L; x <= R; ++x)
  26.         if (isPrime[x - L])
  27.             cout << x << '\n';
  28.  
  29.     return 0;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment