题目链接:https://ac.nowcoder.com/acm/contest/881/E

题目大意

  问有多少个由 (n + m) 个 ‘A’ 和 (n + m) 个 ‘B’,组成的字符串能被分割成 (n + m) 个长度为 2 的子序列,其中恰好有 n 个 “AB”,和 m 个 “BA”。

分析1(DP)

  首先,如果一个串是合法的,那么我们可以用贪心的思路找到一种必然正确的划分子序列的方法:前 n 个 A 分配给 AB,后 m 个 A 分配给 BA,B 同理。
  设 dp[i][j] 表示有 i 个 A 和 j 个 B 的合法前缀方案数。
  对于每一个前缀合法字符串,它下一个长度的前缀合法字符串有 2 种选择,在结尾加 A 或是加 B,为什么不在中间加?因为中间加的情况可以转化为另一个前缀合法字符串在结尾加字符,所以只要讨论在结尾加即可。
  接下来讨论一下不合法情况,由于是在结尾加,比如说加 A,在加之前,A 的数量不能超过 B 的数量加 n,不然的话会出现多余的 A。B 同理。
  最后就是状态转移方程了:
  1. dp[i][j] 由末尾加 A 得到,所以 dp[i][j] = dp[i][j] + dp[i - 1][j]。
  2. dp[i][j] 由末尾加 B 得到,所以 dp[i][j] = dp[i][j] + dp[i][j - 1]。
  3. dp[i][j] 不合法,为 0。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T>
ostream &operator<<(ostream &out, vector<T> &v) {
Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
return out;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 2e3 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; // dp[i][j] 表示有 i 个 A 和 j 个 B 的合法前缀方案数
int n, m, dp[maxN][maxN]; int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
//INIT();
while(~scanf("%d%d", &n, &m)) {
dp[][] = ;
Rep(i, n + m + ) {
Rep(j, n + m + ) {
if(!i && !j) continue;
dp[i][j] = ;
if (i > && j >= i - n) dp[i][j] = (dp[i - ][j] + dp[i][j]) % mod; // 在最后加 A
if (j > && i >= j - m) dp[i][j] = (dp[i][j - ] + dp[i][j]) % mod; // 在最后加 B
}
} printf("%d\n", dp[n + m][n + m]);
}
return ;
}

分析2(卡特兰数的折线法)

  关于折线法:https://blog.csdn.net/qq_26525215/article/details/51453493

  一共有四种情况:

  1. m = 0 && n = 0:此时答案为 1。
  2. m > 0 && n = 0:此时退化成纯卡特兰数问题,答案为${{2m}\choose{m}} - {{2m}\choose{m - 1}}$。
  3. m = 0 && n > 0:此时退化成纯卡特兰数问题,答案为${{2n}\choose{n}} - {{2n}\choose{n - 1}}$。
  4. m > 0 && n > 0:这种是卡特兰数的变化,对应在折线图上就是从一条线变成了上下两条边界线,答案为${{2(n + m)}\choose{n + m}} - {{2(n + m)}\choose{m - 1}} - {{2(n + m)}\choose{n - 1}}$。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T>
ostream &operator<<(ostream &out, vector<T> &v) {
Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
return out;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; LL fac[maxN];
void init_fact() {
fac[] = ;
For(i, , maxN - ) fac[i] = (i * fac[i - ]) % mod;
} //ax + by = gcd(a, b) = d
// 扩展欧几里德算法
inline void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d){
if (!b) {d = a, x = , y = ;}
else{
ex_gcd(b, a % b, y, x, d);
y -= x * (a / b);
}
} // 求a关于p的逆元,如果不存在,返回-1
// a与p互质,逆元才存在
inline LL inv_mod(LL a, LL p = mod){
LL d, x, y;
ex_gcd(a, p, x, y, d);
return d == ? (x % p + p) % p : -;
} inline LL comb_mod(LL m, LL n) {
LL ret; if(m > n) swap(m, n); ret = (fac[n] * inv_mod(fac[m], mod)) % mod;
ret = (ret * inv_mod(fac[n - m], mod)) % mod; return ret;
} LL n, m, ans; int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
//INIT();
init_fact();
while(~scanf("%lld%lld", &n, &m)) {
ans = comb_mod(n + m, * (n + m));
if(n) ans -= comb_mod(n - , * (n + m));
if(m) ans -= comb_mod(m - , * (n + m));
ans = (ans + * mod) % mod;
printf("%lld\n", ans);
}
return ;
}

