HDU 2136 素数打表+求质数因子

时间:2024-06-04 19:06:20

Largest prime factor

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

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
1
2
3
4
5
Sample Output
1
2
1
3
Author
Wiskey
Source
题意:求一个数的最大质数因子在素数表中的位置
 素数打表+求质因子
#include<bits/stdc++.h>
using namespace std;
int n;
bool visit[1000005];
int prime[1000005];
map<int,int>mp;
int len;
int que[10];
void init_prim()
{
memset(visit, true, sizeof(visit));
int num = 0;
for (int i = 2; i <= 1000000; ++i)
{
for(int j=i+i;j<=1000000;j=j+i)
visit[j]=false;
}
for(int i=2;i<=1000000;i++)
if(visit[i])
{
prime[++num]=i;
mp[i]=num;
}
}
void prime_(int nn)
{
len=0;
for(int i=2;i*i<=nn;i++)
{
if(nn%i==0)
{
que[len++]=i;
while(nn%i==0)
{
nn=nn/i;
}
}
}
if(nn>1)
que[len++]=nn;
}
int main()
{
init_prim();
while(scanf("%d",&n)!=EOF)
{
len=0;
memset(que,0,sizeof(que));
if(n==1)
printf("0\n");
else
{
prime_(n);
sort(que,que+len);
//cout<<len<<endl;
printf("%d\n",mp[que[len-1]]);
}
}
return 0;
}