组合数模板

组合数模板

组合数1

题目大意: 给定 n n n组询问,每组询问2给定两个整数 a , b a,b a,b,请你输出 C a b m o d ( 1 0 9 + 7 ) C_{a}^b mod(10^9 +7) Cabmod(109+7)的值。

输入格式
第一行包含整数 n n n
接下来 n n n行,每行包含一组 a a a b b b

输出格式
n n n行,每行输出一个询问的解。
数据范围
1 ≤ n ≤ 10000 1\leq n\leq 10000 1n10000
1 ≤ b ≤ a ≤ 2000 1\leq b\leq a\leq 2000 1ba2000

输入样例:

3
3 1
5 3
2 2

输出样例

3
10
1

代码:

#include <iostream>

using namespace std;

const int N = 2005;
typedef long long ll;
const int 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]=(ll)(c[i-1][j]+c[i-1][j-1])%mod;
}

int main()
{
    init();
    int n;
    cin>>n;
    while(n--)
    {
        int a,b;
        cin>>a>>b;
        cout<<c[a][b]<<endl;
    }
    return 0;
}

组合数2

题目大意: 给定 n n n组询问,每组询问2给定两个整数 a , b a,b a,b,请你输出 C a b m o d ( 1 0 9 + 7 ) C_{a}^b mod(10^9 +7) Cabmod(109+7)的值。

输入格式
第一行包含整数 n n n
接下来 n n n行,每行包含一组 a a a b b b

输出格式
n n n行,每行输出一个询问的解。
数据范围
1 ≤ n ≤ 10000 1\leq n\leq 10000 1n10000
1 ≤ b ≤ a ≤ 1 0 5 1\leq b\leq a\leq 10^5 1ba105
输入样例:

3
3 1
5 3
2 2

输出样例

3
10
1

代码:

#include <iostream>

using namespace std;

const int N = 100005;
const int mod = 1e9+7;
int fact[N];
int infact[N];

typedef long long ll;

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

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

组合数3(lucas)

给定 n n n组询问,每组询问给定三个整数 a , b , p a,b,p a,b,p,其中 p p p是质数,请你输出 C a b   m o d   p C_{a}^b \ mod\ p Cab mod p的值。

输入格式
n n n行,每行输出一个询问的解。

数据范围
1 ≤ n ≤ 20 , 1\leq n\leq 20, 1n20,
1 ≤ b ≤ a ≤ 1 0 18 1\leq b\leq a\leq 10^{18} 1ba1018
1 ≤ p ≤ 1 0 5 , 1\leq p \leq 10^5, 1p105

输入样例:

3
5 3 7
3 1 5
6 4 13

输出样例:

3
3
2

代码:

#include <iostream>

using namespace std;

typedef long long ll;

int p;

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

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

int lucas(ll a,ll b)
{
    if(a<p&&b<p)return C(a,b);
    return (ll)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;
}

组合数4

输入 a , b a,b a,b,求 C a b C_{a}^b Cab的值。
注意结果可能很大,需要使用高精度计算。

输入格式
共一行,包含两个整数 a a a b b b
输出格式
共一行,输出 C a b C_{a}^b Cab的值。
数据范围
1 ≤ b ≤ a ≤ 5000 1\leq b\leq a\leq 5000 1ba5000
输入样例:

5 3

输出样例:

10

代码:

#include <iostream>
#include <vector>

using namespace std;
const int N = 5005;

int primes[N],sum[N];
bool st[N];
int cnt;
int p;
void prime(int n)
{
    for(int i=2;i<=n;i++)
    {
        if(!st[i])
        {
            primes[cnt++]=i;
        }
        for(int j=0;primes[j]*i<=n;j++)
        {
            st[primes[j]*i]=true;
            if(i%primes[j]==0)break;
        }
    }
}

int get(int n)
{
    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;
    prime(a);
    for(int i=0;i<cnt;i++)
    {
        p=primes[i];
        sum[i]=get(a)-get(b)-get(a-b);
    }
    vector<int> res;
    res.push_back(1);
    for(int i=0;i<cnt;i++)
    {
        p=primes[i];
        for(int j=0;j<sum[i];j++)
        {
            res=mul(res,p);
        }
    }
    for(int i=res.size()-1;i>=0;i--)
    cout<<res[i];
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值