[USACO 09FEB]Bullcow

Description

题库链接

\(n\) 头牛,每头牛可以为 \(\text{A}\) 牛也可以为 \(\text{B}\) 牛。现在给这些牛排队,要求相邻两头 \(\text{A}\) 牛之间至少间隔 \(k\)\(\text{B}\) 牛,求方案数,对大质数取模。

\(0\leq k<n\leq 100000\)

Solution

考虑枚举有几头 \(\text{A}\) 牛,设为 \(i\)

\(\text{B}\) 牛数为 \(n-i\) 。由垫球法以及隔板法,可知当前情况下方案为

\[{n-i-(i-1)\times k+i}\choose i\]

Code

#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;   
}
posted @ 2019-07-03 11:02 NaVi_Awson 阅读(...) 评论(...) 编辑 收藏