HDU 2136 Largest prime factor

来源:互联网 发布:和明星合影软件 编辑:程序博客网 时间:2024/04/30 23:36

首先,不要把题意搞错,这是一道痕水的题,并不是求到第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
12345
 


 

Sample Output
01213

 

 

#include <iostream>#include <math.h>#include <stdlib.h>#include <string.h>using namespace std;#define MAX 1000005int 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;}


 

原创粉丝点击