现在的位置: 首页 > 综合 > 正文

HDU2136 Largest prime factor 筛选法求素数位置

2018年01月20日 ⁄ 综合 ⁄ 共 871字 ⁄ 字号 评论关闭

Largest prime factor

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

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
/*
2136 Largest prime factor
给定一个数 求他的最大质因数 并求出它在素数表的位置 
*/
#include<iostream>
#include<cstring>
using namespace std;
#define max 1000001 
int a[max];
int main(){
    int i,k,n,j;
    for(i=2;i<max;i++)
        a[i]=-1;
    a[1]=0;
    k=0;//这里的k表示的是质因子的位置 
    for(i=2;i<max;i++)
    {
        if(a[i]==-1)
        {
            k++;//首先是2的倍数的数 质因子都有2 位置为1
                //再接着3的倍数的数 质因子都有3 位置为2 ... 
            for(j=i;j<max;j+=i)
                a[j]=k;
        }
    }
    while(scanf("%d",&n)!=EOF)
    {
        printf("%d\n",a[n]);
    }
    return 0;
}

抱歉!评论已关闭.