日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces 785 D.Anton and School - 2(组合数处理)

發布時間:2025/4/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)。
組合數預處理:

const int maxn=2e5+10,mod=1e9+7; 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]); }

解題代碼:

#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(组合数处理)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。