• 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;
    }
    /*
    
    */
  • 相关阅读:
    【模板小程序】链表排序(qsort/insert_sort/merge_sort)
    链表回文判断(C++)
    【模板小程序】十进制大数除法(输出商和余数)
    【模板小程序】字符串截取
    【模板小程序】翻转一个句子中的单词
    web前端基础背景
    MongoDB基本知识(补充)
    Python-ORM
    Python-元编程
    ElementUI 中 el-table 获取当前选中行的index
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9576824.html
Copyright © 2020-2023  润新知