BZOJ3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛(dp)

题意

    约翰要带N(1≤N≤100000)只牛去参加集会里的展示活动,这些牛可以是牡牛,也可以是牝牛.牛们要站成一排.但是牡牛是好斗的,为了避免牡牛闹出乱子,约翰决定任意两只牡牛之间至少要有K(O≤K<N)只牝牛.
    请计算一共有多少种排队的方法.所有牡牛可以看成是相同的,所有牝牛也一样.答案对5000011取模

Sol

网上的题解是前缀和优化dp?

那我说一个不一样的做法

设$f[i]$表示到第$i$个位置,该位置放了牡牛的方案,$g[i]$表示到第$i$个位置,且该位置放了牝牛的方案数

然后两个数组可以互相推出来

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<cmath>
#define LL long long 
#define lb(x) (x & (-x))
#define Pair pair<int, int> 
#define fi first
#define se second
#define MP(x, y) make_pair(x, y)
using namespace std;
const int MAXN = 1e6 + 10, mod = 5000011;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); 
    return x * f;
}
int N, K;
int f[MAXN], g[MAXN];
int main() {
    N = read(); K = read();
    f[1] = 1; g[1] = 1;
    for(int i = 2; i <= N; i++) {
        f[i] = (f[i - 1] + g[i - 1]) % mod;
        g[i] = (f[max(i - K - 1, 1)] + g[max(i - K - 1, 0)]) % mod;
    }
    printf("%d", (f[N] + g[N]) % mod);
    return 0;
}
/*

*/
posted @ 2018-09-03 08:51  自为风月马前卒  阅读(392)  评论(0编辑  收藏  举报

Contact with me