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倍,问最长的因式相乘链有多长,有几条最长的因式相乘链。
- 其实就是将一个数分解成若干个素数相乘,因为后面分解出来的素数一定是在前面分解之后分解的,所以自然就大2倍以上。然后问有多少条链,其实就是问将所有因子的全排列有多少种,但是要除去重复因子的全排列。
- n个不同的数的全排列就是n!,除去重复的就是n!除以每个重复因子数的阶乘。
#include <map>
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
map <int,int> prim_factor(ll n2) {
map<int,int> maps;
int N = n2;
for(int i=2;i*i<=N;i++) {
while(n2 % i == 0) {
maps[i]++;
n2 /= i;
}
}
if(n2 > 1)
maps[n2]++;
return maps;
};
ll mult(int k) {
ll ans = 1;
for(int i=2;i<=k;i++)
ans *= i;
return ans;
}
int main() {
ll n;
while(scanf("%lld",&n) != EOF) {
map <int,int> :: iterator iter;
map <int,int> maps = prim_factor(n);
int len = 0;
for(iter=maps.begin();iter!=maps.end();iter++)
len += iter->second;
ll ans = mult(len);
for(iter=maps.begin();iter!=maps.end();iter++)
ans /= mult(iter->second);
printf("%d %lld
",len,ans);
}
return 0;
}