2019牛客暑期多校训练营(第一场)E ABBA (贪心, DP)

链接:https://ac.nowcoder.com/acm/contest/881/E
来源:牛客网
 

题目描述

Bobo has a string of length 2(n + m) which consists of characters `A` and `B`. The string also has a fascinating property: it can be decomposed into (n + m) subsequences of length 2, and among the (n + m) subsequences n of them are `AB` while other m of them are `BA`.

Given n and m, find the number of possible strings modulo (109+7)(109+7).

输入描述:

The input consists of several test cases and is terminated by end-of-file.

Each test case contains two integers n and m.

* 0≤n,m≤1030≤n,m≤103
* There are at most 2019 test cases, and at most 20 of them has max{n,m}>50max{n,m}>50.

输出描述:

For each test case, print an integer which denotes the result.

示例1

输入

复制

1 2
1000 1000
0 0

输出

复制

13
436240410
1

思路:首先需要贪心地想:最终组成的字符串中,前n个A一定是AB中的A。

原因是:如果前n个A中的A是BA中的A,那么它可以在后m个A中找一个替代这个A,使这个A变成AB中的A。

前m个B同理。

由此可以考虑到二维dp[i][j],i,j表示当前有i个A,j个B。

当A的个数小于n时,可以直接在已有序列最后放入一个A;

当A的个数大于n时,必须要求j>i-n时才能再放入一个A,举个例子说明:当前放入了n个A,0个B,如果此时再放入A,那么这个A只能结合成AB,最终数列里会有n+1个AB,所以此时必须放入一个B,才能再放入A使其结合成BA才是合法的;

代码实现时,这两种情况可以合并。

考虑放入B时同理。

AC代码:

//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <string.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define mp Debug(x) printf("%d\n", &x);
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9+7;
const int maxn = 2e3+50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
//typedef vector<ll> vec;
//typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0;
    while (c != '-' && (c<'0' || c>'9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}

int n, m;
int dp[maxn][maxn];
int main(){
    std::ios::sync_with_stdio(false);
    while(cin >> n >> m)
    {
        rep(i, 0, n+m){
            rep(j, 0, n+m){
                dp[i][j] = 0;
            }
        }
        dp[0][0] = 1;
        rep(i, 0, n+m){
            rep(j, 0, n+m){
                if(i-n < j){
                    dp[i+1][j] += dp[i][j];
                    dp[i+1][j] %= MOD;
                }
                if(j-m < i){
                    dp[i][j+1] += dp[i][j];
                    dp[i][j+1] %= MOD;
                }
            }
        }
        cout << dp[n+m][n+m] << endl;
    }

    return 0;
}
/*
6
3 1 5 2 4 6
6 3 4 5 2 1
*/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值