【數論】Codeforces Round #404 (Div. 2)(D)Anton and School - 2
Codeforces Round #404 (Div. 2)(D)
題意:在原字串中取出子字串,問RSBS(
- 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 ")".
題解:對於每個'(',我們預處理出其左邊'('的個數 l[i] ,和右邊')'的個數 r[i] ;包含這個'('的RSBS個數為;
由範德蒙恆等式:;
當min(l,r) = l 時, k = l-1代入;
當min(l,r) = r時, k = r-1代入;
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int N = 200010;
const LL mod = 1e9+7;
const double esp = 1e-6;
char s[N];
int r[N],l[N];
LL mul[N];
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
LL d = exgcd(b,a%b,x,y);
LL t = x;
x = y;
y = t - a/b*y;
return d;
}
void init()
{
LL ans = 1;
mul[0] = 1;
for(int i = 1; i < N; i++)
{
ans = (ans*i)%mod;
mul[i] = ans;
}
}
LL C(int a,int b)
{
LL x,y,d;
exgcd((mul[b-a]*mul[a])%mod,mod,x,y);
x = (x%mod+mod)%mod;
return (mul[b]*x)%mod;
}
int main()
{
scanf("%s",s);
int len = strlen(s);
int rnum = 0, lnum = 0;
for(int i = 0; i < len; i++)
{
if(s[i] == '(')
{
l[i] = lnum;
lnum++;
}
}
for(int i = len-1; i >= 0; i--)
{
if(s[i] == '(')
r[i] = rnum;
else
rnum++;
}
init();
LL sum = 0;
for(int i = 0; i < len; i++)
{
if(s[i] == '(')
{
int x = min(l[i]+1,r[i]-1);
int y = l[i]+r[i];
sum = (sum+C(x,y))%mod;
}
}
cout << sum << endl;
return 0;
}
相關文章
- Codeforces Round #253 (Div. 2) A. Anton and Letters
- Codeforces Round #452 (Div. 2) D
- Codeforces Round 941 (Div. 2) D
- Codeforces Round 960 (Div. 2)(A - D)
- Codeforces Round #360 (Div. 2) D 數學題
- Codeforces Round #325 (Div. 2) D bfs
- Codeforces Round #250 (Div. 2) A-D
- Codeforces Round #251 (Div. 2) A/B/D
- Codeforces Round #283 (Div. 2) D,E
- Codeforces Round #256 (Div. 2)A-D
- Codeforces Round #358 (Div. 2) D dp
- Codeforces Round #359 (Div. 2) D DFS
- Educational Codeforces Round 34 (Rated for Div. 2) D
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #263 (Div. 2) A-D
- Codeforces Round 720 (Div. 2) A-D
- Codeforces Round #673 (Div. 2)(A-D)題解
- Codeforces Round #401 (Div. 2)(C,D,E)
- Codeforces Round #321 (Div. 2) D 狀壓dp
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
- Codeforces Round #280 (Div. 2 A,B,C,D,E)
- Educational Codeforces Round 170 (Rated for Div. 2) A-D
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)
- Codeforces Round #256 (Div. 2)
- Codeforces Round #259 (Div. 2)
- Codeforces Round #257 (Div. 2)
- Codeforces Round #258 (Div. 2)
- Codeforces Round #171 (Div. 2)