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 | #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<cstring> #include<stack> #include<vector> #include<typeinfo> using namespace std; int n, m; bool isprime[10000005]; int prime[10000005]; vector<int> primeList; inline void genPrime() { memset(isprime, true, sizeof(isprime)); isprime[0] = isprime[1] = false; for (int i = 2; i <= 10000000; i++) { if (!isprime[i])continue; primeList.push_back(i); for (int j = 2 * i; j <= 10000000; j += i) { isprime[j] = false; } } } inline void putIn(int v) { for (int i = 0; i < primeList.size() && v >= primeList[i] && v != 1; i++) { if (v%primeList[i] == 0) { prime[primeList[i]]++; while (v%primeList[i] == 0)v /= primeList[i]; } if (isprime[v]) { prime[v]++; break; } } } int main() { genPrime(); scanf("%d", &n); for (int i = 1; i <= n; i++) { int v; scanf("%d", &v); putIn(v); } for (int i = 1; i <= 10000000; i++) { prime[i] += prime[i - 1]; } scanf("%d", &m); for (int i = 1; i <= m; i++) { int l, r; scanf("%d%d", &l, &r); if (r > 10000000)r = 10000000; if (l > 10000000)l = 10000000; printf("%d\n", prime[r] - prime[l - 1]); } } |
http://bjutacm.openjudge.cn/lianxi/181A/