codeforces 785D Anton and School - 2(组合恒等式)

题目大意:http://codeforces.com/problemset/problem/785/D
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters “(” and “)” (without quotes)).
On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met:
It is not empty (that is n ≠ 0).
The length of the sequence is even.
First charactes of the sequence are equal to “(“.
Last charactes of the sequence are equal to “)”.
For example, the sequence “((()))” is an RSBS but the sequences “((())” and “(()())” are not RSBS.
Elena Ivanovna, Anton’s teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted.
Because the answer can be very big and Anton’s teacher doesn’t like big numbers, she asks Anton to find the answer modulo 109 + 7.
Anton thought of this task for a very long time, but he still doesn’t know how to solve it. Help Anton to solve this task and write a program that finds the answer for it!
Input
The only line of the input contains a string s — the bracket sequence given in Anton’s homework. The string consists only of characters “(” and “)” (without quotes). It’s guaranteed that the string is not empty and its length doesn’t exceed 200 000.
Output
Output one number — the answer for the task modulo 109 + 7
解题思路:
* O( n )的预处理。很显然需要统计一下每个位置前面的(有多少个,后面的)有多少个,才可以方便计数。
* 算出对每一个位置前后的()有多少种匹配,为了保证不重复,所以我们要求一定要使用当前位置的(,设当前位置前面有n个(包括本身,后面有m个)。所以对于每一个(出现的位置,可能有的总的组合数为 ni=1(Ci1n1Cim) 如果 n>m ,对应位置组合数为0,结论不变。
* ni=1(Ci1n1Cim) = ni=1(Cnin1Cim) = Cnn+m1 (最后一步由范德蒙恒等式证) 并且由最后的结果可以看出
* 本题还有一个关键就是求逆元,求解逆元有多种方式费马小定理、拓展欧几里得。这里提供一种O(n)求出1-n阶乘逆元的方法
先用任意一种方式求出 n! 的逆元
然后 i!1(i+1)!1(i+1)(modp)
证明如下
(i+1)!1i!1(i+1)1(modp)
i!1(i+1)!1(i+1)11(modp)

这里还有两篇讲解逆元的文章大家感兴趣可以看下http://blog.csdn.net/daniel_csdn/article/details/50783734
http://blog.miskcoo.com/2014/09/linear-find-all-invert
不过第一篇文章的方法三似乎是错的。
AC代码

/*
    @Author: wchhlbt
    @Date:   2017/3/18
*/
#include <bits/stdc++.h>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 200000
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

ll fac[maxn+7],inv[maxn+7];
int l[maxn+7],r[maxn+7];
char s[maxn+7];

ll quickpow(ll m,ll n,ll k)     // return m^n % k
{
    ll b = 1;
    m = m%k;
    while (n > 0)
    {
          if (n & 1)
             b = (b*m)%k;
          n = n >> 1 ;
          m = (m*m)%k;
    }
    return b;
}

ll cal(ll n, ll m)
{
    //cout << inv[n] << " " << inv[m-1] << endl;
    return fac[n+m-1] * inv[n] % mod * inv[m-1] % mod;
}
int main()
{
    //freopen("test.txt","r",stdin);
    scanf("%s",s+1);
    int len = strlen(s+1);
    for(int i = 1; i<=len; i++)
        l[i] = l[i-1] + (s[i]=='(');
    for(int i = len; i>=1; i--)
        r[i] = r[i+1] + (s[i]==')');
    fac[0] = 1; inv[0] = 1;
    for(int i = 1; i<=maxn+5; i++){
        fac[i] = i*fac[i-1]%mod;
    }
    inv[maxn+5] = quickpow(fac[maxn+5], mod-2, mod);
    for(int i = maxn+4; i>=0; i--)
        inv[i] = inv[i+1] * (i+1) % mod;
    ll ans = 0;
    for(int i = 1; i<=len; i++){
        if(s[i]=='(')
            ans = (ans + cal(l[i], r[i]) ) % mod;
    }
    printf("%I64d\n",ans);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值