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
这题就是对X进行质因数分解,然后将1分别累乘它的质因数会得到一个序列,序列长度+1就是最终要求的最长数,至于有多少个这样长度的序列,实际上就是对质因数进行排列,因为存在相同的质因数。所以记录相同的质因数后,再去掉重复的排列法就可以了。
#ifndef HEAD #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string> #include <iostream> #include <queue> #include <list> #include <algorithm> #include <stack> #include <map> using namespace std; #endif // !HEAD long long product(int i) { long long res = 1; for (int c = 1; c <= i;c++) { res *= c; } return res; } int main() { #ifdef _DEBUG freopen("d:\\in.txt", "r", stdin); #endif int a; map<int, int> mapPrimeNumber; while (scanf("%d\n", &a) != EOF) { mapPrimeNumber.clear(); int countofprime = 0; for (int i = 2; i * i <= a; i++) { if (a % i == 0) { a /= i; if (mapPrimeNumber.find(i) != mapPrimeNumber.end()) { mapPrimeNumber[i] ++; } else mapPrimeNumber[i] = 1; countofprime++; i--; } } if (mapPrimeNumber.find(a) != mapPrimeNumber.end()) { mapPrimeNumber[a] ++; } else mapPrimeNumber[a] = 1; countofprime++; long long count = 1; for (int i = 1; i <= countofprime; i++) { count *= i; } for (map<int, int> ::iterator it = mapPrimeNumber.begin(); it != mapPrimeNumber.end();++it) { count /= product(it->second); } printf("%d %d\n", countofprime, count); } return 0; }