http://codeforces.com/gym/100633/problem/B

B. Dispersed parentheses
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The sequence of calculations in arithmetic expressions is usually set by a certain arrangement of parentheses. For example, (3·(2 + 1))·(4 - 5). After deleting all the elements from the expression except parentheses remaining symbols form a parentheses sequence (())(). Let’s assume that adding character «0» does not corrupt the sequence. Let’s call such sequence a disperse parentheses sequence. Also this can be defined as follows:

  • An empty line is a disperse parentheses sequence.
  • If S and T — disperse parentheses sequences, then lines 0S, S0, (S) and ST are also disperse parentheses sequences.

The depth of disperse parentheses sequence is the maximum difference between the number of opening and closing parentheses in the sequence prefix. (The prefix of line S is the line, which can be obtained from S by deleting symbols from the tail of the line. For example, the prefixes of line «ABCAB» are lines «», «A», «AB», «ABC», «ABCA» and «ABCAB».) Thus, the depth of the sequence «(0)(0())0» equals two (prefix «(0)(0(» contains three openinig and one closing parentheses).

Calculate the number of possible disperse parentheses sequences n symbols long, that have a depth k.

Input

Single line contains space-separated integers n and k (1 ≤ n ≤ 300, 0 ≤ k ≤ n).

Output

Output the number of possible disperse parentheses sequences n symbols long, that have a depth k modulo (109 + 9).

Examples
Input
3 0
Output
1
Input
3 1
Output
3
Input
3 2
Output
0

这里学到了一个括号序列的状压表示,也就是,要表示一个合法的括号序列,除了用[lef][rig]表示有开括号lef个,
闭括号rig个之外,还可以用她们的差来表示,直接压缩到一维,[dis]表示两种括号的差值。可知当rig > lef的时候,整个序列是不可能合法的。所以dis < 0直接是0.
那么设dp[n + 1][dis][k]表示长度是n的括号序列,差值是dis,深度是k的合法情况。
ans = dp[n + 1][0][k] 那么可以按位dfs,开始的状态是dis = 0, deep = 0, lef = 0, rig = 0,那么在第cur位,你可以放0,可以放'('或者')'。然后三种情况统计一次的和,就是第cur位的合法情况。 一开始从dp[1][0][0]出发,推导dp[n + 1][0][k]
dp[1][0][0]的意思是在第0位,差值是0,深度是k的合法情况数。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset> int n, k;
const int MOD = 1e9 + ;
const int maxn = + ;
bool vis[maxn][maxn][maxn];
int dp[maxn][maxn][maxn];
int dfs(int cur, int dis, int deep, int lef, int rig) {
if (dis < || deep > k) return ;
if (vis[cur][dis][deep]) return dp[cur][dis][deep];
vis[cur][dis][deep] = true;
if (cur == n + ) {
if (dis == && deep == k) {
return dp[cur][dis][deep] = ;
} else return ;
}
LL ans = ;
ans += dfs(cur + , dis, deep, lef, rig);
ans += dfs(cur + , dis + , max(deep, lef + - rig), lef + , rig);
ans += dfs(cur + , dis - , max(deep, rig + - lef), lef, rig + );
return dp[cur][dis][deep] = ans % MOD;
}
void work() {
scanf("%d%d", &n, &k);
cout << dfs(, , , , ) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

如果不用这个括号序列的状压,则MLE。dp[n][deep][lef][rig]

2017年7月17日 20:40:26

现在来看这题。

其实是从第一位开始构造。

dp[i][j][k]

表示构造了i个,左括号和右括号差值是j,现在深度是k,在这个状态下,后面那些序列随便变,最后得到的合法情况数会是dp[i][j][k]

整个dfs的过程相当于在第cur位放什么的过程。

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; const int MOD = 1e9 + ;
LL dp[][][];
bool vis[][][];
int n, k;
LL dfs(int cur, int dis, int depth) {
if (depth > k) return ;
if (cur == n + ) {
if (dis == && depth == k) return ;
else return ;
}
if (vis[cur][dis][depth]) return dp[cur][dis][depth];
vis[cur][dis][depth] = true;
LL ans1 = dfs(cur + , dis, depth);
ans1 += dfs(cur + , dis + , max(dis + , depth));
if (dis > ) {
ans1 += dfs(cur + , dis - , depth);
}
return dp[cur][dis][depth] = ans1 % MOD;
}
void work() {
cin >> n >> k;
cout << dfs(, , ) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

B. Dispersed parentheses 记忆化搜索 + 括号序列的状压表示的更多相关文章

  1. [bzoj2461][BeiJing2011][符环] (括号配对+记忆化搜索+高维dp)

    Description 在可以炼制魔力强大的法杖的同时,Magic Land 上的人们渐渐意识到,魔力强大并不一定能给人们带来好处——反而,由此产生的破坏性的高魔力释放,给整个大陆蒙上了恐怖的阴影.  ...

  2. trie上记忆化搜索,括号匹配——cf1152D好题!

    一开始以为是卡特兰数的性质,,后来发现其实是dp,但是用记忆化搜索感觉更方便一点先来考虑字典树上的问题 设要求的序列长度是2n,我们用二元组(a,b)来表示前面长为a的序列中出现的 '(' - ')' ...

  3. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索

    https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...

  4. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  5. HDU 4597 Play Game (DP,记忆化搜索,博弈)

    题意:Alice和Bob玩一个游戏,有两个长度为N的正整数数字序列,每次他们两个,只能从其中一个序列,选择两端中的一个拿走.他们都希望可以拿到尽量大的数字之和, 并且他们都足够聪明,每次都选择最优策略 ...

  6. CodeForces 132C Logo Turtle (记忆化搜索)

    Description A lot of people associate Logo programming language with turtle graphics. In this case t ...

  7. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  8. 专题1:记忆化搜索/DAG问题/基础动态规划

      A OpenJ_Bailian 1088 滑雪     B OpenJ_Bailian 1579 Function Run Fun     C HDU 1078 FatMouse and Chee ...

  9. CF1028G Guess the Numbers 构造、记忆化搜索

    传送门 考虑如果我们当前可以询问\(x\)个数,还剩下\(q\)次询问机会,我们要怎么构造询问方式? 肯定会这么考虑: 找到一个尽可能大的\(P\)满足\([x,P]\)能在每一次能询问\(x\)个数 ...

随机推荐

  1. codeforces A. Black-and-White Cube 解题报告

    题目链接:http://codeforces.com/problemset/problem/323/A 题目意思:给定值 k ,需要输出 k 个 k 行 k 列的单位立方体各表示什么颜色(或者是黑色或 ...

  2. application 长用到的API

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. 盈创动力之 JS校验方法

    var IS_NULL = 128; // 10000000var IS_FULL = 64; // 01000000var IS_HALF = 32; // 00100000var IS_ASCII ...

  4. linux安装netcat 运行udp服务器

    liunx下安装netcat 1.下载安装包 wget https://sourceforge.net/projects/netcat/files/netcat/0.7.1/netcat-0.7.1. ...

  5. UVA-11892(组合游戏)

    题意: 给n堆石子,每堆有ai个,每次可以取每堆中任意数目的石子;但是上一次操作的人没有将一堆全部取走,那么下一个人还要在那一堆取; 思路: 每次取到这堆就剩一个的策略; AC代码: #include ...

  6. 002 - 配置Pycharm的背景颜色为豆沙绿护眼

    本文记录的是Pycharm2017年1月版本 Pycharm有自带的主题可供选择, 好多小伙伴喜欢使用Monoka的风格 但是也有不是很喜欢黑底风格的小伙伴可以使用默认主题+豆沙绿来替代 默认主题是白 ...

  7. CTSC2012 熟悉的文章

    传送门 首先很容易想到对于所有的模式串建出广义后缀自动机,之后对于我们每一个要检查的文本串,先在SAM上跑,计算出来每一个位置能匹配到的最远的位置是多少.(就是当前点减去匹配长度) 之后--考虑DP- ...

  8. 给DataTable中添加一行数据

    一.如果该DataTable有两列,列的名称是Name,Age,且该DataTable的名称是dt; DataRow dr = dt.NewRow(); dr["Name"] = ...

  9. Ubuntu下使用gcc编译c文件,未识别cos,sin

    Ubuntu下使用gcc编译c文件,虽然我调用了math.h的头文件,但是未识别cos,sin 报错:( fft.c ) /tmp/ccwXjD8C.o: In function `fft': fft ...

  10. project工期出现小数问题

    进入“选项”,点击“日程”,将默认“开始”.“结束”时间调整为“9点~18点.原为PREJECT 日历 (注意是日历~!!)默认的“8点~17点”,与preject系统默认时间“9点~18点”有差别~ ...