范德蒙恒等式

 Anton and School - 2 

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.

Examples

Input
)(()()
Output
6
Input
()()()
Output
7
Input
)))
Output
0

Note

In the first sample the following subsequences are possible:

  • If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())".
  • If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()".
  • If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()".
  • If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()".
  • If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()".

The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6.

题解:范德蒙恒等式(快速幂+逆元+排列组合)

这个题就是用上面来写,图中的a 就是 L【i】,b就是R【i】,也就是如果从左边再选出X个,右边就要选出X+1个来对应。0<= X <=min(L【i】-1 , R【i】);然后把所有的情况加起来取模,就是答案

 写的时候,cin输入,从0开始计算l,r数组,结果出错了,strlen(str)一定要记录下来,不然T到自闭(我太菜了,没想到这个原因)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <string>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <list>
 9 #include <deque>
10 #include <queue>
11 #include <stack>
12 #include <cstdlib>
13 #include <cstdio>
14 #include <cmath>
15 #include <iomanip>
16 #define ull unsigned long long
17 #define ll long long
18 #define pb push_back
19 #define rep(i,start,end) for(int i=start;i<=end;i++)
20 #define per(i,end,start) for(int i=end;i>=start;i--)
21 #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
22 #define lc now<<1
23 #define rc now<<1|1
24 using namespace std;
25 const int mod = 1000000007 ; ///998244353;
26 const int mxn = 2e5 +7;
27 ll _,n,m,t,k,u,v,ans,cnt,ok,lim;
28 int  l[mxn] ,  r[mxn] ;
29 ll dp[mxn] , inv[mxn];
30 char str[mxn];
31 ll ksm(ll x, ll k)
32 {
33     ll ans = 1;
34     while(k)
35     {
36         if(k&1) ans=ans*x%mod;
37         x = x*x%mod;
38         k>>=1;
39     }
40     return ans;
41 }
42 ll C(ll n,ll m){return dp[n]%mod*inv[n-m]%mod*inv[m]%mod;}
43 int main()
44 {
45     tle;
46     dp[0]=1;inv[0] = 1 ;
47     rep(i,1,mxn) dp[i] = dp[i-1]*i%mod , inv[i] = ksm(dp[i] , mod-2);
48     scanf("%s",str+1);
49     int len = strlen(str+1) ;
50     rep(i,1,len) l[i] = ( str[i]=='('?l[i-1]+1:l[i-1] );
51     per(i,len,1) r[i] = ( str[i]==')'?r[i+1]+1:r[i+1] );
52     ll ans = 0 ;
53     for(int i=1;i<=len;i++)
54     {
55         if(str[i]=='(')
56             ans = ( ans + C(l[i]+r[i]-1,l[i]) )%mod ;
57     }
58     printf("%lld
",(ans%mod));
59 }

Walk 

链接:https://ac.nowcoder.com/acm/contest/5929/K
来源:牛客网

题目描述

多多喜欢行走,有一天老师问他一个问题:在一个方格点阵中,左上角点的坐标为(1, 1),行坐标从上到下依次递增,列坐标从左到右依次递增,每次行走可以向上、下、左、右移动一格。现在要从(1, 1)点走到(N, M)点,在行走步数最少的情况下,有多少种行走方法?(答案可能过大,请对答案取模1000000007)

输入描述:

第一行输入一个正整数 T,代表询问次数 (1 ≤ T ≤ 100000)
接下来 T 行,每行输入两个正整数 N,M (1 ≤ N ≤ 106,1 ≤ M ≤ 106)

输出描述:

对于每次询问,输出一个正整数,代表在行走步数最少的情况下,从(1, 1)点走到(N, M)点的方法总数 (答案可能过大,请对答案取模1000000007)
示例1

输入

1
2 2

输出

2

说明

(1, 1)    (1, 2)
(2, 1)    (2, 2)
在步数最少的情况下,从(1, 1)走到(2, 2)的方法有2种:
第一种:(1, 1) -> (1, 2) -> (2, 2)
第二种:(1, 1) -> (2, 1) -> (2, 2)
示例2

输入

1
2 3

输出

3

说明

(1, 1)    (1, 2)    (1, 3)
(2, 1)    (2, 2)    (2, 3)
在步数最少的情况下,从(1, 1)走到(2, 3)的方法有3种:
第一种:(1, 1) -> (1, 2) -> (1, 3) -> (2, 3)
第二种:(1, 1) -> (1, 2) -> (2, 2) -> (2, 3)
第三种:(1, 1) -> (2, 1) -> (2, 2) -> (2, 3)
示例3

输入

1
5 3

输出

15
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define pi acos(-1)
#define ull unsigned long long
#define ll long long
#define pb push_back
#define all(vc) vc.begin() , vc.end()
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc now<<1
#define rc now<<1|1
#define eps 1e-9
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
using namespace std;
const int mod = 1000000007;//998244353;
const int mxn = 2e6 +7;
const int inf = 1000000007;
ll _,n,m,t,k,u,w,v,ans,cnt,ok,lim,len,tmp,last;
struct node{int x,y,t;};//e[mxn];
ll num[mxn];
ll ksm(ll a,ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b&1) ans = ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
ll C(ll n,ll m)
{return num[n] * ksm(num[m], mod - 2) % mod * ksm(num[n - m], mod - 2) % mod;}
int main()
{
    num[1] = 1 ; num[0] = 1 ;
    rep(i,2,mxn) num[i] = i*num[i-1]%mod;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        n-- ; m-- ;
        cout<<C(n+m,m)<<endl;
    }
}
View Code
所遇皆星河
原文地址:https://www.cnblogs.com/Shallow-dream/p/12851703.html