题目描述
给定n组询问,每组询问给定两个整数a,b,请你输出Cba mod (109+7)的值
C++ 代码
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 100010, mod = 1e9 + 7;
LL fact[N], infact[N];//infact[N]指逆元,因为mod为素数,所以可以用费马小定理来求;
//qmi是快速幂;
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()
{
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;
scanf("%d%d", &a, &b);
int res;
res = (LL)fact[a] * infact[b] % mod * infact[a - b] % mod;//前两个相乘之后就要取模;
printf("%d\n", res);
}
return 0;
}