2019牛客多校第一场E ABBA(DP)题解
链接:https://ac.nowcoder.com/acm/contest/881/E
来源:牛客网
ABBA
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
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
(
10
9
+
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
≤
10
3
0≤n,m≤103
- There are at most 2019 test cases, and at most 20 of them has
max
{
n
,
m
}
50
max{n,m}>50.
输出描述:
For each test case, print an integer which denotes the result.
示例1
输入
复制
1 2
1000 1000
0 0
输出
复制
13
436240410
1
题意:
问给你长度为2*(n+m)的字符串,由n+m个‘A'和’B'组成,要求有n个AB子序列,和m个BA子序列,这样的串有多少种 ?
思路:
先看一个合法串什么什么样的,因为子序列有n个AB,m个BA,那么显然前n个A必为AB的A,前m个B必为BA的B,因为如果我前n个A中有一个是BA的A,那么我们可以从更后面 随便找一个A给这个B用。
定义dp状态: dp[i][j] 为放了i个A,j个B,合法的状态数。
来看转移:
放A:
如果i < n 那么可以直接放这个A,理由如上。
如果i>=n 那么我们要确保 这个放的A能给前面的B当作BA中的A用,那么当前我们BA需要的A是min(j,m) 个
已经给了i-n个,故如果(i-n)<min(j,m) 还可以继续放A
B 同理:
如果j< m 直接放这个B
如果j > = m ,那么我们要确保 放这个B能给前面的一个A当作AB中的B用,那么我们AB需要的B是 min(i,n )个
已经放了 j-m 个,如果(j-m) <min(i,n) 就可以继续放这个B
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll dp[3000][2005];
const ll mod = 1e9 + 7;
int n, m;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
while (cin >> n >> m) {
repd(i, 0, n + m) {
repd(j, 0, n + m) {
dp[i][j] = 0;
}
}
dp[0][0] = 1;
repd(i, 0, n + m) {
repd(j, 0, m + n) {
if (i < n || i - n < j) {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j] %= mod;
}
if (j < m || (j - m) < i) {
dp[i][j + 1] += dp[i][j];
dp[i][j + 1] %= mod;
}
}
}
cout<<dp[n+m][n+m]<<endl;
}
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
2019牛客多校第一场E ABBA(DP)题解的更多相关文章
- 2019牛客多校第一场E ABBA dp
ABBA dp 题意 给出2(N+M)个AB字符,问能构造出N个AB子序列和M个BA子序列组成的2*(n+m)的序列种类有多少 思路 碰到计数构造类的题目,首先要去找到判断合法性的条件,即什么情况下合 ...
- 2019牛客多校第一场 E-ABBA(dp)
ABBA 题目传送门 解题思路 用dp[i][j]来表示前i+j个字符中,有i个A和j个B的合法情况个数.我们可以让前n个A作为AB的A,因为如果我们用后面的A作为AB的A,我们一定也可以让前面的A对 ...
- 2019牛客多校第一场E ABBA 贪心 + DP
题意:问有多少个有(n + m)个A和(n + m)个B的字符串可以凑出n个AB和m个BA. 思路:首先贪心的发现,如果从前往后扫,遇到了一个A,优先把它看成AB的A,B同理.这个贪心策略用邻项交换很 ...
- 2019 牛客多校第一场 E ABBA
题目链接:https://ac.nowcoder.com/acm/contest/881/E 题目大意 问有多少个由 (n + m) 个 ‘A’ 和 (n + m) 个 ‘B’,组成的字符串能被分割成 ...
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
- 2019牛客多校第一场A-Equivalent Prefixes
Equivalent Prefixes 传送门 解题思路 先用单调栈求出两个序列中每一个数左边第一个小于自己的数的下标, 存入a[], b[].然后按照1~n的顺序循环,比较 a[i]和b[i]是否相 ...
- 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 ...
- 2019 牛客多校第一场 D Parity of Tuples
题目链接:https://ac.nowcoder.com/acm/contest/881/D 看此博客之前请先参阅吕凯飞的论文<集合幂级数的性质与应用及其快速算法>,论文中很多符号会被本文 ...
- 【2019牛客多校第一场】XOR
题意: 给你一个集合A,里边有n个正整数,对于所有A的.满足集合内元素异或和为0的子集S,问你∑|S| n<=1e5,元素<=1e18 首先可以转化问题,不求∑|S|,而是求每个元素属于子 ...
随机推荐
- 在K8S上跑一个helloworld
建立docker镜像 为了方便起见,这里直接使用一个js网页作为应用,以此创建镜像 hello world网页 创建server.js,输入以下代码创建helloworld网页: var http = ...
- 集合(Java总结一)
一.Queue 一个队列就是一个先入先出(FIFO)的数据结构 1.没有实现的阻塞接口的LinkedList: 实现了java.util.Queue接口和java.util.AbstractQueue ...
- json中loads()和dumps()的应用
import json s = {'name': 'jack'} #将dict转换成strl = json.dumps(s)print(type(l)) #将str转换成dictm = json.lo ...
- mxml 嵌入as代码出错,缺少 CDATA
如果<mx:Script> 中有大于小于符号,代码必须包含在<![CDATA[ 之中,否则会报错
- 分布式任务celery
Celery的架构由三部分组成,消息中间件(message broker),任务执行单元(worker)和任务执行结果存储(task result store)组成. 消息中间件 Celery本身不提 ...
- js脚本实现在该界面直接跳转到一个登录界面并且自动登录
1:首先说明的是自动登录也是需要密码的,这是前一个网页传输过去的 2:这里我使用的是post提交表单的形式 <------------------------------------------ ...
- Java不可变序列String和可变序列StringBuilder、StringBuffer
String String变量是不可变的,源码里面用了final修饰. private final char value[]; String str = "Hello"; Syst ...
- 【Deep Learning Nanodegree Foundation笔记】第 9 课:Model Evaluation and Validation
In this lesson, you'll learn some of the basics of training models. You'll learn the power of testin ...
- 黑龙江网络安全技能竞赛awd后门分析复现
0x0环境 0x1分析复现 0x2感想 围绕主办方留下的浅显后门可以打满整场,想拿第一还是要搞定深层后门
- mysql 关键字大全
mysql无论表名,还是字段名都应该避开mysql关键字. 如字段使用关键字,sql查询需加上` `. 查询插件,当使用关键字,会报错. usage