2019 cattle off more school first question ABBA E

Topic Link

Portal

Thinking

First, we know that \ ( 'A' \) in place of \ (n-\) position which is not binding, \ ( 'B' \) in place of \ (m \) positions there is no constraint, other . see the discussion below.
\ (dp [i] [j ] \) represents put \ (I \) a \ ( 'A' \) and (J \) \ a \ ( 'B' \) program number, and then consider the transfer to the next a state:

  • If \ (I \ n-Leq \) , then the \ ( 'A' \) can take place;
  • If \ (J \ Leq m \) , then the \ ( 'B' \) can take place;
  • If \ (I> n-\) , then to put \ ( 'A' \) need to put \ ( 'A' \) after excess \ ( 'A' \) using a preceding \ ( 'B' \) and it matches, that \ (Ni-1 \ Leq J \) ;
  • If \ (J> m \) , then to put \ ( 'B' \) need to put \ (\ 'B') after excess \ ( 'B' \) preceded by \ ( 'A' \) and its match, that \ (NJ-1 \ Leq i \) .

Code to achieve the following

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)

const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int n, m;
LL dp[2007][2007];

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;
            }
        }
        dp[0][0] = 1;
        for(int i = 0; i <= n + m; ++i) {
            for(int j = 0; j <= n + m; ++j) {
                if(i < n + j) dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % mod;
                if(j < m + i) dp[i][j+1] = (dp[i][j+1] + dp[i][j]) % mod;
            }
        }
        printf("%lld\n", dp[n+m][n+m]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Dillonh/p/11210778.html