模版-组合数带取模

#include <cstdio>

typedef long long ll;

const int N = 100005;		// 最大能计算的n上限
const int MOD = 1000000007; // 1e9 + 7

ll fac[N]; // 用于存储阶乘结果

ll qpow(ll a, ll b) { // 快速幂
	ll res = 1, base = a;
	while (b) {
		if (b & 1) res = res * base % MOD;
		base = base * base % MOD;
		b >>= 1;
	}
	return res;
}

void init() {
	fac[0] = 1;
	for (int i = 1; i < N; i++) {
		fac[i] = fac[i - 1] * i % MOD;
	}
}

ll cal(ll n, ll m) {
    if (n < m) return 0;
    return 1LL * fac[n] * qpow(fac[m], MOD - 2) % MOD * qpow(fac[n - m], MOD - 2) % MOD;
}

int main() {
	init();
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		ll n, m;
		scanf("%lld %lld", &n, &m);
		ll res = cal(n, m);
		printf("%lld\n", res);
	}

	return 0;
}
posted @ 2020-12-21 12:44  牟翔宇  阅读(116)  评论(0编辑  收藏  举报