CodeForces 385C Bear and Prime Numbers :给定n个数和m个询问,对于每个询问n个数里面是l-r中质数倍数和(不重复) :筛法的技巧
题意和范围:http://codeforces.com/problemset/problem/385/C
好题呀==
在筛质数的过程中顺带累加筛他的质数vis值
最后统计就是前缀和相减==
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int vis[20000005],f[20000005]; 6 int main() 7 { 8 int n,i,j,x,l,r,m; 9 scanf("%d",&n); 10 memset(f,0,sizeof(f)); 11 memset(vis,0,sizeof(vis)); 12 for (i=1;i<=n;i++) 13 { 14 scanf("%d",&x); 15 f[x]++; 16 } 17 for (i=2;i<=20000000;i++) 18 if (vis[i]==0) 19 { 20 vis[i]+=f[i]; 21 for (j=2;j*i<=20000000;j++) 22 { 23 vis[i*j]=-1; 24 vis[i]+=f[i*j]; 25 } 26 } 27 f[0]=0; 28 for (i=1;i<=20000000;i++) 29 if (vis[i]!=-1) f[i]=f[i-1]+vis[i]; 30 else f[i]=f[i-1]; 31 scanf("%d",&m); 32 while (m--) 33 { 34 scanf("%d%d",&l,&r); 35 l--; 36 l=min(l,20000000); 37 r=min(r,20000000); 38 printf("%d\n",f[r]-f[l]); 39 } 40 return 0; 41 }
题目链接:见上