HDU 2136 Largest prime factor

首先,不要把题意搞错,这是一道痕水的题,并不是求到第1000000个素数,而是只要处理到1000000;

并记录出不是素数的素因数,这一步在求素数的时候便可以顺便处理出来

 

                              Largest prime factor

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4923    Accepted Submission(s): 1732


Problem Description
Everybody knows any number can be combined by the prime number.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF(1) = 0.
 


 

Input
Each line will contain one integer n(0 < n < 1000000).
 


 

Output
Output the LPF(n).
 


 

Sample Input
   
   
   
   
1 2 3 4 5
 


 

Sample Output
   
   
   
   
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;
}


 

你可能感兴趣的:(HDU 2136 Largest prime factor)