X-factor Chains
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6496 | Accepted: 2018 |
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
Source
POJ Monthly--2007.10.06, ailyanlu@zsu
题目大意:
输入一个数n,有一条链X,X0=1,xm=n,而且后一个数整除前一个数为0。求链的最长长度,和可以形成的链数。
解题思路:
对于任意一个数它的最长链的第i项一定是i个质因子的乘积,所以最长链长就是各个质因子的幂之和。链数就是组合数:质因子幂之和的阶乘除以各个质因子幂的阶乘。
附AC代码:
题目大意:
输入一个数n,有一条链X,X0=1,xm=n,而且后一个数整除前一个数为0。求链的最长长度,和可以形成的链数。
解题思路:
对于任意一个数它的最长链的第i项一定是i个质因子的乘积,所以最长链长就是各个质因子的幂之和。链数就是组合数:质因子幂之和的阶乘除以各个质因子幂的阶乘。
附AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define LL long long
int x;
vector<int> prime_factor_time(int n)//求各个质因数的幂
{
vector<int> res;
for(int i=2;i*i<=n;++i)
{
int time=0;
while(n%i==0)
{
++time;
n/=i;
}
res.push_back(time);
}
if(n!=1)
res.push_back(1);
return res;
}
LL factor(const int& n)//阶乘
{
LL res = 1;
for (int i = 1; i <= n; ++i)
res *= i;
return res;
}
int main()
{
while(~scanf("%d",&x))
{
int max_len=0;//计算最长链
vector<int> times=prime_factor_time(x);
for(vector<int>::iterator it=times.begin();it!=times.end();++it)
max_len+=(*it);
LL num=factor(max_len);//计算链数
for(vector<int>::iterator it=times.begin();it!=times.end();++it)
num/=factor(*it);
printf("%d %lld\n",max_len,num);
}
return 0;
}