数学知识——组合数

数学知识——组合数

  • 组合数I(n<=10000, a,b<=2000 mod p)
  • 组合数II(n<=1000,a,b<=105 mod p)
  • 组合数III(n<=20, a,b<=1018, mod p)
  • 组合数IV(a,b<=5000)
  • 满足条件的01序列

AcWing 885. 求组合数 I

  • 核心思路:预处理、动态规划o(n2)
    • 从a个数中选b个数的所有方案可以分为两类(从a个物品中任选一个设为x):
      • 选出的b个物品包含x:c[a-1][b-1]
      • 选出的b个物品不包含x:c[a-1][b]
    • 故c[a][b] = c[a-1][b-1] + c[a-1][b]
    • 初始化:
      • 非法状态:c[i][j] = 0
      • 起点边界:c[i][0] = 1(i = 1,2,…,N-1)
#include<iostream>

using namespace std;

typedef long long LL;
const int N = 2005, mod = 1e9 + 7;

int n;
LL c[N][N];

int main()
{
    for(int i = 0; i < N; i++)
        c[i][0] = 1;
    
    for(int i = 1; i < N; i++)
        for(int j = 1; j < N; j++)
            c[i][j] = c[i - 1][j - 1] + c[i - 1][j] % mod;
            
    cin >> n;
    
    while(n--)
    {
        int a, b;
        cin >> a >> b;
        cout << c[a][b] % mod<< endl;
    }
    return 0;
}

AcWing 886. 求组合数 II

  • 核心思路:预处理逆元和阶乘o(n)
  • 因为a,b太大了用动态规划预处理会超时,所以这里不用。
  • 因为1e9+7为质数,1e9+7与所有小于1e9+7的数互质,可以用快速幂来算出逆元
  • 预处理出阶乘和阶乘的模mod逆元,每次询问查表即可
#include<iostream>

using namespace std;

const int N = 1e5 + 5, mod = 1e9 + 7;
typedef long long LL;

LL fact[N], infact[N];
int n;

int qmi(int a, int k, int p)
{
    int res = 1;
    while(k)
    {
        if(k & 1)res = (LL)res * a % mod;
        k >>= 1;
        a = (LL)a * a % mod;
    }
    return res;
}
int main()
{
    fact[0] = infact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i % mod;
        infact[i] = infact[i - 1] * qmi(i, mod - 2, mod) % mod;
    }
    
    cin >> n;
    while(n--)
    {
        int a, b;
        cin >> a >> b;
        cout << fact[a] % mod * infact[b] % mod * infact[a - b] % mod << endl;
    }
    return 0;
}

AcWing 887. 求组合数 III

  • 因为a,b大的离谱,所以上述两种方法都不适用,但是询问数n很小。感觉该题的做法更像求组合数II做法的升级版。
  • 卢卡斯定理:c[a][b] % p = c[a%p][b%p] * c[a/p][b/p] % p
#include<iostream>

using namespace std;

typedef long long LL;
int p;

int qmi(int a, int k)
{
    LL res = 1;
    while(k)
    {
        if(k & 1)res = res * a % p;
        k >>= 1;
        a = (LL)a * a % p;
    }
    return res;
}
//计算a,b均小于p时的组合数,利用阶乘+逆元+预处理(对应求组合数II)
LL C(int a, int b)
{
    int res = 1;
    for(int i = 1, j = a; i <= b; i++, j--)
    {
        res = (LL)res * j % p;
        res = (LL)res * qmi(i, p - 2) % p;
    }
    return res;
}

LL lucas(LL a, LL b)
{
    if(a < p && b < p)return C(a, b) % p;
    return C(a % p, b % p) * lucas(a / p, b / p) % p;//卢卡斯定理
}
int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        LL a, b;
        cin >> a >> b >> p;
        cout << lucas(a, b) << endl;
    }
    return 0;
}

AcWing 888. 求组合数 IV

  • 核心思路:高精度乘法、线性筛质数
  • 没有mod一个p,中间变量和结果太大一个变量存不下,所以要用数组来存数
  • c[a][b] = a!/b!(a-b)!
  • 将筛选1~a的质因数p1、p2、…pm,统计a!/b!(a-b)!的质因数相应的指数a1,a2,…am。
  • 利用高精度乘法计算p1a1 * p2a2* … *pmam
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>

using namespace std;

const int N = 5005;

int primes[N], cnt, sum[N];
bool st[N];
int a, b;

void broke(int n)
{
    for(int i = 2; i <= n; i++)
    {
        if(!st[i])primes[cnt++] = i;
        for(int j = 0; i * primes[j] <= n; j++)
        {
            st[i * primes[j]] = true;
            if(i % primes[j] == 0)
                break;
        }
    }
}

int get(int a, int p)//统计a中质因数p的指数并返回该指数
{
    int res = 0;
    while(a)
    {
        res += a / p;
        a /= p;
    }
    return res;
}

vector<int> mul(vector<int>a, int b)
{
    vector<int>c;
    int t = 0;
    for(int i = 0; i < a.size(); i++)
    {
        t += a[i] * b;
        c.push_back(t % 10);
        t /= 10;
    }
    while(t)
    {
        c.push_back(t % 10);
        t /= 10;
    }
    return c;
}
int main()
{
    cin >> a >> b;
    broke(a);//筛出2~a中的所有质数,这些质数包含了a!,b!和(a-b)!的所有质因数
    for(int i = 0; i < cnt; i++)
    {
        int p = primes[i];
        sum[i] = get(a, p) - get(b, p) - get(a - b, p);//计算c[a][b]中质因数p的指数
    }
    vector<int>res;
    res.push_back(1);//依次遍历筛选出的2~a的所有质数,利用高精度乘法循环将该质数乘入res中,循环次数为sum[i]
    for(int i = 0; i < cnt; i++)
        for(int j = 0; j < sum[i]; j++)
            res = mul(res, primes[i]);
    for(int i = res.size() - 1; i >= 0; i--)
        cout << res[i];
    return 0;
}

AcWing 889. 满足条件的01序列

  • 卡特兰定理:个数为c[2n][n]/(n+1)
  • 知道了卡特兰定理,则该题转化为一个求组合数的问题
#include<iostream>

using namespace std;

const int mod = 1e9 + 7;
typedef long long LL;

int qmi(int a, int k)
{
    int res = 1;
    while(k)
    {
        if(k & 1)res = (LL)res * a % mod;
        k >>= 1;
        a = (LL)a * a % mod;
    }
    return res;
}
int main()
{
    int n;
    int res = 1;
    cin >> n;
    for(int i = 1, j = 2 * n; i <= n; i++, j--)
    {
        res = (LL)res * j % mod;
        res = (LL)res * qmi(i, mod - 2) % mod;
    }
    res = (LL)res * qmi(n + 1, mod - 2) % mod;
    cout << res << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值