2019 牛客多校第一场 E ABBA的更多相关文章

  1. 2019牛客多校第一场E ABBA(DP)题解

    链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 ABBA 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语 ...

  2. 2019牛客多校第一场E ABBA 贪心 + DP

    题意:问有多少个有(n + m)个A和(n + m)个B的字符串可以凑出n个AB和m个BA. 思路:首先贪心的发现,如果从前往后扫,遇到了一个A,优先把它看成AB的A,B同理.这个贪心策略用邻项交换很 ...

  3. 2019牛客多校第一场E ABBA dp

    ABBA dp 题意 给出2(N+M)个AB字符,问能构造出N个AB子序列和M个BA子序列组成的2*(n+m)的序列种类有多少 思路 碰到计数构造类的题目,首先要去找到判断合法性的条件,即什么情况下合 ...

  4. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

  5. 2019牛客多校第一场A-Equivalent Prefixes

    Equivalent Prefixes 传送门 解题思路 先用单调栈求出两个序列中每一个数左边第一个小于自己的数的下标, 存入a[], b[].然后按照1~n的顺序循环,比较 a[i]和b[i]是否相 ...

  6. 2019牛客多校第一场 A.Equivalent Prefixes

    题目描述 Two arrays u and v each with m distinct elements are called equivalent if and only if RMQ(u,l,r ...

  7. 2019 牛客多校第一场 D Parity of Tuples

    题目链接:https://ac.nowcoder.com/acm/contest/881/D 看此博客之前请先参阅吕凯飞的论文<集合幂级数的性质与应用及其快速算法>,论文中很多符号会被本文 ...

  8. 2019牛客多校第一场 E-ABBA(dp)

    ABBA 题目传送门 解题思路 用dp[i][j]来表示前i+j个字符中,有i个A和j个B的合法情况个数.我们可以让前n个A作为AB的A,因为如果我们用后面的A作为AB的A,我们一定也可以让前面的A对 ...

  9. 【2019牛客多校第一场】XOR

    题意: 给你一个集合A,里边有n个正整数,对于所有A的.满足集合内元素异或和为0的子集S,问你∑|S| n<=1e5,元素<=1e18 首先可以转化问题,不求∑|S|,而是求每个元素属于子 ...

随机推荐

  1. js滚动到顶部底部代码

    <!DOCTYPE HTML> <html> <head> <meta charset=UTF-8> <title>SCROLL</t ...

  2. CSS template

    ylbtech-CSS3: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech.cn ...

  3. Openstack组件实现原理 — Nova 体系结构

    目录 目录 前文列表 Nova体系结构 虚拟机实例化流程 前文列表 Openstack组件部署 - Overview和前期环境准备 Openstack组建部署 - Environment of Con ...

  4. PAT_A1059#Prime Factors

    Source: PAT A1059 Prime Factors (25 分) Description: Given any positive integer N, you are supposed t ...

  5. FreeBSD_11-系统管理——{Part_1-xfce 桌面}

    一.首先安装 Xorg 安装 xorg pkg install xorg 清除旧文件(如果已前安装过 xorg) /etc/X11/xorg.conf /usr/local/etc/X11/xorg. ...

  6. 谷歌与Airbnb的JS代码规范

    谷歌JS代码规范 规范代码原因:代码规范是为了保持源代码编写模式一致,便于维护代码,可读性高. 1.使用空格代替tab 规范随后指出应该使用2个,而不是4个空格带实现缩进.(除了每一行的终止符序列,A ...

  7. Day 16 : Python 时间模块[time,]datetime[]及第三方模块的下载与安装

    在进行python程序开发时,除了可以使用python内置的标准模块外,还右许多第三方模块使用,可以在python官网找到. 在使用第三方模块时,需要下载并安装此模块,然后就可以使用标准模块一样导入并 ...

  8. 5-vim-工作模式-02-工作模式切换演练

    1.末行模式 命令 英文 功能 :   进入末行模式 w write 保存 q quit 退出,如果没有保存,不允许退出 q! quit 强行退出,不保存退出 wq write&quit 保存 ...

  9. 微信小程序支付之代码详解

    微信小程序自带的一套规则,类似vue语法,但是好多功能都集成在api中,给了很多初学者轮子,所以首先要熟悉这些api,忘记可照官网继续开发 这里主要说下微信小程序的支付,原理类似上篇介绍的公众网页支付 ...

  10. winform 窗体拖动

    winform 由于自带的界面太丑,有时候就想着去掉标题栏,自己写,自己做UI 但是发现没法拖动了,或者,有时候我们也想让整个窗体都能够随着鼠标进行拖动,来来来,看下面 #region 让窗口可以随意 ...