2019牛客暑期多校训练营(第一场)E : ABBA

15 篇文章 0 订阅

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 .

样例1 2时 13种情况

ABABAB ABABBA ABBAAB ABBABA ABBBAA BAABAB BAABBA BABAAB BABABA BABBAA BBAAAB BBAABA BBABAA

思路: 设d[i][j] 表示当前i个A,j个B的合法方案数。 从上面13种情况可以发现, 放的过程种合法的方案都是 cntA - cntB ≤ n, cntB - cntA ≤ m。 A优先用于AB的A, 剩余的B = cntB - cntA, 只能组成BA, 但是数目要≤m, B优先用于BA的B, 剩余的A = cntA - cntB个A只能组成AB, 数目要求≤n。去除非法状态, 每种状态就由A - 1, B - 1两个状态转移。

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define sz(a) ((int)(a).size())
typedef long long ll;
bool cmp(int a, int b) { return a > b; }
#define me(a, b) (memset(a, b, sizeof a))
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1);
const int N = 2e4 + 10;
template<typename S, typename T> inline bool Min(S &a, const T &b){ return a > b ? a = b, true : false; }
template<typename S, typename T> inline bool Max(S &a, const T &b){ return a < b ? a = b, true : false; }
template<typename S, typename T> inline void Adm(S &a, const T &b){ a = (a + b) % mod; if (a < 0) a += mod; }
template<typename S, typename T> inline void Mum(S &a, const T &b){ a = 1LL * a * b % mod; }
template<typename T> inline T gcd(T a, T b){ while (b){ T t = b; b = a % b; a = t; }return a; }
template<typename T> inline int bcnt(T x){ int cnt = 0; while (x)++cnt, x &= x - 1; return cnt; }
inline ll fpow(ll a, ll b, ll mod) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; }
#define bo bool operator < (const node &oth)const
#define AC return 0;

int a[N];
int d[N][N];
int main(){
	int n, m;
	while (cin >> n >> m)
	{
		for (int i = 0; i <= n + m; i++)
			for (int j = 0; j <= n + m; j++)
				d[i][j] = 0;
		d[0][0] = 1;
		for (int i = 0; i <= (n + m); i++)
		{
			for (int j = 0; j <= (n + m); j++)
			{
			
				if (i - j > n || j - i > m)
					continue;
				Adm(d[i][j], d[i - 1][j] + d[i][j - 1]);
			}
		}
		cout << d[n + m][n + m] << endl;
	}
	AC
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值