思路:
我们先把k个数取出来,这时就剩n-k+1个空位,然后我们把r个连续的捆绑在一起放进这几个空里,然后在剩下的数里面把还要选的数选了。最后容斥一下就行了
c o d e code code
#include<iostream>
#include<cstdio>
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
int n, k, r, fac[10000005], ifac[10000005];
int qpow(int x, int y) {
ll ans = 1;
for(; y; y >>= 1, x = (ll)x * x % mod) if(y & 1) ans = (ll)ans * x % mod;
return ans;
}
int C(int n, int m) {
if(m > n) return 0;
return (ll)fac[n] * ifac[n - m] % mod * ifac[m] % mod;
}
int main() {
scanf("%d%d%d", &n, &k, &r);
fac[0] = 1, ifac[0] = 1;
for(int i = 1; i <= n; i ++) fac[i] = (ll)fac[i - 1] * i % mod;
ifac[n] = qpow(fac[n], mod - 2);
for(int i = n - 1; i >= 1; i --) ifac[i] = (ll)(i + 1) * ifac[i + 1] % mod;
ll op = 1;
ll ans = 0;
for(int i = 1; i <= k / r; i ++) {
ans += (ll) C(n - k + 1, i) * C(n - i * r, k - i * r) % mod % mod * op;
op = -op;
}
printf("%d", (ans % mod + mod) % mod);
return 0;
}