原题链接X-factor Chains
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6575 Accepted: 2054
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
//http://poj.org/problem?id=3421
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = (~(0ULL)>>1);
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=10000 + 5;
int n;
vector<int> res;
//得到一个数的阶乘
ull f(int x){
ull ans=1;
for(int i=2;i<=x;i++)
ans*=i;
return ans;
}
//得到一个数的所有质因数的幂
void solve(int x){
for(int i=2;i*i<=x;i++){
int t=0;//因为从低位开始,所以一定所有的进入循环的i都是质因子
while(x%i==0) {t++;x/=i;}
res.push_back(t);
}
if(x!=1) res.push_back(1);
return;
}
int main(){
while(scanf("%d",&n)==1){
res.clear();
solve(n);
int len=res.size();
int sum=0;//sum代表所有质因子的幂的和
for(int i=0;i<len;i++)
sum+=res[i];
ull ans=f(sum);//得到sum的阶乘
for(int i=0;i<len;i++)
ans/=f(res[i]);//除以其中一个质因数的幂的阶乘
cout << sum << " " << ans << endl;
}
return 0;
}
/*
因子链:将一个数X分解成从1到X的数列,前一个数可以整除后一个数,求最大链长和链的个数。
因为题目里要求Xi | Xi+1,而Xm又限定为X,所以我们可以想到Xm-1是X除以其某个约数得到的,Xm-1也是可以由Xm-2的到的,可以认为这些单位之间就是插入了一个乘数。
由此我们可以知道“X-factor Chains”是通过不断乘以X的约数得到的,为了长度最大,所以约数必须是素数。通过记录有哪些素因数,以及素因子的数量,我们就可以得到链的长度。
而长度最大的链的数量则是这些数的排列数,公式为素因子个数之和的阶乘/每个素因子个数的阶乘。
*/