Largest prime factor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4916 Accepted Submission(s): 1728
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
Author
Wiskey
Source
Recommend
威士忌
你说关于素数的题目,我怎么还是弄不清楚!诶,,
这道题目也是非常的巧的,每次都把具备自己因子的数更新,
这样在最后就可以直接输出,
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 1000005;
int p[maxn];
void init()
{
int cnt = 0;
memset(p, -1, sizeof(p));
for (int i = 2; i < maxn; i++)
{
if (p[i] == -1)//如果没有更新到,那么这个数就是素数,
{
cnt++;
for (int j = i; j < maxn; j += i)
{
p[j] = cnt;//更新所有的含有这个因子的数
}
}
}
}
int main()
{
init();
int n;
while(scanf("%d", &n) != EOF)
{
if (n == 1)
{
printf("0\n");
continue;
}
printf("%d\n", p[n]);
}
// system("pause");
return 0;
}