You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int MOD = 5000011; // 质数,可以使用费马小定理求逆元
const int N = 1e5 + 10;
int fact[N]; // 阶乘数组
// 快速幂
int qmi(int a, int b, int p) {
int res = 1;
a %= p;
while (b) {
if (b & 1) res = res * a % p;
b >>= 1;
a = a * a % p;
}
return res;
}
// 利用组合数定义式求C(m,n),用到快速幂求逆元
int C(int m, int n) {
if (m < n) return 0;
return fact[m] * qmi(fact[n], MOD - 2, MOD) % MOD * qmi(fact[m - n], MOD - 2, MOD) % MOD;
}
int n, k; // n只牛,两只牡牛之间最少需要k只牝牛
signed main() {
// 预处理范围内数字的阶乘
fact[0] = 1;
for (int i = 1; i < N; i++) fact[i] = (fact[i - 1] * i) % MOD;
cin >> n >> k;
int res = 0;
for (int i = 1; i <= (n + k) / (k + 1); i++) // 枚举牡牛的所有可能数量
res = (res + C(n - (i - 1) * k, i)) % MOD; // 累计组合数
// 如果0只牡牛,还应该增加一种方案,就是全都是牝牛
cout << res + 1 << endl;
}