首先,不要把题意搞错,这是一道痕水的题,并不是求到第1000000个素数,而是只要处理到1000000;
并记录出不是素数的素因数,这一步在求素数的时候便可以顺便处理出来
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4923 Accepted Submission(s): 1732
1 2 3 4 5
0 1 2 1 3
#include <iostream> #include <math.h> #include <stdlib.h> #include <string.h> using namespace std; #define MAX 1000005 int a[MAX]={}; int n,m; void solve( ) { for( int i=0 ; i<MAX ; i++ ) a[i] = -1; int num = 0; for( int i=2 ; i<=MAX ; i++ ){ if( a[i] == -1 ){ num++; for( int j=i ; j<=MAX ; j += i ) a[ j ] = num; } } } int main() { solve( ); while( scanf("%d",&n) == 1 ){ if( n == 1 ) printf("0\n"); else printf("%d\n",a[n]); } return 0; }