Largest prime factor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5330 Accepted Submission(s): 1877
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.
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/* 类似筛素数的方法,先把lpf数组清0,然后题目要求prime[1]=0,所以这个不用改,把prime[2]置为1,因为他自己就是素数,所以他的lpf就是他自己,而2是第一个素数, 接下来为了提高一点点效率,我先把所有的偶数的prime都置为2,用一个sum记录是第几个素数,然后就跟筛素数一样,从3开始找prime为0的数,有且仅有这些数是素数,把相应的sum给prime,然后开始找这些数i的倍数,更新这些倍数j的prime[j]为prime[i],因为他们都是i的倍数,而i是素数,他们当前的最大prime肯定就是i(当然以后可能会更新),prime[i]表示的就是素数i是第几个素数,所以prime[j]=prime[i]。就这样,打好表,然后输入一个n就输出表格的相应内容就行了。还有记住用scanf和printf输入输出不要用cin和cout,不然会超时*/ #include<iostream> #include<stdio.h> #include<math.h> const int MAX=1000000; int x[MAX]; using namespace std; int main() { int n,m,i,j,sum; memset(x,-1,sizeof(x)); x[1]=0; sum=0; for(i=2;i<MAX;i++) { if(x[i]==-1) { sum+=1; for(j=i;j<MAX;j+=i) x[j]=sum; } } while(scanf("%d",&n)!=EOF) printf("%d\n",x[n]); return 0; }