日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj3421 X-factor Chains

發布時間:2024/4/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 100

Sample 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。