X-factor Chains
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions:7875 | Accepted: 2505 |
Description
Given a positive integer X, an X-factor chain of length m is a sequence of integers,
1 = X0, X1, X2, …, Xm = X
satisfying
Xi < Xi+1 and Xi | Xi+1 where a | b means a perfectly divides into b.
Now we are interested in the maximum length of X-factor chains and the number of chains of such length.
Input
The input consists of several test cases. Each contains a positive integer X (X ≤ 220).
Output
For each test case, output the maximum length and the number of such X-factors chains.
Sample Input
2 3 4 10 100
Sample Output
1 1 1 1 2 1 2 2 4 6
dp:
1000000的因子不足50个,直接dp便可求得。
代码:
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
int dp[3003];
long long sum[3003];
int main()
{
int x;
while(~scanf("%d",&x))
{
int y[3003],r=0;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
if(i*i==x)
y[r++]=i;
else y[r++]=i,y[r++]=x/i;
}
}
y[r++]=x;
sort(y,y+r);
for(int i=0;i<=3000;i++)
dp[i]=sum[i]=1;
int ma=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<i;j++)
{
if(y[i]%y[j]==0)
{
if(dp[j]+1>dp[i])
dp[i]=dp[j]+1,sum[i]=sum[j];
else if(dp[j]+1==dp[i])
{
sum[i]=sum[i]+sum[j];
}
}
}
ma=max(ma,dp[i]);
}
long long s=0;
for(int i=0;i<r;i++)
if(dp[i]==ma)
s+=sum[i];
printf("%d %lld\n",ma,s);
}
return 0;
}
素因子分解: 那100来说,先让他除以最小素数直到不能增除,100/2=50; 50/2=25。不能整出了,100=2^2*25; 25除以下一个素数,25/3不能整除,25/5=5,5/5=1;除到1结束。 所以100=2^2*5^2;最长因子便是指数相加2+2=4; 因为因子要保证后一项整除前一项, 这四个因子便是 1、2 2*2 2*2*5 2*2*5*5 2、2 2*5 2*5*2 2*5*2*5 3、2 2*5 2*5*5 2*5*5*2 4、5 5*5 5*5*2 5*5*2*2 5、5 5*2 5*2*2 5*2*2*5 6、5 5*2 5*2*5 5*2*5*2 可以看出如果有长度为4 便是四个因子的随机组合,有4!种 但是其中有重复,2有2个,5有2个,去掉重复的情况便是 4!/(2!+2!) 代码: #include<cstdio> #include<string.h> bool mp[1100005]; long long prim[300002],K[33]; int L; using namespace std; void sushu() { L=0; memset(mp,1,sizeof(mp)); for(int i=2;i<=1100000;i++) { if(mp[i]) { prim[++L]=i; for(int j=i+i;j<=1100000;j=j+i) mp[j]=0; } } K[1]=1; for(long long i=2;i<=30;i++) K[i]=i*K[i-1]; } int main() { sushu(); long long n; while(~scanf("%lld",&n)) { int i,j;bool bb=0; long long a=0,s=0,r=1; for(i=1;i<=L;i++) { while(n%prim[i]==0) { bb=1;a++; n=n/prim[i]; } if(bb)bb=0,s+=a,r=r*K[a],a=0; if(n==1)break; } printf("%lld %lld\n",s,K[s]/r); } return 0; }

本文详细解析了X-factor链条算法的实现原理与过程,通过具体的样例输入输出展示了如何求解给定正整数X的最大长度X-factor链条及相应链条的数量。文章提供了两种不同的算法实现思路,包括动态规划方法与素因子分解方法。
195

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



