CF1264D2 Beautiful Bracket Sequence (hard version)
考虑\(D1\)的\(O(n^2)\),我们直接进行组合处理。
考虑在\(p\)这个位置,左边有\(l\)个(,右边有\(r\)个),左边有\(l\)个问号,右边有\(r\)个问号。
这个位置的贡献为:
\(\sum_{i = 0} ^ x (l+i)\binom{x}{i}\binom{y}{l + i - r}\)
考虑我们拆项。
\(l\sum_{i = 0} ^ x\binom{x}{i}\binom{y}{l + i - r} + \sum_{i = 0} ^ x i\binom{x}{i}\binom{y}{l + i - r}\)
考虑前者可以写作\(l\binom{x + y}{r + y - l}\)
后者把\(i\)吸收进去,\(x\)提取出来
\(x\binom{x + y - 1}{r + y - l - 1}\)
那么一个点的贡献是\(l\binom{x + y}{r + y - l} + x\binom{x + y - 1}{r + y - l - 1}\)
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
#define N 1000005
#define mod 998244353
ll l[N],r[N],cnt[N];
char a[N];
ll s[(N << 1) + 10],inv[(N << 1) + 10];
inline ll pow(ll a,ll b){
ll ans = 1;
while(b){
if(b & 1)ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
inline ll C(ll x,ll y){
if(x < y || x < 0 || y < 0)return 0;
return s[x] * inv[y] % mod * inv[x - y] % mod;
}
int main(){
scanf("%s",a + 1);
ll n = std::strlen(a + 1);
s[0] = 1;
for(int i = 1;i <= N << 1;++i)
s[i] = s[i - 1] * i % mod;
inv[N << 1] = pow(s[N << 1],mod - 2);
for(int i = (N << 1) - 1;i >= 0;--i)
inv[i] = (i + 1) * inv[i + 1] % mod;
l[0] = r[0] = cnt[0] = 0;
for(int i = 1;i <= n;++i){
l[i] = l[i - 1];
r[i] = r[i - 1];
cnt[i] = cnt[i - 1];
if(a[i] == '(')
l[i] ++ ;
if(a[i] == ')')
r[i] ++ ;
if(a[i] == '?')
cnt[i] ++ ;
}
ll ans = 0;
for(int i = 1;i <= n;++i){
ll li = l[i] - l[0];
ll ri = r[n] - r[i];
ll x = cnt[i] - cnt[0];
ll y = cnt[n] - cnt[i];
ans = (ans + li * C(x + y,ri + y - li) % mod + x * C(x + y - 1,ri + y - li - 1) % mod) % mod;
}
std::cout<<ans<<std::endl;
}
CF1264D2 Beautiful Bracket Sequence (hard version)的更多相关文章
- CF1264D2 Beautiful Bracket Sequence
我们枚举每两个字符的空档,统计一个空档左边有 \(l\) 个左括号, 右边有 \(r\) 个右括号,左边有 \(u\) 个问号,右边有 \(v\) 个问号. 则对于 \(p\) 的答案 \(ans_p ...
- CF1264D1 Beautiful Bracket Sequence (easy version)
考虑在一个确定的括号序列中,我们可以枚举中间位置,按左右最长延伸出去的答案计算. 我们很自然的思考,我们直接维护左右两边,在删除一些字符后能够延伸的最长长度. 我们设\(f_{i,j}\)为\(i\) ...
- Codeforces 1264D - Beautiful Bracket Sequence(组合数学)
Codeforces 题面传送门 & 洛谷题面传送门 首先对于这样的题目,我们应先考虑如何计算一个括号序列 \(s\) 的权值.一件非常显然的事情是,在深度最深的.是原括号序列的子序列的括号序 ...
- UESTC 1546 Bracket Sequence
Bracket Sequence Time Limit: 3000MS Memory Limit: 65536KB 64 ...
- CF#138 div 1 A. Bracket Sequence
[#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...
- CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...
- Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈
C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...
- Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp
C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- (中等) UESTC 94 Bracket Sequence,线段树+括号。
There is a sequence of brackets, which supports two kinds of operations. we can choose a interval [l ...
随机推荐
- C++ 与 Visual Studio 2019 和 WSL(二)
终端 A more integrated terminal experience | Visual Studio Blog (microsoft.com) Say hello to the new V ...
- 【数据结构】<栈的应用>回文判断
通过栈与队列相关内容的学习,我们知道,栈是"先进后出"的线性表,而队列是"先进先出"的线性表.可以通过构造栈与队列来实现在这一算法.将要判断的字符序列依次压栈和 ...
- javascript-vue介绍
vue.js是一个用于创建web交互页面的库 从技术角度讲,vue专注于MVVM模型的viewModel层,它通过双向数据绑定把view层和model层连接起来,实际DOM封装和输出格式都被抽象为Di ...
- vue3.x移动端页面基于vue-router的路由切换动画
移动端页面切换一般都具有动画,我们既然要做混合开发,做完之后还是不能看起来就像一个网页,所以我们基于vue-router扩展了一个页面切换push和pop的动画.这是一篇比较硬核的帖子,作者花了不少精 ...
- AtCoder Beginner Contest 224
AtCoder Beginner Contest 224 A - Tires 思路分析: 判断最后一个字符即可. 代码如下: #include <bits/stdc++.h> using ...
- Java:线程池
Java:线程池 本笔记是根据bilibili上 尚硅谷 的课程 Java大厂面试题第二季 而做的笔记 获取多线程的方法: 实现 Runnable 接口 实现 Callable 接口 实例化 Thre ...
- 移动端 h5 uniapp 读,写,删本地文件或sd文件
移动端 h5 uniapp 读,写,删本地文件或sd文件 应用场景: 当我们需要做离线应用或者是加载本地文件时使用到此方法.(本篇文章给大家分享访问app私有文件目录,系统公共目录,sd外置存储的文件 ...
- [技术博客]OKhttp3使用get,post,delete,patch四种请求
OKhttp3使用get,post,delete,patch四种请求 1.okhttp简介 okhttp封装了大量http操作,大大简化了安卓网络请求操作,是现在最火的安卓端轻量级网络框架.如今okh ...
- Noip模拟79 2021.10.17(题目名字一样)
T1 F 缩点缩成个$DAG$,然后根据每个点的度数计算期望值 1 #include<cstdio> 2 #include<cstring> 3 #include<vec ...
- Machine learning(4-Linear Regression with multiple variables )
1.Multiple features So what the form of the hypothesis should be ? For convenience, define x0=1 At t ...