HDU 2136 Largest prime factor(素数筛法)

来源:互联网 发布:plc编程仿真软件 编辑:程序博客网 时间:2024/03/29 08:30

Largest prime factor

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12087    Accepted Submission(s): 4290


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
12345
 

Sample Output
01213

题意:每一个素数都对应一个序号,比如说1对应0,2对应1,3对应2,5对应3,;现在给你一个数n,要你找出最大质因数对应的序号。

思路:素数筛法求解;先用素数筛法打表,对每一个素数,把它以及它的倍数都标上序号,当输入n时,直接打印即可。

AC代码:

#include<cstdio>#include<cstring>using namespace std;const int maxn=1e6+5;int save[maxn];int main(){memset(save,0,sizeof(save));int cnt=1;save[1]=0;for(int i=2;i<maxn;i++){if(!save[i]){for(int j=i;j<maxn;j+=i)save[j]=cnt;cnt++;}}int n;while(scanf("%d",&n)==1){printf("%d\n",save[n]);}return 0;}




1 0
原创粉丝点击