#include <bits/stdc++.h>#define i64 long longusing namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
vector<i64> fac(N, 0), inv(N, 0);
i64 qpow(i64 base, int b) {
i64 res = 1;
while (b) {
if (b & 1) res = res * base % mod;
base = base * base % mod;
b >>= 1;
}
return res;
}
i64 C(int x, int y) {
if (x - y < 0) return 0;
if (!y || !(x - y)) return 1;
return ((fac[x] * inv[y] % mod) * inv[x - y]) % mod;
}
int in(int l, int r, int x) {
return l <= x && x <= r;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
fac[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = fac[i - 1] * i % mod;
}
inv[N - 1] = qpow(fac[N - 1], mod - 2);
for (int i = N - 2; i >= 1; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
int n, m, q;
cin >> n >> m >> q;
while (q--) {
int x, t;
cin >> x >> t;
i64 ans = 0;
for (int i = 1; i <= n; i++) {
if (i == x) continue;
int a = min(n - i - in(i, n, x), t - in(i, i + t, x));
int b = m - 1;
i64 c = fac[m];
ans = (ans + (C(a, b) * c % mod)) % mod;
}
cout << ans << "\n";
}
return 0;
}
100 分
C++
#include <bits/stdc++.h>#define i64 long longusing namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
vector<i64> fac(N, 0), inv(N, 0);
inline i64 qpow(i64 base, int b) {
i64 res = 1;
while (b) {
if (b & 1) res = res * base % mod;
base = base * base % mod;
b >>= 1;
}
return res;
}
inline i64 C(int x, int y) {
if (x < 0 || y < 0 || x - y < 0) return 0;
if (!y || !(x - y)) return 1;
return ((fac[x] * inv[y] % mod) * inv[x - y]) % mod;
}
inline int in(int l, int r, int x) {
return l <= x && x <= r;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
fac[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = fac[i - 1] * i % mod;
}
inv[N - 1] = qpow(fac[N - 1], mod - 2);
for (int i = N - 2; i >= 1; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
int n, m, q;
cin >> n >> m >> q;
while (q--) {
int x, t;
cin >> x >> t;
int A = max(1, x - t), B = x - 1;
i64 S = (((n - t) * C(t, m - 1) % mod) + C(t, m)) % mod;
i64 E1 = C(min(t, n - x), m - 1);
i64 C1 = max(0, min(B, n - t) - A + 1) * C(t - 1, m - 2) % mod;
i64 C2 = 0;
if (max(A, n - t + 1) <= B) {
C2 = (C(min(n - A, t - 1), m - 1) - C(n - x, m - 1) + mod) % mod;
}
i64 ans = ((((S - E1 - C1 - C2) % mod) + mod) % mod) * fac[m] % mod;
cout << ans << "\n";
}
return 0;
}