Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
1000个数为一个桶,就快了#include<stdio.h> #include<string.h> bool n[100001]; int can[101]; void find_primes() { memset(n,true,sizeof(n)); int i,j; n[0] = false; n[1] = false; for(i = 2; i < 350; i++) { if(n[i]) { for(j = i*i; j < 100000; j+=i) { n[j] = false;// enter here 19W times } } } int start, end; memset(can,0,sizeof(can)); for(i = 0; i < 100; i++)//0~999,1000~1999... { int start = i*1000; int end = start + 1000; for(j = start; j < end; j++) { if(n[j])can[i]++; } } } int main() { find_primes(); int i,j; int a,b; int re; //for(i=0;i<1000;i++)if(n[i])printf("%d ",i); printf("\n"); while(scanf("%d%d",&a,&b)) { if(-1 == a && -1 == b)break; re = 0; for(i=a; i<=b; ) { if( i < 2 ) { i++; } else if( 0 == i % 1000 && i+1000 <= b ) { re += can[i/1000]; i+=1000; } else { if(n[i])re++; i++; } } printf("%d\n",re); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator