X-factor Chains
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7733 | Accepted: 2447 |
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
大致题意:构造一个序列,使得ai整除ai+1,a0 = 1,an = x,现在给定x,求这样的序列最长的长度是多少?有多少个?
分析:将x分解质因数.从第i个数变成第i-1个数就是去掉一个质因子,那么把x的质因子的次数加上,这就是第一问的答案.第二问实际上就是要求排列数,相同的数在不同的位置只能算一次.那么利用可重复的排列数公式计算即可.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll n,ans,jie[30],sum,yinzi[30],tot,mul; int main() { jie[0] = 1; for (int i = 1; i <= 20; i++) jie[i] = jie[i - 1] * i; while (scanf("%lld",&n) != EOF) { tot = ans = sum = 0; mul = 1; memset(yinzi,0,sizeof(yinzi)); for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { ++tot; while (n % i == 0) { n /= i; yinzi[tot]++; sum++; } } } if (n > 1) { ++tot; ++sum; yinzi[tot]++; } printf("%lld ",sum); ans = jie[sum]; for (int i = 1; i <= tot; i++) mul *= jie[yinzi[i]]; ans /= mul; printf("%lld ",ans); } return 0; }