Largest prime factor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10004 Accepted Submission(s): 3534
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
题意:求最大素因子的序号,素数序号即为在素数表中的位置,例如:第1个为 2 ,第2个为 3 ,第 3 个为 5,
特别规定:第 0 个为 1.
用了素数筛选法还是超时,网上查看别人的代码,几乎一模一样,最后发现,别人用的是scanf(),printf(),而我用的是cin,cout,一改,便A了.
AC代码:
#include <iostream>
#include <cstdio>
using namespace std;
#define maxn 1000000+5
int a[maxn];
int main()
{
int ans=0;
for(int i=2;i<maxn;i++)
{
if(!a[i])
{
ans++;
for(int j=i;j<maxn;j+=i)
a[j]=ans;
}
}
int n;
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",a[n]);
}
return 0;
}

本文介绍了一种快速计算任意整数最大素因子序号的方法,并通过代码实现解决了特定问题。采用素数筛选法结合输入输出优化技巧,确保算法效率。

被折叠的 条评论
为什么被折叠?



