zoukankan      html  css  js  c++  java
  • poj3421 X-factor Chains

    题意:

    Description

    Given a positive integer X, an X-factor chain of length m is a sequence of integers,

    1 = X0X1X2, …, 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
    ", cnt, fac[cnt] / res);
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    GNU make manual 翻译(九十九)
    GNU make manual 翻译( 九十五)
    Shell的 for 循环小例子
    makefile中对目录遍历的小例子
    GNU make manual 翻译(九十三)
    GNU make manual 翻译( 一百)
    GNU make manual 翻译( 九十七)
    GNU make manual 翻译( 九十八)
    mapserver4.8.3 的readme.win32的中文翻译文件
    遥控器编程
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6443982.html
Copyright © 2011-2022 走看看