(AcWing)求组合数 II

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

输入格式

第一行包含整数 n。

接下来 n 行,每行包含一组 a 和 b。

输出格式

共 n 行,每行输出一个询问的解。

数据范围

1≤n≤10000,
1≤b≤a≤10^5

输入样例:

3
3 1
5 3
2 2

输出样例:

3
10
1
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100010, mod = 1e9+7;
int fact[N],infact[N];
typedef long long LL;

//快速幂求逆元
int qmi(int a,int b,int p)
{ 
    int res = 1;
    while(b)
    {
        if(b&1) res = (LL)res*a%mod;
        a = (LL)a*a%mod;
        b>>=1;
    }
    return res;
}

int main()
{
    fact[0] = infact[0] = 1;
    for(int i=1;i<N;i++){
            fact[i] = (LL)fact[i-1]*i%mod;           //求i的阶乘
            infact[i] = (LL)infact[i-1]*qmi(i,mod-2,mod)%mod; //求i的逆元的阶乘
    }
    int n;
    cin>>n;
    while(n--)
    {
        int a,b;
        cin>>a>>b;
        /*运用的公式:
       b          a!
      C    = ____________ 
       a      (a-b)!*b!
  
        原式 = a! * ((a-b)!^-1)*(b!^-1);
        */
        cout<<(LL)fact[a]*infact[a-b]%mod*infact[b]%mod<<endl;
    }
    
}

猜你喜欢

转载自blog.csdn.net/GF0919/article/details/131960423
今日推荐