POJ 3421 多重组合排列

本文深入探讨了X-Factor链算法,一种用于求解整数因子链最大长度及相应链数目的算法。通过分解质因子,分析了如何构建最长因子链,并详细解释了多重集全排列原理,用于计算特定长度的因子链数目。文章提供了C++实现代码,展示了算法的具体应用。

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,然后分解它的因子从X0到Xm,其中X(k+1)>Xk且Xk整除X(k+1),即X(k+1)%Xk==0,让你求出这样的序列的最长长度,当然这个长度你可以理解为下标m(从0开始的),也可以理解成所有的因子不包含1的个数,然后输出的下一个数是存在长度为m这样的因子序列有几个;

分析:因子问题,可以想到向质因子考虑,因为任何一个整数都是由其质因子相乘得到的,因此可以对整数分解质因子,对于最长链的长度可以这样求:拿样例n=100来说,它的质因子有1,2,5,其中2有两个,5有2个(这点很重要),它的题目要求最长链的因子都是由前一项乘以一个质因子得到的:

举个栗子:100存在这样一个符合题意的样例 1 ,2 ,4 ,20 ,100,然后对其分解变成1,2,2*2,2*2*5,2*2*5*5,这样就可以明显看出我上面的那一句是什么意思,20>4,20%4==0,那么20,相对于4就是4乘以一个质因子5得到的20,最终就是将所有的质因子相乘得到整数n(这样才是最长的,2最多有2个,5最多也有2个,因此答案不可能出现3个2或者3个5相乘的因子的情况),因此最长的链的长度就是所有质因子个数的相加的和;

然后再求最长链的个数问题,在这里我是参考了这位博主的思路来理解的:传送

然后就是多重集全排列的问题了

https://blog.youkuaiyun.com/silence401/article/details/52787782?locationnum=10&fps=1

https://blog.youkuaiyun.com/theArcticOcean/article/details/46794101?utm_source=blogxgwz3

 

综上:其组合数为N!/ (N1! *N2! * N3! *Nm!)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
const int N=1e4;
int prime[N];
int factor(int n){
    int tot=0;
    for(int i=2;i*i<=n;i++){
        if(n%i==0) {
        n/=i;
        prime[++tot]++;
        while(n%i==0) n/=i,prime[tot]++;
        }
    }
    if(n>1) prime[++tot]++;
    return tot;
}
ll Sum(int v){
    ll ans=0;
    for(int i=1;i<=v;i++) ans+=prime[i];
    return ans;
}
ll Sum1(ll num,int u)
{
    ll base=1;
    for(int i=2;i<=num;i++)
        base*=i;
        for(int k=1;k<=u;k++){
            ll t=1;
            for(int j=2;j<=prime[k];j++)
            t*=j;
        base/=t;
        }
    return base;
}
int main(){
    int n;
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while(scanf("%d",&n)!=EOF){
            memset(prime,0,sizeof prime);
    int sum=factor(n);
    ll len=Sum(sum);
    ll ans=Sum1(len,sum);
    printf("%lld %lld\n",len,ans);
    }
}

 

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1017 1018 1019 1028 1032 1035 1040 1042 1045 1046 1047 1050 1056 1061 1062 1063 1065 1067 1068 1080 1083 1088 1089 1091 1094 1095 1102 1111 1113 1117 1118 1125 1126 1127 1129 1130 1131 1132 1141 1142 1144 1149 1151 1157 1159 1160 1163 1164 1166 1174 1177 1182 1183 1186 1188 1189 1190 1191 1195 1200 1201 1207 1218 1226 1251 1256 1258 1260 1273 1274 1276 1283 1298 1305 1306 1308 1315 1316 1319 1321 1323 1324 1325 1328 1338 1339 1364 1389 1401 1422 1423 1426 1455 1458 1459 1469 1477 1485 1511 1517 1519 1523 1552 1562 1564 1565 1573 1579 1651 1654 1655 1656 1658 1659 1663 1664 1699 1700 1703 1716 1730 1737 1740 1753 1771 1797 1799 1804 1833 1837 1840 1844 1861 1887 1906 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1970 1979 1980 1985 1988 1989 2000 2001 2002 2018 2019 2021 2027 2033 2044 2051 2081 2084 2104 2109 2112 2135 2136 2137 2153 2155 2181 2182 2184 2185 2186 2187 2188 2190 2192 2195 2228 2229 2234 2236 2241 2242 2245 2247 2248 2249 2253 2264 2287 2299 2301 2309 2336 2337 2348 2352 2353 2362 2371 2378 2386 2387 2388 2389 2392 2393 2394 2402 2403 2406 2411 2413 2419 2421 2446 2449 2456 2479 2488 2492 2503 2509 2513 2524 2528 2531 2533 2545 2553 2559 2564 2575 2576 2586 2591 2593 2594 2602 2623 2632 2656 2676 2680 2707 2750 2774 2777 2780 2782 2812 2818 2840 2908 2922 2934 2965 2983 2993 2996 3020 3041 3168 3169 3176 3183 3184 3185 3191 3193 3214 3219 3224 3250 3253 3255 3256 3257 3258 3259 3264 3267 3273 3275 3277 3278 3279 3280 3295 3297 3302 3303 3311 3312 3321 3325 3348 3349 3355 3356 3357 3368 3372 3386 3400 3421 3424 3425 3427 3428 3438 3452 3468 3486 3517 3561 3585 3589 3602 3612 3614 3615 3616 3617 3618 3619 3620 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3637 3660 3661 3662 3663 3664 3665 3666 3668 3669 3670 3671 3672 3673 3687
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值