X-factor Chains
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8730 | Accepted: 2775 |
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,求由n的因子组成的序列(任意前一项能整除后一项)的最大长度,以及所有长度等于最大长度序列的个数,
思路:
想让序列尽可能长,则后一项与前一项的比值要尽可能小,又因为任意一个合数都能分解成若干个质数相乘,所以一定存在一个最小的素数因子。
把素因子从小到大排序,那么,x1一定是第一个素因子,x2是x1乘下一个素因子,不难发现,最长的长度就是所有素因子的个数和。
比如100,a为素因子,b为素因子个数(100=2*2*5*5,素因子从小到大排序是:2,2,5,5)
a | 2 | 5 |
b | 2 | 2 |
1、序列的首是1毋庸置疑
2、为了序列尽量长用2分割,2
3、此时2还剩1个,2*2
4、此时2已用完,用5 ,2*2*5
5、最后是2*2*5*5
即序列最长为4:1、2、4、20、100
在求有几种序列的时候,我们可以把质因子看做物品,把分解后的质因数个数看成是物品个数,这就转换成了多重集排列问题,按照公式可求解。
设S是一个集合,其中有Ri个xi,则成S是一个多重集
比如说100,它质因数是2和5,分别有2个,那么排序有(4!)/(2!*2!)=6
2 2 5 5 -> 2 4 20 100
2 5 2 5 -> 2 10 20 100
2 5 5 2 -> 2 10 50 100
5 5 2 2 -> 5 25 50 100
5 2 5 2 -> 5 10 50 100
5 2 2 5 -> 5 10 20 100
一共6种
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
int x;
ll f[25];
void solve(){
int ans=0;ll base=1;
for(int i=2;i*i<=x;i++){
if(x%i==0){
int cnt=0;
while(x%i==0){
x/=i;
cnt++;
}
ans+=cnt;
base*=f[cnt];
}
}
if(x!=1)ans++;
ll res=f[ans]/base;
printf("%d %lld\n",ans,res);
return ;
}
int main(){
f[0]=1;
for(int i=1;i<=22;i++){
f[i]=f[i-1]*i;
}
while(scanf("%d",&x)!=EOF){
solve();
}
}