POJ 3421 X-factor Chains (整数分解+组合数)

21 篇文章 0 订阅
20 篇文章 0 订阅
X-factor Chains
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6496 Accepted: 2018

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

Source

POJ Monthly--2007.10.06, ailyanlu@zsu

题目大意:
    输入一个数n,有一条链X,X0=1,xm=n,而且后一个数整除前一个数为0。求链的最长长度,和可以形成的链数。

解题思路:
    对于任意一个数它的最长链的第i项一定是i个质因子的乘积,所以最长链长就是各个质因子的幂之和。链数就是组合数:质因子幂之和的阶乘除以各个质因子幂的阶乘。

附AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define LL long long

int x;

vector<int> prime_factor_time(int n)//求各个质因数的幂
{
    vector<int> res;
    for(int i=2;i*i<=n;++i)
    {
        int time=0;
        while(n%i==0)
        {
            ++time;
            n/=i;
        }
        res.push_back(time);
    }
    if(n!=1)
        res.push_back(1);
    return res;
}

LL factor(const int& n)//阶乘
{
    LL res = 1;
    for (int i = 1; i <= n; ++i)
        res *= i;
    return res;
}

int main()
{
    while(~scanf("%d",&x))
    {
        int max_len=0;//计算最长链
        vector<int> times=prime_factor_time(x);
        for(vector<int>::iterator it=times.begin();it!=times.end();++it)
            max_len+=(*it);
        LL num=factor(max_len);//计算链数
        for(vector<int>::iterator it=times.begin();it!=times.end();++it)
            num/=factor(*it);
        printf("%d %lld\n",max_len,num);
    }
    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值