Codeforces 785 D.Anton and School - 2(组合数处理)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 785 D.Anton and School - 2(组合数处理)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Codeforces 785 D.Anton and School - 2
題目大意:從一串由“(”,“)”組成的字符串中,找出有多少個子序列滿足:序列長度為偶數,且前n/2個為“(”,后n/2個為“)”;
思路:枚舉每一個左括號,則以該左括號為左右分界的子序列個數為∑C(L-1,i)C(R,i+1)(其中L為該左括號向左的左括號數,R為該左括號向右的右括號數,i從0累加到L-1)。而∑C(L-1,i)C(R,i+1)=∑C(L-1,L-1-i)C(R,i+1)=C(L+R-1,L)。
組合數預處理:
解題代碼:
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<map> #include<stack> #include<queue> #include<set> #include<cmath> #include<algorithm> #include<climits> using namespace std; typedef long long ll; typedef pair<int,int> P; typedef map<int,int> M; typedef queue<int> Q; typedef set<int> S; typedef vector<int> V; const int maxn=2e5+10,mod=1e9+7; inline ll add(ll a,ll b) {a+=b;if (a>=mod)a-=mod;if (a<0)a+=mod;return a; } inline ll mul(ll a,ll b) {return a*b%mod; } ll jc[maxn],inv[maxn]; ll kpow(ll a,ll x) {ll res=1;while (x){if (x&1)res=res*a%mod;a=a*a%mod;x>>=1;}return res; } void init() {jc[0]=1;for (int i=1;i<maxn;++i){jc[i]=jc[i-1]*i%mod;}inv[maxn-1]=kpow(jc[maxn-1],mod-2);for (int i=maxn-2;i>=0;--i){inv[i]=inv[i+1]*(i+1)%mod;;} } inline ll C(ll n,ll m) {if (m>n)return 0;return mul(mul(jc[n],inv[m]),inv[n-m]); }char s[maxn]; int main() {std::ios::sync_with_stdio(0);cin.tie(0);int i,j,m,n,k,l=0,r=0; cin>>s;n=strlen(s);init();for (i=0;i<n;++i){r+=s[i]==')';}ll ans=0;for (i=0;i<n;++i){l+=s[i]=='(';r-=s[i]==')';if (s[i]=='(')ans=add(ans,C(l+r-1,l));}cout<<ans;return 0; }轉載于:https://www.cnblogs.com/orangee/p/9151343.html
總結
以上是生活随笔為你收集整理的Codeforces 785 D.Anton and School - 2(组合数处理)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装consul
- 下一篇: SCU - 4438 Censor