#11301: 怎麼解決TLE的問題


cclemon (堅果哥)


#include<stdio.h>
#include<stdlib.h>
#define N 100000000
int main()
{
int *ptr=(int*)malloc((N+1)*sizeof(int));

int i,j;
for(i=2;i<(N+1);i++) *(ptr+i)=1;

for(i=3;i<N/2;i+=2)
for(j=2;i*j<(N+1);j++)
*(ptr+i*j)=0;

int a,b;
while(scanf("%d %d",&a,&b)!=EOF)
{
int count=0;
if(a<3) count=1;

if(a%2==0) a++;
for(i=a;i<(b+1);i+=2)
if(*(ptr+i)==1) count++;

printf("%d\n",count);
}
free(ptr);
}

這樣還是TLE,我不知道該怎麼辦了…救救我QQ

#11304: Re:怎麼解決TLE的問題


p3a_owhj (阿普二信)


 

這樣還是TLE,我不知道該怎麼辦了…救救我QQ

可以參考演算法筆記 http://www.csie.ntnu.edu.tw/~u91029/Prime.html