poj3421 X-factor Chains
題意:
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 100Sample Output
1 1 1 1 2 1 2 2 4 6思路:
一開始想到dp。令dp[i][j]表示長度為i,以j結尾的鏈的個數,于是dp[i+1][k] += dp[i][j] (j為k的因子),然而復雜度高,并不會優化。
后來發現要想鏈最長,只能從1開始,每次乘上這個數的某個質因子才行。于是就變成了分解質因子+排列組合的問題。
實現:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <algorithm> 6 #include <map> 7 using namespace std; 8 typedef long long ll; 9 10 ll fac[21]; 11 int x; 12 13 void init() 14 { 15 fac[0] = 1; 16 for (ll i = 1; i <= 20; i++) 17 fac[i] = fac[i - 1] * i; 18 } 19 20 map<int, int> prime_factor(int n) 21 { 22 map<int, int> res; 23 for (int i = 2; i * i <= n; i++) 24 { 25 while (n % i == 0) 26 { 27 res[i]++; 28 n /= i; 29 } 30 } 31 if (n != 1) 32 res[n] = 1; 33 return res; 34 } 35 36 int main() 37 { 38 init(); 39 while (scanf("%d", &x) != EOF) 40 { 41 map<int, int> f = prime_factor(x); 42 map<int, int>::iterator it; 43 ll res = 1; 44 int cnt = 0; 45 for (it = f.begin(); it != f.end(); it++) 46 { 47 res *= fac[it->second]; 48 cnt += it->second; 49 } 50 printf("%d %lld\n", cnt, fac[cnt] / res); 51 } 52 return 0; 53 }?
轉載于:https://www.cnblogs.com/wangyiming/p/6443982.html
總結
以上是生活随笔為你收集整理的poj3421 X-factor Chains的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: binutils工具集之---nm
- 下一篇: EvenBus源码分析