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

题意:

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

题解:

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

引理一:质因数分解


此处幂为底数的个数,想得到更长的序列数,则需要使底数更小
例如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,则其全排列数为:

这里写图片描述
证明:先在n个空位中放入k1类共C(n,n1)种方法
再在n-n1个空位中放入k2类共C(n-n1,n2)种方法
.
.
.
最后一类 共C(n-n1-….-n(k-1),nl)种方法
用乘法法则:
这里写图片描述

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值