X-factor Chains 质因子分解+组合数学

X-factor Chains
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7986 Accepted: 2546

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

将一个数分解成若干个素数相乘,因为后面分解出来的素数一定是在前面分解之后分解的,所以自然就大2倍以上。然后问有多少条链,其实就是问将所有因子的全排列有多少种,但是要除去重复因子的全排列。
n个不同的数的全排列就是n!,除去重复的就是n!除以每个重复因子数的阶乘。
注:防止爆掉都用的long long 存储


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int N=22;
ll fac[N+1]={1};
void init(){
	for(ll i=1;i<=N;i++){
		fac[i]=fac[i-1]*i;
	}
}
int main(){
	init();
	ll n;
	while(cin>>n){
		ll sum=0,d=1;
		for(ll i=2;i*i<=n;i++){
			if(n%i==0){
				ll cnt=0;
				while(n%i==0){
					n/=i;
					cnt++;
				}
				sum+=cnt;
				d*=fac[cnt];
			}
		}
		if(n>1) sum++;
		cout<<sum<<" "<<fac[sum]/d<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43193094/article/details/86686831