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 69 70 71
| #include<iostream> #include<sstream> #include<map> #include<cmath> #include<fstream> #include<queue> #include<vector> #include<sstream> #include<cstring> #include<cstdio> #include<stack> #include<bitset> #include<ctime> #include<string> #include<iomanip> #include<algorithm> using namespace std ; #define INT long long int const int INF = 0x3f3f3f ; const double esp = 0.0000000001 ; const double PI = acos(-1.0) ; const int mod = 1000000007 ; const int MY = 100 + 5 ; const int MX = 10000000 + 5 ; int Max ,n ,m ; bool isprime[MX] ; int sum[MX] ,num[MX] ; void init() // 筛法同时记录个数 { memset(isprime ,false ,sizeof(isprime)) ; memset(sum ,0 ,sizeof(sum)) ; for(int i = 2 ;i <= Max ; ++i) { sum[i] += sum[i-1] ; if(!isprime[i]) { sum[i] += num[i] ; for(int j = i + i ;j <= Max ; j += i) { sum[i] += num[j] ; isprime[j] = true ; } } } } int main() { int x ; while(~scanf("%d" ,&n)) { memset(num ,0 ,sizeof(num)) ; Max = 0 ; for(int i = 0 ;i < n ; ++i) { scanf("%d" ,&x) ; num[x]++ ; // 记录个数 Max = max(Max ,x) ; } init() ; scanf("%d" ,&m) ; int le ,rt ; for(int i = 0 ;i < m ; ++i) { scanf("%d%d" ,&le ,&rt) ; if(rt > Max) rt = Max ; if(le > Max) cout<<"0"<<endl ; else cout<<sum[rt]-sum[le-1]<<endl ; } } return 0 ; }
|