首页 > 其他分享 >CF785D Anton and School - 2

CF785D Anton and School - 2

时间:2023-12-07 11:58:32浏览次数:32  
标签:School include CF785D int Anton sum ans aligned mod

题意

给定一个长度为 \(n\) 的括号序列,求该括号序列满足下列条件的子序列个数。

  • 长度为偶数
  • 设长度为 \(2m\),则 \(s_{1 \ldots m} =\) (,\(s_{m + 1 \ldots 2m} =\) )

Sol

设 \(i\) 为最后一个 (,\(a\) 表示 \(\sum_{j = 1} ^ {i - 1} [s_i = '(']\)。\(b\) 表示 \(\sum_{j = i + 1} ^ n [s_i = ')']\)

显然可得:

\[ \begin{aligned} ans &= \sum_{i = 1} ^ {len} \sum_{k = 0} ^ {min(a + 1, b)} C_a ^ k C_b ^ {k + 1} \end{aligned} \]

我们都知道:

\[ \begin{aligned} \sum_{j = 0} ^ {k} C_n ^ {k - i} C_m ^ i = C_{n + m}^k \end{aligned}\]

这个很显然吧,考虑组合意义。

在 \(n + m\) 个物品里面共选 \(k\) 个。

发现这两个式子长得很像。

考虑随便乱搞下。

\[ \begin{aligned} ans &= \sum_{i = 1} ^ {len} \sum_{k = 0} ^ {min(a + 1, b)} C_a ^ k C_b ^ {k + 1} \\ &= \sum_{i = 1} ^ {len} \sum_{k = 0} ^ {min(a + 1, b)} C_a ^ {a - k} C_b ^ {k + 1} \\ &= \sum_{i = 1} ^ {len} C_{a + b} ^ {a + 1} \end{aligned} \]

这道题就做完了,时间复杂度 \(O(n)\)。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#define int long long
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
string read_() {
	string ans;
	char c = getchar();
	while (c != '(' && c != ')')
		c = getchar();
	while (c == '(' || c == ')') {
		ans += c;
		c = getchar();
	}
	return ans;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}

const int N = 2e5 + 5, mod = 1e9 + 7;

namespace Mth {

array <int, N> fac, inv;

int pow_(int x, int k, int p) {
	int ans = 1;
	while (k) {
		if (k & 1) ans = ans * x % p;
		x = x * x % p;
		k >>= 1;
	}
	return ans;
}

void init() {
	fac[0] = 1; int n = 2e5;
	for (int i = 1; i <= n; i++)
		fac[i] = fac[i - 1] * i % mod;
	inv[n] = pow_(fac[n], mod - 2, mod);
	for (int i = n; i; i--)
		inv[i - 1] = inv[i] * i % mod;
}

int C(int n, int m) {
	if (n < m) return 0;
	return fac[n] * inv[m] % mod * inv[n - m] % mod;
}

void Mod(int& x) {
	if (x >= mod) x -= mod;
	if (x < 0) x += mod;
}

}

array <int, N> h;

signed main() {
	string s = " " + read_();
	int n = s.size() - 1;
	for (int i = n; i; i--)
		h[i] = h[i + 1] + (s[i] == ')');
	Mth::init();
	int ans = 0, tp = 0;
	for (int i = 1; i <= n; i++) {
		if (s[i] == ')') continue;
		ans += Mth::C(tp + h[i], tp + 1), Mth::Mod(ans);
		tp++;
	}
	write(ans), puts("");
	return 0;
}

标签:School,include,CF785D,int,Anton,sum,ans,aligned,mod
From: https://www.cnblogs.com/cxqghzj/p/17881366.html

相关文章

  • Antonio_ice_room
    |工程概论|<班级链接>||-----------------|---------------||作业要求|<作业要求链接>||这个作业的目标|养成记录学习过程习惯|自我介绍:普普通通的大学生,没有什么特别的爱好,求学生涯至今毫无波澜,希望未来能有一些起色值得一提的事情:后悔没有在大一的时候就加......
  • 443A - Anton and Letters
    A.AntonandLettershttps://codeforces.com/problemset/problem/443/ARecently,Antonhasfoundaset.ThesetconsistsofsmallEnglishletters.Antoncarefullywroteoutallthelettersfromthesetinoneline,separatedbyacomma.Healsoaddedanope......
  • School Manners
      SchoolManners       Mannersintheschoolroom,aseverywhereareimportanttohappyrelationswiththegroup.WesternmannersheredifferonlyslightlyfromgoodChinesemanners. GreetingtheTeacher        Ifyouareinv......
  • 2019考研英语(一)小作文真题解析与参考 Aiding Rural Primary Schools 答复信
    2019考研英语(一)小作文真题解析与参考Directions:Supposeyouareworkingforthe“AidingRuralPrimarySchools” projectofyouruniversity.Writeanemailtoanswertheinquiryfromaninternationalschoolvolunteer,specifyingthedetailsoftheproject.Yous......
  • https://www.w3cschool.cn/weixinapp/
    框架为开发者提供了一系列基础组件,开发者可以通过组合这些基础组件进行快速开发。详细介绍请参考组件文档。什么是组件:组件是视图层的基本组成单元。组件自带一些功能与微信风格一致的样式。一个组件通常包括 开始标签 和 结束标签,属性 用来修饰这个组件,内容 在两个标签......
  • w3school
    http://www.runoob.com/w3cnote_genre/androidhttps://www.tutorialspoint.com/android/android_sqlite_database.htm http://www.w3school.com.cn/https://www.journaldev.com/10439/android-butterknife-examplehttps://code.tutsplus.com/tutorials/quick-tip-using-butter......
  • Python 报错“TypeError: School() takes no arguments”
    Python报错“TypeError:School()takesnoarguments”现象  原因__init__输入成__int__  解决方案   ......
  • Component name “School“ should always be multi-word
    脚手架报错:Componentname“School”shouldalwaysbemulti-word原因是语法检测的问题,只要关闭语法检测就可以了。解决方法:在项目目录里找到vue.config.js关闭语法检......
  • Codeforces Round #723 (Div. 2)D. Kill Anton 贪心,逆序对个数
    problemD.KillAntontimelimitpertest2secondsmemorylimitpertest512megabytesinputstandardinputoutputstandardoutputAfterrejecting10100datastruct......
  • PHP的验证码实现(w3schools推荐)
    本文使用PHP一些可用的特性实现了验证码功能。该教程非常的简单,使用可以改变的字体生成了验证码图片,正如我们所了解的,验证码是用于避免垃圾评论或者自动提交的。本验证码程......