http://acm.hdu.edu.cn/showproblem.php?pid=2136
Largest prime factor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11089 Accepted Submission(s): 3914
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
题意:
给定一个数字,求出它的最大素因数。
思路:
之前没理解题意(题意参考了新浪博客->一个人的旅行,)。
筛选法即可。注意特殊情况0, 1;
AC CODE:
#include<stdio.h>
#include<cstring>
#include<algorithm>
#define AC main()
using namespace std;
const int MYDD = 1103 + 1e6;
bool isprime[MYDD];
int LPS[MYDD] = {0, 0, 1, 2, 1, 3};
void Init() {
int p = 0;
memset(isprime, 1, sizeof(isprime));
isprime[2] = 1;//当前的最大素因子
for(int j = 2; j < MYDD; j++) {
if(isprime[j]) {
p++;
LPS[j] = p;
for(int k = j*2; k < MYDD; k=k+j) {
isprime[k] = 0;
LPS[k] = p;//是当前素数倍数的数字其最大素因子
}
}
}
}
int AC {
int n;
Init();
while(scanf("%d", &n) != EOF) {
printf("%d\n", LPS[n]);
}
return 0;
}

本文介绍了一个算法问题——给定一个数字,如何找出该数字的最大素因数及其在素数序列中的位置。通过使用筛选法,文章提供了一种高效解决此问题的方法,并给出了完整的AC代码。

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



