因为题目里要求Xi | Xi+1,而Xm又限定为X,所以我们可以想到Xm-1是X除以其某个约数得到的,Xm-1也是一样。由此我们可以知道“X-factor Chains”是通过不断乘以X的约数得到的,为了长度最大,所以约数必须是素数。通过记录有哪些素因数,以及素因子的数量,我们就可以得到链的长度。
而长度最大的链的数量则是这些数的排列数,公式为素因子个数之和的阶乘/每个素因子个数的阶乘。
代码(G++):
#include <iostream>
#include <map>
using namespace std;
map<int, int> res;
map<int, int>::iterator mi;
long long fact(int n)
{
long long v;
if(n == 0 || n == 1) return 1;
else{
v = n;
for(int i=2; i<n; i++)
{
v *= i;
}
return v;
}
}
int main()
{
int x, sum;
long long ans;
while(cin>>x)
{
sum = 0;
res.clear();
for(int i=2; i*i<=x; i++)
{
while(x%i == 0)
{
x /= i;
++res[i];
++sum;
}
}
if(x != 1)
{
++res[x];
++sum;
}
ans = fact(sum);
for(mi=res.begin(); mi!=res.end(); mi++)
{
ans /= fact(mi->second);
}
cout<<sum<<' '<<ans<<endl;
}
return 0;
}
题目:
X-factor Chains
Time Limit: 1000MS Memory Limit: 65536K
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