考虑\(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)的更多相关文章

  1. CF1264D2 Beautiful Bracket Sequence

    我们枚举每两个字符的空档,统计一个空档左边有 \(l\) 个左括号, 右边有 \(r\) 个右括号,左边有 \(u\) 个问号,右边有 \(v\) 个问号. 则对于 \(p\) 的答案 \(ans_p ...

  2. CF1264D1 Beautiful Bracket Sequence (easy version)

    考虑在一个确定的括号序列中,我们可以枚举中间位置,按左右最长延伸出去的答案计算. 我们很自然的思考,我们直接维护左右两边,在删除一些字符后能够延伸的最长长度. 我们设\(f_{i,j}\)为\(i\) ...

  3. Codeforces 1264D - Beautiful Bracket Sequence(组合数学)

    Codeforces 题面传送门 & 洛谷题面传送门 首先对于这样的题目,我们应先考虑如何计算一个括号序列 \(s\) 的权值.一件非常显然的事情是,在深度最深的.是原括号序列的子序列的括号序 ...

  4. UESTC 1546 Bracket Sequence

                                        Bracket Sequence Time Limit: 3000MS   Memory Limit: 65536KB   64 ...

  5. CF#138 div 1 A. Bracket Sequence

    [#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...

  6. CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)

    E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...

  7. 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 ...

  8. 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 ...

  9. (中等) UESTC 94 Bracket Sequence,线段树+括号。

    There is a sequence of brackets, which supports two kinds of operations. we can choose a interval [l ...

随机推荐

  1. Java(20)参数传递之类名、抽象类、接口

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201632.html 博客主页:https://www.cnblogs.com/testero ...

  2. scrapy爬虫简单项目入门练习

    [写在开头] scrapy环境配置配置好了之后,开始着手简单项目入门练习.关于环境配置见上一篇博客https://www.cnblogs.com/ljxh/p/11235079.html. [正文部分 ...

  3. 浏览器有别_HTTP报文的回车换行

    本来以为浏览器HTTP报文的生成应该是完全一致的.但最近在做一个项目的时候,发现Safari和Chrome提交同一份表单,后端的处理结果不一致.看提交结果呢,是因为Safari多了个回车.由于原项目的 ...

  4. 2021.7.21考试总结[NOIP模拟22]

    终于碾压小熠了乐死了 T1 d 小贪心一波直接出正解,没啥好说的(bushi 好像可以主席树暴力找,但我怎么可能会呢?好像可以堆优化简单找,但我怎么可能想得到呢? 那怎么办?昨天两道单调指针加桶,我直 ...

  5. Verdi Transaction Debug Mode 简单使用

    转载:Verdi Transaction Debug Mode 简单使用_Holden_Liu的博客-CSDN博客 文档与源码: User Guide: Verdi_Transaction_and_P ...

  6. PCIE学习链接集合

    <PCIE基础知识+vivado IP core设置> https://blog.csdn.net/eagle217/article/details/81736822 <一步一步开始 ...

  7. Python matplotlib 概率论与数理统计 伯努利分布 二项分布

    Python 代码实现 二项分布 import numpy as np import matplotlib.pyplot as plt import math from scipy import st ...

  8. DeWeb和WebXone的区别

    DeWeb和WebXone的区别 相同点: 1 两者为同一开发者研发.QQ:45300355,碧树西风 2 都是为了解决Delphi开发Web的问题 区别: 1 WebXone采用的ActiveX/N ...

  9. CSS学习笔记:浮动属性

    目录 一.浮动流是什么 二.通过代码实例了解浮动特点 1. 搭建测试框架 2. 添加浮动 3. 浮动元素的排布 4. 给行内元素添加浮动效果 5. 子元素浮动后对父元素的影响 5.1 在父元素中添加o ...

  10. 负载均衡算法WRR介绍

    一.负载均衡 负载均衡是一个很大的概念,既有从硬件层面来解决问题的,又有从软件层面解决的,有关负载均衡的介绍,推荐阅读: http://os.51cto.com/art/201108/285359.h ...