跳到正文

剪贴板

剪贴板 m2ddplis

原文
剪贴板信息

剪贴板信息

收录
更新

具体细节看代码

C++
#include<bits/stdc++.h>
#define ll long long//记得long long
#define LL long long
#define ld long double
using namespace std;
const int BUF_SIZE=1<<22;char in_buf[BUF_SIZE],*p1=in_buf,*p2=in_buf,out_buf[BUF_SIZE],*pp=out_buf;inline char myget(){if(p1==p2){p1=in_buf;p2=in_buf+fread(in_buf,1,BUF_SIZE,stdin);}return p1==p2?EOF:*p1++;}inline void myput(char c){if(pp-out_buf==BUF_SIZE){fwrite(out_buf,1,BUF_SIZE,stdout);pp=out_buf;}*pp++=c;}struct FastIn{template<typename T>FastIn& operator>>(T&x){x=0;char c=myget();bool sign=0;while(c<'0'||c>'9'){if(c=='-')sign=1;c=myget();}while(c>='0'&&c<='9'){x=x*10+(c-'0');c=myget();}if(sign)x=-x;return *this;}FastIn& operator>>(char*s){char c=myget();while(c==' '||c=='\n'||c=='\t'||c=='\r')c=myget();while(c!=' '&&c!='\n'&&c!='\t'&&c!='\r'&&c!=EOF)*s++=c,c=myget();*s='\0';return *this;}FastIn& operator>>(string&s){s.clear();char c=myget();while(c==' '||c=='\n'||c=='\t'||c=='\r')c=myget();while(c!=' '&&c!='\n'&&c!='\t'&&c!='\r'&&c!=EOF)s+=c,c=myget();return *this;}FastIn& operator>>(char& x) {char c = myget();while (c == ' ' || c == '\n' || c == '\t' || c == '\r')c = myget();x = c;return *this;}}fin;struct FastOut{template<typename T>FastOut& operator<<(T x){if(x<0){myput('-');x=-x;}if(x==0){myput('0');return *this;}char buf[20];int pos=0;while(x>0){buf[pos++]=x%10+'0';x/=10;}while(pos>0)myput(buf[--pos]);return *this;}FastOut& operator<<(const char*s){while(*s)myput(*s++);return *this;}FastOut& operator<<(const string&s){for(char c:s)myput(c);return *this;}FastOut& operator<<(char c){myput(c);return *this;}~FastOut(){fwrite(out_buf,1,pp-out_buf,stdout);}}fout;
#define cin fin
#define cout fout//神秘快读
const int N=2e5+5,mod=998244353;
const int C_N=2e5+5;ll Cx[C_N],Cy[C_N];
inline ll pow2(ll x,ll y){ll sum=1;while(y){if(y&1)sum=sum*x%mod;x=x*x%mod;y>>=1;}return sum;}
void CCC(){Cx[0]=Cy[0]=1;for(int i=1;i<C_N;i++)Cx[i]=Cx[i-1]*i%mod;Cy[C_N-1]=pow2(Cx[C_N-1],mod-2);for(int i=C_N-2;i;i--)Cy[i]=Cy[i+1]*(i+1)%mod;return;}
inline ll C(int n,int m){if(m<0||m>n)return 0;return Cx[n]*Cy[m]%mod*Cy[n-m]%mod;}
inline ll A(int n,int m){if(m<0||m>n)return 0;return Cx[n]*Cy[n-m]%mod;}
ll C3(ll n,ll m){if(m==0)return 1;return C(n%mod,m%mod)*C3(n/mod,m/mod)%mod;}//预处理A与C
ll t,n;
signed main(){
	CCC();//调用预处理
	cin>>t;
	while(t--){
		cin>>n;
		ll x,y=0,j=0,qum=1;//初始化
		for(int i=1;i<=n;++i){
			cin>>x;
			if(i==n&&x!=-1&&x!=n){//判断a_n是否合法
				qum=0;
				break;
			}
			if(i==n)x=n;//需要再最后一位再经行总处理,不然可能爆1
			if(x==-1)continue;
			if(qum==0)continue;//为了避免CF不讲武德,在qum==0时应该直接跳过
			ll s=x-y,sum=0;
			for(int k=0;k<=s;++k){
				sum=(sum+C(i-j,k)*A(j-y,k)%mod*C(i-y-k,s-k)%mod*A(i-j,s-k))%mod;//直接套公式
			}
			qum=qum*sum%mod;//乘上之前的
			y=x,j=i;
		}
		cout<<qum<<'\n';//输出答案
	}
	return 0;
}