hdu 2136 素数打表

来源:互联网 发布:编程常用语言有哪些 编辑:程序博客网 时间:2024/05/22 00:24

Largest prime factor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10083 Accepted Submission(s): 3564

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

Author
Wiskey

Source
HDU 2007-11 Programming Contest_WarmUp

Recommend
威士忌

//超时算法:#include <stdio.h>#define HIGH 1000000int A[HIGH+1]={0};void PRIME(void){    for(int i=2;i<=HIGH;i++)        if(!A[i])            for(int j=i*2;j<=HIGH;j+=i) A[j]=1;  //O(NloglogN)    for(int i=2,count=0;i<=HIGH;i++)    {        if(!A[i]) A[i]=++count;                   //O(N)        else      A[i]=0;    }//清零是为了防止直接才Cal里运算} int Cal(int n){    if(n==1) return 0;    if(A[n]) return A[n];    for(int i=n-1;i>=2;i--)        if(A[i]&&!(n%i)) return A[i];    return 0;}int main(){    PRIME();    int n;    while(~scanf("%d",&n))        printf("%d\n",Cal(n));    return 0;}
//AC做法#include <stdio.h>#define HIGH 1000000int A[HIGH+1]={0};void PRIME(void){    for(int i=2,j,count=0;i<=HIGH;i++)    {        if(!A[i])             for(++count,j=i;j<=HIGH;j+=i) A[j]=count; //其实并不难想,只是在打表时顺带了计数,而如果不是这个最大因子时,下一个最大因子会更新    }}int Cal(int n){    if(n==1) return 0;    if(A[n]) return A[n];    return 0;}int main(){    PRIME();    int n;    while(~scanf("%d",&n))        printf("%d\n",Cal(n));    return 0;}  
0 0
原创粉丝点击