素数筛法

来源:互联网 发布:node什么意思 编辑:程序博客网 时间:2024/05/16 09:20
         
J - Largest prime factor
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 2136

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<stdio.h>#include<math.h>#include<string.h>#include<algorithm>#include<map>using namespace std;int a[1000001]={0};map<int,int>m;void prime1(){  int p=1,i,j;   for(i=2;i<=1000000;i++)//从2开始,知道2是素数 {if(a[i]==0)//如果是素数 {for(j=i;j<=1000000;j+=i)//从 j=i开始,因为此题需要不断更新数据,更节省时间的方法直接从i*i开始 {a[j]=p;//标记把此素数的 倍数 }p++;}}}int main(){    prime1();int n,flag,i,j;while(~scanf("%d",&n)){  if(n==1)  printf("0\n");  else  {  printf("%d\n",a[n]);}}}