zoukankan      html  css  js  c++  java
  • 2019牛客暑期多校训练营(第一场)- E ABBA

    dp

    dp[i][j]表示前i+j个字符中放了i个A和j个B的方法数。

    我们可以贪心的先把前n个A都作为AB的A,前m个B都作为BA的B,这样显然是不影响答案的正确性的,因为假设前n个A中有一个是BA的A,那么我们一定可以在更后面找到一个A来代替当前的A成为BA的A,B的情况也同理。

    因此我们对于A,只要放的A的数量小于n + min(j, m),其中j为放的B的数量,m为BA中的B的数量,n为AB中A的数量,那我们可以直接相加方法数当成状态的转移,对于放B的情况也同理。

    dp的边界是我们在前n个位置可以直接放A,也可以在前m个位置直接放B。

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    #define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int ret = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar();
        return w ? -ret : ret;
    }
    inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template <typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template <typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template <typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 2005;
    const int MOD = 1e9 + 7;
    int n, m, dp[N<<1][N<<1];
    
    int main(){
    
        while(~scanf("%d%d", &n, &m)){
            for(int i = 0; i <= n + m; i ++)for(int j = 0; j <= n + m; j ++) dp[i][j] = 0;
            for(int i = 0; i <= n; i ++) dp[i][0] = 1;
            for(int i = 0; i <= m; i ++) dp[0][i] = 1;
            for(int i = 1; i <= n + m; i ++){
                for(int j = 1; j <= n + m; j ++){
                    if(i <= n + min(j, m)) dp[i][j] = (dp[i][j] % MOD + dp[i - 1][j] % MOD) % MOD;
                    if(j <= m + min(i, n)) dp[i][j] = (dp[i][j] % MOD + dp[i][j - 1] % MOD) % MOD;
                }
            }
            printf("%d
    ", dp[n + m][n + m] % MOD);
        }
        return 0;
    }
    
  • 相关阅读:
    sort
    usaco-3.1-humble-pass
    usaco-3.1-inflate-pass
    usaco-3.1-agrinet-pass
    usaco-2.4-fracdec-pass
    usaco-2.4-comhome-pass
    usaco-2.4-cowtour-pass
    usaco-2.4-maze1-pass
    usaco-2.4-ttwo-pass
    usaco-2.3-concom-pass
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/11215180.html
Copyright © 2011-2022 走看看