Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
2014-03-15 10:16
621 查看
/* 可以在筛选质数的同时,算出每组数据中能被各个质数整除的个数, 然后算出[0,s]的个数 [l,r] 的个数即为[0,r]的个数减去[0,l]个数。 */ #include <stdio.h> #include <iostream> #include <string.h> #define maxn 10000010 using namespace std; int prime[maxn]; int isprime[maxn]; int x[maxn]; void make_prime(){ memset(isprime, 0, sizeof(isprime)); for(int i = 2;i < maxn;i++){ if(!isprime[i]){ //prime[i] += x[i]; for(int j = i;j < maxn;j += i){ isprime[j] = 1; prime[i] += x[j]; } } } // for(int i = 0;i < 100;i++) // printf("%d ",prime[i]); for(int i = 2;i < maxn;i++) prime[i] += prime[i-1]; } int main(){ int n,m; while(~scanf("%d",&n)){ int temp = 0; memset(x, 0, sizeof(x)); memset(prime, 0, sizeof(prime)); for(int i = 0;i < n;i++){ scanf("%d",&temp); x[temp]++; } // for(int i = 0;i < 25;i++) // printf("%d ",x[i]); // printf("\n"); make_prime(); int l,r; scanf("%d",&m); while(m--){ scanf("%d%d",&l,&r);//由于x1, x2, ..., xn (2 ≤ xi ≤ 10^7) if(l > maxn) l = maxn-1;//这里的边界一定要注意,WA了好几次才发现 if(r > maxn) r = maxn-1; printf("%d\n",prime[r]-prime[l-1]); } } return 0; }
相关文章推荐
- Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)
- Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers(素数筛法)
- Codeforces Round #226 (Div. 2) D. Bear and Floodlight
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #356 (Div. 2)D. Bear and Tower of Cubes
- Codeforces Round #257 (Div. 1) D. Jzzhu and Numbers
- Codeforces Round #215 (Div. 1) C. Sereja and the Arrangement of Numbers(欧拉图)
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
- Codeforces Round #195 (Div. 2) A. Vasily the Bear and Triangle
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力
- Codeforces Round #160 (Div. 2) A. Roma and Lucky Numbers
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #232 (Div. 2) B. On Corruption and Numbers
- Codeforces Round #356 (Div. 2) B. Bear and Finding Criminal 水题
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers