求组合数四种方法

给定 n 组询问,每组询问给定两个整数 a,b,请你输出 Cabmod(109+7) 的值。

O(n*n)做法递推式预处理

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2010,mod = 1e9+7;
int c[N][N];
void init(){
    for (int i=0;i<N;i++)
        for (int j = 0; j <=i; j ++ )
        if (!j) c[i][j]=1;
        else
        c[i][j] = (c[i-1][j]+c[i-1][j-1])%mod;
}
int main()
{
    ios::sync_with_stdio(false);
    init();
    int n;
    cin>>n;
    while (n -- ){
        int a,b;
        cin>>a>>b;
        cout<<c[a][b]<<endl;
    }
    return 0;
}

O(n*log n)逆元预处理

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+10,mod = 1e9+7;
int f1[N],f2[N];
int qmi(int a,int k,int p)
{
    int res = 1;
    while (k)
    {
        if (k&1) res = (LL)res*a%p;
        a = (LL) a*a%p;
        k>>=1;
    }
    return res;
}
int main()
{
    ios::sync_with_stdio(false);
    f1[0]=f2[0]=1;
    for (int i=1;i<N;i++)
    {
        f1[i] = (LL)f1[i-1]*i%mod;
        f2[i] = (LL)f2[i-1]*qmi(i,mod-2,mod)%mod;
    }
    int n;
    cin>>n;
    while (n -- ){
        int a,b;
        cin>>a>>b;
       printf ("%d\n",(LL)f1[a]*f2[b]%mod*f2[a-b]%mod);
    }
    return 0;
}

Lucas定理

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;


int qmi(int a, int k, int p)
{
    int res = 1;
    while (k)
    {
        if (k & 1) res = (LL)res * a % p;
        a = (LL)a * a % p;
        k >>= 1;
    }
    return res;
}


int C(int a, int b, int p)
{
    if (b > a) return 0;

    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) % p;
    }
    return res;
}


int lucas(LL a, LL b, int p)
{
    if (a < p && b < p) return C(a, b, p);
    return (LL)C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;
}


int main()
{
    int n;
    cin >> n;

    while (n -- )
    {
        LL a, b;
        int p;
        cin >> a >> b >> p;
        cout << lucas(a, b, p) << endl;
    }

    return 0;
}

高精度加质数分解

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;


const int N = 5010;

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


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


int get(int n, int p)
{
    int res = 0;
    while (n)
    {
        res += n / p;
        n /= 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()
{
    int a, b;
    cin >> a >> b;

    get_primes(a);

    for (int i = 0; i < cnt; i ++ )
    {
        int p = primes[i];
        sum[i] = get(a, p) - get(a - b, p) - get(b, p);
    }

    vector<int> res;
    res.push_back(1);

    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 -- ) printf("%d", res[i]);
    puts("");

    return 0;
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值