跳到正文

题解

题解:CF2063F1 Counting Is Not Fun (Easy Version)

dulinfan2023约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
CF2063F1

版本

  1. v1当前

思路

有括号序列能想到 Catalan 数,1,1,2,5,14,42,⋯1,1,2,5,14,42,\cdots,容易发现样例的输出都是这些数,猜测答案与 Catalan 数有关,比如 ???? 的合法括号序列有 C42=2=2C_{\frac{4}{2}=2}=2 种,?????? 的合法括号序列有 C62=3=5C_{\frac{6}{2}=3}=5。

题目告诉你一个合法初始字符串有 2n2n 位,那么合法括号序列有 CnC_n 种,直接输出即可。

对于加入一个括号,比如 ???????? 变成 ??(??)??,数量是 C22C22C22=1C_{\frac{2}{2}}C_{\frac{2}{2}}C_{\frac{2}{2}}=1。

那么答案就显而易见了,求出每个 ? 连续串的长度,求出数量相乘即可。

code:

C++
#include<bits/stdc++.h>
using namespace std;
typedef int i32;
#define int long long
#define debug() cout<<"come on"<<'\n'
#define mkp make_pair
#define pb push_back
#define ft first
#define sd second
typedef pair<int,int> pii;
typedef pair<int,pii> piii;
const int mod=998244353;
stack<int> s;
int fac[10005],inv[10005],p[10005],ans,l[10005],r[10005],cnt[10005],T,n;
int qpow(int a,int b){
	int res=1;
	while(b){
		if(b&1)res*=a,res%=mod;
		a*=a,a%=mod;
		b>>=1;
	}
	return res;
}
int cat(int x){ return fac[2*x]*inv[x]%mod*inv[x+1]%mod; }
signed main(){
	fac[0]=inv[0]=1;
	for(int i=1;i<=1e4;i++)fac[i]=fac[i-1]*i%mod,inv[i]=qpow(fac[i],mod-2);
	cin>>T;
	s.push(0);
	while(T--){
		cin>>n;
		for(int i=1;i<=n;i++)cin>>l[i]>>r[i];
		for(int i=0;i<=2*n;i++)p[i]=0;
		for(int i=0;i<=n;i++){
			for(int j=0;j<=n;j++)cnt[j]=0;
			p[l[i]]=i,p[r[i]]=-i,ans=1;
			for(int j=1;j<=2*n;j++){
				if(!p[j])++cnt[s.top()];
				else if(p[j]<0)s.pop();
				else s.push(p[j]);
			}
			for(int j=0;j<=n;j++)ans*=cat(cnt[j]/2),ans%=mod;
			cout<<ans<<" \n"[i==n];
		}
	}
	return 0;
}
原文