1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; #define pb push_back #define mp make_pair #define fi first #define se second const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; inline ll addmod(ll a, ll b) { return (a + b) % MOD; } inline ll submod(ll a, ll b) { return (a - b + MOD) % MOD; } inline ll mulmod(ll a, ll b) { return a * b % MOD; }
void Eratosthenes(int n,vector<int> &primes,vector<bool> &isPrime) { isPrime[0]=isPrime[1]=false; for (int i=2; i<n; i++) isPrime[i] = true; for(int i=2;i*i<=n;i++) { if(isPrime[i]) { primes.push_back(i); for(int j=i*i;j<=n;j+=i) { isPrime[j]=false; } } } } void solve() { vector<int> primes(1e6+1); vector<bool> isPrime(1e6+1); Eratosthenes(1e6,primes,isPrime); vector<int> qzh(1e6+1,0); for(int i=1;i<=1e6;i++) { qzh[i]=qzh[i-1]+(isPrime[i]==true?1:0); } int T; cin>>T; while(T--) { int l,r; cin>>l>>r; cout<<qzh[r]-qzh[l]; }
}
int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t=1; while(t--) { solve(); } return 0; }
|