欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ3421 X-factor Chains

程序员文章站 2022-06-08 12:06:27
...

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

题意:

给出一个数字 n ,问 n 的因子组成的满足任意前一项都能整除后一项的序列的最大长度,以及所有不同序列的个数

题解:

分解成素因子后所有素因子的幂和是第一个答案,幂和的阶乘/(各幂的阶乘之积)是第二个答案。

引理一:质因数分解

POJ3421 X-factor Chains
此处幂为底数的个数,想得到更长的序列数,则需要使底数更小
例如n=100
100 用2分解100-> 2*2*25 得到了2个因数:2 2*2
用5分解25-> 2*2*5*5 得到了2个因数:2*2*5 2*2*5*5
此处运用埃氏筛法的思想,分解到sqrt(n)即可

引理二:有n个物体,共k类,第ki类物品数量为ni,则其全排列数为:

POJ3421 X-factor Chains
证明:先在n个空位中放入k1类共C(n,n1)种方法
再在n-n1个空位中放入k2类共C(n-n1,n2)种方法
.
.
.
最后一类 共C(n-n1-….-n(k-1),nl)种方法
用乘法法则:
POJ3421 X-factor Chains

CODE

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
#define maxn 2000
#define LL long long

LL f(LL a)
{
    LL sum = 1;
    for (int i = 2; i <= a; ++i)
        sum *= i;
    return sum;
}

int main()
{
    /*
    long long sum=2;
    for(int i=1;i<=20;i++)
        sum*=2;
    sum=sqrt(sum);
    cout << sum << endl;
     */
    int n;
    while(cin >> n)
    {
        LL Hash[maxn];
        memset(Hash,0,sizeof(Hash));
        int ans1 = 0;
        for (int i = 2; i*i<=n; ++i)
            while(n % i == 0)
            {
                Hash[i]++;
                n /= i;
                ans1++;
                cout << i << endl;
            }

        if (n != 1)
            ans1++;
        LL ans2 = f(ans1);
        for (int i = 2; i < maxn; ++i)
            if (Hash[i])
                ans2 /= f(Hash[i]);
        printf("%d %lld\n", ans1 == 0 ? 1 : ans1, ans2);
    }

    return 0;
}