cf785D(组合数学)
题目链接: http://codeforces.com/problemset/problem/785/D
题意: 左边全为 '(' 右边全为 ')' 且两者数量想等的字符串称为 RSBS. 给出一个由 '(' 和 ')' 组成的字符串, 问其有多少子序列是 RSBS.
思路: 可以先预处理一下, 用 a[i] 记录 i 前面(包括 i 这个位置)的 '(' 的数目, b[i] 记录 i 后面(包括 i 这个位置)的 ')' 的数目, 然后从左往右枚举以 '(' 结尾的情况,
那么当前情况下的 RSBS 数目为:
C(a[i] - 1, 0) * C(b[i], 1) + C(a[i] - 1, 1) * C(b[i], 2) + C(a[i] - 1, 2) * C(b[i], 3) + ...
= ∑min(a-1, b-1)0 C(a - 1, x) * C(b, x + 1)
= ∑min(a-1, b-1)0 C(a - 1, a - 1 - x) * C(b, x + 1)
= C(a - 1 + b, a) (范德蒙恒等式)
然后将所有情况的 RSBS 数目累加一下就好啦.
注意这里的组合数比较大, 取模的话需要用到 exgcd 或者 快速幂.
代码1: 快速幂求组合数取模 C(n, m) % mode = (n! % mode) * get_pow((n - m)! * m! % mode, mode - 2) % mode. (这个公式能通过费马小定理变换得到).
#include <iostream>
#define ll long long
using namespace std; const int mode = 1e9 + ;
const int MAXN = 2e5 + ;
ll a[MAXN], b[MAXN], gel[MAXN];
string s; ll get_pow(ll x, int n){
ll ans = ;
while(n){
if(n & ) ans = ans * x % mode;
x = x * x % mode;
n >>= ;
}
return ans;
} int main(void){
ll ans = ;
cin >> s;
if(s[] == '(') a[] = ;
for(int i = ; i < s.size(); i++){
if(s[i] == '(') a[i] = a[i - ] + ;
else a[i] = a[i - ];
}
for(int i = s.size() - ; i >= ; i--){
if(s[i] == ')') b[i] = b[i + ] + ;
else b[i] = b[i + ];
}
gel[] = ;
for(int i = ; i < MAXN; i++){
gel[i] = gel[i - ] * i % mode;
}
for(int i = ; i < s.size(); i++){
if(s[i] == ')') continue;
ll cnt1 = a[i], cnt2 = a[i] + b[i] - ;
ans = (ans + (gel[cnt2] * get_pow(gel[cnt1] * gel[cnt2 - cnt1] % mode, mode - )) % mode) % mode;
}
cout << ans << endl;
return ;
}
代码2: 用乘法逆元求得组合数取模
#include <iostream>
#define ll long long
using namespace std; const int mode = 1e9 + ;
const int MAXN = 2e5 + ;
ll a[MAXN], b[MAXN], gel[MAXN];
string s; void exgcd(ll a, ll b, ll &x, ll &y){
if(!b){
y = ;
x = ;
return;
}
exgcd(b, a % b, y, x);
y -= a / b * x;
} int main(void){
ll ans = ;
cin >> s;
if(s[] == '(') a[] = ;
for(int i = ; i < s.size(); i++){
if(s[i] == '(') a[i] = a[i - ] + ;
else a[i] = a[i - ];
}
for(int i = s.size() - ; i >= ; i--){
if(s[i] == ')') b[i] = b[i + ] + ;
else b[i] = b[i + ];
}
gel[] = ;
for(int i = ; i < MAXN; i++){
gel[i] = gel[i - ] * i % mode;
}
for(int i = ; i < s.size(); i++){
if(s[i] == ')') continue;
ll cnt1 = a[i], cnt2 = a[i] + b[i] - ;
ll cc1 = gel[cnt2], cc2 = gel[cnt2 - cnt1] * gel[cnt1] % mode;
ll x, y;
exgcd(cc2, mode, x, y);
x = (x % mode + mode) % mode;
ans = (ans + (cc1 * x) % mode) % mode; }
cout << ans << endl;
return ;
}
cf785D(组合数学)的更多相关文章
- poj 3734 Blocks 快速幂+费马小定理+组合数学
题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...
- 组合数学or not ---- n选k有重
模板问题: 1. 取物品 (comb.pas/c/cpp) [问题描述] 现在有n个物品(有可能相同),请您编程计算从中取k个有多少种不同的取法.[输入] 输入文件有两行,第一行包含两个整数n,k(2 ...
- 组合数学(全排列)+DFS CSU 1563 Lexicography
题目传送门 /* 题意:求第K个全排列 组合数学:首先,使用next_permutation 函数会超时,思路应该转变, 摘抄网上的解法如下: 假设第一位是a,不论a是什么数,axxxxxxxx一共有 ...
- uestc1888 Birthday Party 组合数学,乘法原理
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=25539#problem/G 题目意思: 有n个人,每个人有一个礼物,每个人能拿 ...
- UVA 11076 Add Again 计算对答案的贡献+组合数学
A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs ...
- POJ3252——Round Number(组合数学)
Round Numbers DescriptionThe cows, as you know, have no fingers or thumbs and thus are unable to pla ...
- HDU4675【GCD of scequence】【组合数学、费马小定理、取模】
看题解一开始还有地方不理解,果然是我的组合数学思维比较差 然后理解了之后自己敲了一个果断TLE.... 我以后果然还得多练啊 好巧妙的思路啊 知识1: 对于除法取模还需要用到费马小定理: a ^ (p ...
- hdu 4810 Wall Painting (组合数学+二进制)
题目链接 下午比赛的时候没有想出来,其实就是int型的数分为30个位,然后按照位来排列枚举. 题意:求n个数里面,取i个数异或的所有组合的和,i取1~n 分析: 将n个数拆成30位2进制,由于每个二进 ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
随机推荐
- JQuery 常用代码
1.选择器 1.根据标签名: $('p') 选择文档中的所有段落 2. 根据ID: $("#some-id") 3.类: $('.some-class') $('.t ...
- POJ1060 Modular multiplication of polynomials解题报告 (2011-12-09 20:27:53)
Modular multiplication of polynomials Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3 ...
- 原生js图片懒加载特效
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- spring MVC--配置注解
<context-param> 作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数 param-name 设定上下文的参数名称.必须是唯一名称 param-value 设定 ...
- C/C++ 安全编码 —— 不安全的函数
1. 文件与IO操作 gets():从控制台输入到字符数组: char response[8]; gets(response); 如果控制台输入超过 8 个字符,程序便会发生不确定的行为.其主要问题在 ...
- PS 滤镜——(扭曲)球面化 Spherize
%%%% Spherize clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorit ...
- [转]_int64、long long 的区别
大学生程序代写 C++的64位整数[原]by 赤兔 http://baike.baidu.com/view/1235293.htm 在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常 ...
- Django学习(1)——python manage.py startapp app-name新建app报错问题
作为一个刚接触python的小白,开始学习Django注定前路漫漫,记录一下学习过程中的问题和解决方案. 感谢“自强学堂”的无私奉献,根据教程安装了Django 1.9.12后,尝试新建项目,此时使用 ...
- 洛谷【P3908】异或之和
二进制前置技能:https://www.cnblogs.com/AKMer/p/9698694.html 题目传送门:https://www.luogu.org/problemnew/show/P39 ...
- 不同类型input尺寸设置区别
最近发现为不用类型的input设置相同的尺寸,却得到了不一样的尺寸结果.发现不同类型的input的height和width竟然含义不同.在此小整理一下. (1)button类型 规律 button类型 ...