Advertisement
Manioc

Crivo

Oct 25th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. vector <int> primes;
  5. bitset<1000005> bs;
  6. long long tamanho;
  7.  
  8. void sieve(long long num) {
  9.     tamanho = num + 1;
  10.     bs.set();                                                
  11.     bs[0] = bs[1] = 0;                                    
  12.     for (long long i = 2; i <= tamanho; i++) if (bs[i]) {
  13.         for (long long j = i * i; j <= tamanho; j += i) bs[j] = 0;
  14.         primes.push_back((int)i);
  15.     }
  16. }
  17.  
  18.  
  19. int main(){
  20.     sieve(1000010);
  21.     int num;
  22.     cin >> num;
  23.     while(num--){
  24.         int x, y;
  25.         cin >> x >> y;
  26.         int i = 1, cont = 0;
  27.         while (y >= primes[i]){
  28.             if (primes[i] >= x){
  29.                 if (primes[i]-primes[i-1] == 2) cont++;
  30.                 else if (primes[i+1]-primes[i] == 2)cont++;
  31.             }
  32.             i++;
  33.         }
  34.         cout << cont << endl;
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement