组合数总结

                                  痛定思痛

 

超赞博客链接:Here~

  由于前期没有认真的考虑学过的算法的使用限制。于是,我决定没一个我学习过的算法都认真总结,分析。

组合数的求解一般有三种:

C(n,m)原始求解方法:

   C(n,m) = [n*(n-1)*(n-2)....(n-m+1)] / [m*(m-1)...1] = (n-m+1)! / m! = n! / [m!*(n-m)!]

   这个公式转换很重要,是解体思路的根本!

 

一、杨辉三角法

二、Lucas暴力求解

三、Lucas打表法

四、当P不是素数

 

第一种就这里就略过。

第二种

解法:

   这时候的C(N,M) % P。N,P都很大,但是M较小(N,P < 10^9,M〈 10^4)。所以,这时候无法与处理阶乘。但是,通过原始公式,我们发现。求解过程最多是循环了M次。所以我们就从这里下手,循环暴力求解。

 

   给出的C(N,M) % P ,这时候的N,P都很大,没发打表。而M相对来说比较小。所以,此时我们可以运用暴力的求解方法。这里的数据范围一般是(N < 10^18,P < 10^9),M < 10^5 (P要为素数)

经典题目链接

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;

typedef long long LL;

LL N,M,P;

LL powmod(LL a,LL b,LL p){
   LL res = 1;
   a %= p;

   while(b > 0){
       if(b & 1) res = res * a % p;
       a = a * a % p;
       b >>= 1;
   }

   return res;
}

//暴力循环求解
LL Norma_C(LL n,LL m,LL p){
    LL ans = 1;
    for(int i = 1;i <= m;++i){
        LL a = (n + i - m) % p;
        LL b = i % p;
        ans = ans * (a * powmod(b,p-2,p) % p) % p; //费马小定理,求解逆元
    }
    return ans;
}

LL Lucas(LL n,LL m,LL p){
    if(!m) return 1;
    if(n%p < m%p) return 0;
    LL tmp = Norma_C(n%p,m%p,p);
    return tmp * Lucas(n / p,m / p,p);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%I64d%I64d%I64d",&N,&M,&P);
        printf("%I64d\n",Lucas(N,M,P));
    }
    return 0;
}


 


第三种:打表Lucas求解

解法:

   只要根据min(N,P)的大小选择一个小的一个打出素数表就好了。

   这种题一般都有明显的特征,要么n < 10^6或者p<10^6的素数。

 

第四种:当P不是素数

解法:

   1、先对P进行质因子分解。

   2、非别对每个质因子大组合数运算  

   3、运用中国剩余定理求最终结果

   一般数据范围: N,M,P < 10^5

经典题目链接

/*
    题目:C(n,m) % P (P为任意数)
    限制:n,m,p < 10^5
*/

typedef long long LL;
const int MAXN = 200005;
int top,primes[MAXN];
bool vst[MAXN];
int N,M,P;

//素数表
void init(){
    memset(vst,0,sizeof(vst));
    vst[0] = vst[1] = 1;
    for(int i = 2;i < MAXN;++i)if(!vst[i]){
        primes[top++] = i;
        for(int j = i+i;j < MAXN;j += i) vst[j] = 1;
    }
}

//计算阶乘n!中包含素因子p的个数
LL factNum(int n,int p){
    LL cnt = 0;
    while(n > 0){
        cnt += n / p;
        n /= p;
    }
    return cnt;
}

//a^b % p
LL powmod(LL a,LL b,LL p){
    LL res = 1;
    a %= p;

    while(b > 0){
        if(b & 1) res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }

    return res;
}

//求C(n,m)%p, p任意数  n,m,p<=1e5
LL solve(int n,int m,int p){
    LL x,y,z,ans = 1;
    for(int i = 0;i < top&&primes[i] <= n;++i){
        x = factNum(n,primes[i]);
        y = factNum(n - m,primes[i]);
        z = factNum(m,primes[i]);
        x -= (y + z);
        ans = ans * powmod(primes[i],x,p) % p;
    }
    return ans;
}


 经典题目链接

这题因为P是固定的,且分解得到的素因子只有两个。所以,我们可以直接运用中国剩余定理。

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long LL;
const int MAXN = 20;
const int MOD1 = 97;
const int MOD2 = 3761599;
const int MOD = MOD1 * MOD2;

int a[MAXN];
LL inv1,inv2;
LL N,M;
LL fact1[MOD1 + 10],fact2[MOD2 + 10];

LL powmod(LL a,LL b,LL mod){
   LL res = 1;
   a %= mod;
   while(b > 0){
      if(b & 1) res  = res * a % mod;
      a = a * a % mod;
      b >>= 1;
   }

   return res;
}

void init(){
    fact1[0] = fact2[0] = 1;
    for(int i = 1;i < MOD1;++i)
        fact1[i] = fact1[i-1] * i % MOD1;
    for(int i = 1;i < MOD2;++i)
        fact2[i] = fact2[i-1] * i % MOD2;

    inv1 = powmod(MOD2,MOD1 - 2,MOD1);   //MOD2 mod MOD1
    inv2 = powmod(MOD1,MOD2 - 2,MOD2);   //MOD1 mod MOD2
}

LL Normal_C(LL n,LL m,LL p,LL fact[]){
    return fact[n] * powmod(fact[m]*fact[n-m],p-2,p) % p;
}

LL Lucas(LL n,LL m,LL p,LL fact[]){
   if(m == 0) return 1;
   if(n%p < m%p) return 0;

   return Normal_C(n%p,m%p,p,fact) * Lucas(n / p,m / p,p,fact) % p;
}

int main()
{
    init();

    int T;
    scanf("%d",&T);
    for(int kase = 1;kase <= T;++kase){
        int k;
        scanf("%I64d%I64d%d",&N,&M,&k);
        for(int i = 0;i < k;++i)
            scanf("%d",&a[i]);
        a[k] = M;

        LL res = 1;
        for(int i = 0;i < k;++i){
            LL m1 = Lucas(a[i+1] - a[i] + N - 1,a[i+1] - a[i],MOD1,fact1);
            LL m2 = Lucas(a[i+1] - a[i] + N - 1,a[i+1] - a[i],MOD2,fact2);
            LL mm = (MOD2 * m1 * inv1 + MOD1 * m2 * inv2) % MOD;   //CAT
            res = res * mm % MOD;
        }

        printf("Case #%d: %I64d\n",kase,res);
    }
    return 0;
}



 复杂加经典神题,强烈推荐!!!传送门

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值