題庫連接php
有 \(n\) 頭牛,每頭牛能夠爲 \(\text{A}\) 牛也能夠爲 \(\text{B}\) 牛。如今給這些牛排隊,要求相鄰兩頭 \(\text{A}\) 牛之間至少間隔 \(k\) 頭 \(\text{B}\) 牛,求方案數,對大質數取模。c++
\(0\leq k<n\leq 100000\)spa
考慮枚舉有幾頭 \(\text{A}\) 牛,設爲 \(i\)。code
\(\text{B}\) 牛數爲 \(n-i\) 。由墊球法以及隔板法,可知當前狀況下方案爲ip
\[{n-i-(i-1)\times k+i}\choose i\]get
#include <bits/stdc++.h> using namespace std; const int N = 200000+5, yzh = 5000011; int fac[N], ifac[N], n, k, ans = 1; int C(int n, int m) {return 1ll*fac[n]*ifac[m]%yzh*ifac[n-m]%yzh; } int main() { scanf("%d%d", &n, &k); fac[0] = fac[1] = ifac[0] = ifac[1] = 1; for (int i = 2; i <= (n<<1); i++) fac[i] = 1ll*i*fac[i-1]%yzh; for (int i = 2; i <= (n<<1); i++) ifac[i] = -1ll*yzh/i*ifac[yzh%i]%yzh; for (int i = 2; i <= (n<<1); i++) ifac[i] = 1ll*ifac[i]*ifac[i-1]%yzh; for (int i = 1; i <= n && n-i-(i-1)*k >= 0; i++) (ans += C(n-i-(i-1)*k+i, i)) %= yzh; printf("%d\n", (ans+yzh)%yzh); return 0; }