Codeforces785D (组合数的逆元)
来源:互联网 发布:民国算法 编辑:程序博客网 时间:2024/06/03 21:21
题意:给出一些括号的字符串,然后求删除里面的一些字符使得字符变成RSBS的类型,RSBS的类型为(),(()),((())).
思路:有一个公式:
#include<bits/stdc++.h>using namespace std;typedef long long ll;typedef pair<int,int> P;typedef set<int>::iterator ITER;#define fi first#define se second#define INF 0x3f3f3f3f#define clr(x,y) memset(x,y,sizeof x)const int maxn = 2e5 + 10;const int Mod = 1e9 + 7;const int N = 2;struct Matrix{Matrix(){clr(m,0);}int m[N][N];};Matrix Mul(Matrix a,Matrix b){Matrix c;for(int i = 0; i < N; i ++){for(int k = 0; k < N; k ++){if(a.m[i][k] == 0)continue;for(int j = 0; j < N; j ++)c.m[i][j] += a.m[i][k] * b.m[k][j] % Mod,c.m[i][j] %= Mod;}}return c;}ll Mul(ll a,ll b){a %= Mod;b %= Mod;ll ret = 0;while(b){if(b & 1){ret += a;if(ret > Mod)ret -= Mod;}b >>= 1;a = (a << 1) % Mod;}return ret;}Matrix pows(Matrix x,ll n){Matrix ret;for(int i = 0; i < 2; i ++)ret.m[i][i] = 1;while(n){if(n & 1)ret = Mul(ret,x);x = Mul(x,x);n >>= 1;}return ret;}ll pows(ll x,ll n){ll ret = 1;while(n){if(n & 1)ret = ret * x % Mod;x = x * x % Mod;n >>= 1;}return ret;}ll powss(ll x,ll n){ll ret = 1;while(n){if(n & 1)ret = Mul(ret,x);x = Mul(x,x);n >>= 1;}return ret;}ll gcd(ll x,ll y){return y ? gcd(y,x % y):x;}ll euler(int n){int ret = n;for(int i = 2; i * i<= n; i ++)if(n % i == 0){ret = ret / i * (i - 1);while(n % i == 0)n /= i;}if(n > 1)ret = ret / n * (n - 1);return ret;}void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d = a;x = 1;y = 0;return;}ex_gcd(b,a % b,d ,y,x);y -=a/b * x;}inline int lowbit(int x){return x &(-x);}//void update(int x,int val){for(int i = x; x < maxn; i += lowbit(i))tree[i] += val;}//void get(int x){int ret = 0;for(int i = x; i > 0 ;i -= lowbit(i))ret += tree[i];return ret;}//int finds(int x){return fa[x] == x ? x : (fa[x] = finds(fa[x]));}char s[maxn];int num1[maxn],num2[maxn];ll fac[maxn],inv[maxn];//fac[i]代表i的阶乘,inv表示i的阶乘的逆元void Init(){ fac[1] = 1; for(int i = 2; i < maxn; i ++) { fac[i] = 1LL * fac[i - 1] * i % Mod; } inv[maxn - 1] = pows(fac[maxn - 1],Mod - 2); for(int i = maxn - 2; i >= 0; i --)//阶乘的逆元这样求 inv[i] = 1LL * inv[i + 1] * (i + 1) % Mod;}ll calc(ll m,ll n){ if(m == 0 || n == m)return 1; return fac[n] * inv[m] % Mod * inv[n - m] % Mod;}int main(){ Init(); while( ~ scanf("%s",s)) { clr(num1,0);clr(num2,0); int len = strlen(s); for(int i = 0; i < len; i ++) { if(s[i] == '(')num1[i] = num1[i - 1] + 1; else num1[i] = num1[i - 1]; } for(int i = len - 1; i >= 0; i --) { if(s[i] == ')')num2[i] = num2[i + 1] + 1; else num2[i] = num2[i + 1]; } ll ans = 0; for(int i = 0; i < len; i ++) { if(s[i] == '(') { ans = (ans + calc(num1[i],num1[i] + num2[i] - 1)) % Mod; } } printf("%I64d\n",ans); } return 0;}
- Codeforces785D (组合数的逆元)
- 组合数、逆元,数学
- FZU2282-组合数-逆元
- hdu4869(逆元+求组合数)
- hdu5651(乘法逆元&&组合数)
- 组合数处理(逆元求解)
- 组合数【Lucas定理,逆元】
- Chess(组合数,逆元)
- 递推求逆元,求组合数
- 2017-07-25 51nod 1119 组合数取模 某数关于质数的逆元
- Ural 1903 Unidentified Ships 组合数 + 乘法逆元
- ZOJ 3841 Cards (组合数+乘法逆元)
- zoj3903-数学公式(快速幂除法逆元组合数模板)
- ssoj2431 棋盘路径(乘法逆元+组合数)
- HDU 5698 瞬间移动 (组合数 + 阶乘逆元)
- lightoj 1067 - Combinations (组合数、乘法逆元)
- light oj 1102 Problem Makes Problem [组合数+逆元]
- HDU5894分位置(组合数,lucas,乘法逆元)
- 持续集成之Jenkins(一)Jenkins安装
- How Many Tables HDU
- Spring 事物手动回滚
- 表单验证 datetype 使用
- Python——列表生成式备忘
- Codeforces785D (组合数的逆元)
- poj1496—Word Index(组合数)
- c#基础 (一)数据类型,数据类型转换 结构体,构造函数
- PHP基础教程-11 创建单位转换器
- Android开发之sqlite数据库的应用
- android学习——使用TableLayout动态生成表格,并为tablerow中的列添加点击监听
- nlp中加入attention
- [PAT乙级]1048. 数字加密(20)
- 剑指Offer读书笔记5