Largest prime factor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11238 Accepted Submission(s): 3976
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
题意就是找到这个数n的最大质因子是第几个素数...
OK,题意很简单,范围有100W,怎么搞呢?
基于素筛的思想,先找到100W内每个素数是第几个,前期工作已经搞定,然后我们想,如果该数如果是素数,则直接输出他是第几个素数即可。如果该数是合数,那么我们一定可以把它拆成2个数的乘积,OK,这里需要一点dp的思路,我们首先假设n=a*b这里,假设我们已经知道了a和b的最大质因子是第几个素数了。下面即可得出状态转移方程
num[n]=max(num[n],max(num[a],num[b]));n=a*b,即b=n/a;
这样,我们即可在nlogn的范围内找到该问题的解。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#define eps 1e-8
#define zero(x) (((x>0?(x):-(x))-eps)
#define mem(a,b) memset(a,b,sizeof(a))
#define memmax(a) memset(a,0x3f,sizeof(a))
#define pfn printf("\n")
#define ll __int64
#define ull unsigned long long
#define sf(a) scanf("%d",&a)
#define sf64(a) scanf("%I64d",&a)
#define sf264(a,b) scanf("%I64d%I64d",&a,&b)
#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define sf2(a,b) scanf("%d%d",&a,&b)
#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define sff(a) scanf("%f",&a)
#define sfs(a) scanf("%s",a)
#define sfs2(a,b) scanf("%s%s",a,b)
#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)
#define sfd(a) scanf("%lf",&a)
#define sfd2(a,b) scanf("%lf%lf",&a,&b)
#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)
#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)
#define sfc(a) scanf("%c",&a)
#define ull unsigned long long
#define pp pair<int,int>
#define debug printf("***\n")
const double PI = acos(-1.0);
const double e = exp(1.0);
const int INF = 0x7fffffff;;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
bool cmpbig(int a, int b){ return a>b; }
bool cmpsmall(int a, int b){ return a<b; }
using namespace std;
int heshu[1000010];
int num[1000010];
int sushu[1000010];
int main()
{
//freopen("data.in","r",stdin);
mem(heshu,0);
mem(sushu,0);
mem(num,0);
int i,j;
heshu[0]=heshu[1]=1;
for(i=2;i*i<=1000000;i++)
for(j=i;j*i<=1000000;j++)
heshu[i*j]=1;
int flag=0;
for(i=1;i<=1000000;i++)
{
if(heshu[i]==0)
{
sushu[i]=++flag;
num[i]=flag;
}
}
for(i=2;i*i<=1000000;i++)
for(j=i;j*i<=1000000;j++)
num[i*j]=max(num[i*j],max(num[i],num[j]));
int n;
while(~sf(n))
{
printf("%d\n",num[n]);
}
return 0;
}