Files
python/TangDou/AcWing_TiGao/T5/ZhuHeJiShu/1307_GeBanFa.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

39 lines
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}