D 扔硬币
题解
数学题。计算P(AB)/P(A),即计算
对于组合数的计算,需要用逆元。详见逆元博客
AC代码
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);
//#define ull unsigned long long
#define ull long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
#define pre(i, x, y) for(int i = x; i >=y; i--)
#define INF (0x3f3f3f3f)
const ull mod = 1e9 + 7;
using namespace std;
const int N = 1e5 + 10;
ull f[N], finv[N];
ull quickPow(ull a, ull b, ull p) {
ull ans = 1;
while (b) {
if (b & 1) ans = ans * a % p;
a = a * a % p;
b >>= 1;
}
return ans;
}
void init() {
f[0] = f[1] = 1;
finv[0] = finv[1] = 1;
rep(i, 2, N - 10) {
f[i] = f[i - 1] * i % mod;
finv[i] = finv[i - 1] * quickPow(i, mod - 2, mod) % mod;
}
}
ull C(int n, int m) {
return f[n] % mod * finv[m] % mod * finv[n - m] % mod;
}
int main() {
init();
int T;
cin >> T;
while (T--) {
int n, m, k;
cin >> n >> m >> k;
if (m + k > n) {
cout << "0" << endl;
continue;
}
ull sum = 0, num = 0;
ull fz = C(n, k);
rep(i, 0, m - 1) {
sum = (sum + C(n, i)) % mod;
}
ull fm = quickPow(2, n, mod) - sum;
if (fm < 0) fm = (fm + mod) % mod;
ull ans = fz * quickPow(fm, mod - 2, mod) % mod;
cout << ans << endl;
}
return 0;
}