結果

問題 No.2484 Add to Variables
ユーザー 👑 kmjpkmjp
提出日時 2023-09-24 01:55:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,965 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 213,700 KB
実行使用メモリ 51,676 KB
最終ジャッジ日時 2023-09-24 01:56:03
合計ジャッジ時間 37,685 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
50,592 KB
testcase_01 AC 88 ms
50,524 KB
testcase_02 AC 87 ms
50,480 KB
testcase_03 AC 89 ms
50,508 KB
testcase_04 AC 90 ms
50,468 KB
testcase_05 AC 89 ms
50,556 KB
testcase_06 AC 91 ms
50,688 KB
testcase_07 AC 96 ms
50,756 KB
testcase_08 AC 95 ms
50,616 KB
testcase_09 AC 96 ms
50,488 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 WA -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 WA -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 WA -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,M;
int B[2020];
const ll mo=998244353;

ll modpow(ll a, ll n = mo-2) {
	ll r=1; a%=mo;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

template <class T> using vec=vector<T>; //using vec=valarray<T>;

template<class T> vec<T> fft(vec<T> v, bool rev=false) {
	int n=v.size(),i,j,m;
	for(int m=n; m>=2; m/=2) {
		T wn=modpow(5,(mo-1)/m);
		if(rev) wn=modpow(wn);
		for(i=0;i<n;i+=m) {
			T w=1;
			for(int j1=i,j2=i+m/2;j2<i+m;j1++,j2++) {
				T t1=v[j1],t2=v[j2];
				v[j1]=t1+t2;
				v[j2]=ll(t1+mo-t2)*w%mo;
				while(v[j1]>=mo) v[j1]-=mo;
				w=(ll)w*wn%mo;
			}
		}
	}
	for(i=0,j=1;j<n-1;j++) {
		for(int k=n>>1;k>(i^=k);k>>=1);
		if(i>j) swap(v[i],v[j]);
	}
	if(rev) {
		ll rv = modpow(n);
		FOR(i,n) v[i]=(ll)v[i]*rv%mo;
	}
	return v;
}

template<class T> vec<T> MultPoly(vec<T> P,vec<T> Q,bool resize=false) {
	if(resize) {
		int maxind=0,pi=0,qi=0,i;
		int s=2;
		FOR(i,P.size()) if(norm(P[i])) pi=i;
		FOR(i,Q.size()) if(norm(Q[i])) qi=i;
		maxind=pi+qi+1;
		while(s*2<maxind) s*=2;
		
		if(s<=16) { //fastpath
			vec<T> R(s*2);
			for(int x=0;x<=pi;x++) for(int y=0;y<=qi;y++) (R[x+y]+=P[x]*Q[y])%=mo;
			return R;
		}
		vec<T> P2(s*2),Q2(s*2);
		FOR(i,pi+1) P2[i]=P[i];
		FOR(i,qi+1) Q2[i]=Q[i];
		swap(P,P2),swap(Q,Q2);
	}
	P=fft(P), Q=fft(Q);
	for(int i=0;i<P.size();i++) P[i]=(ll)P[i]*Q[i]%mo;
	return fft(P,true);
}

ll comb(ll N_, ll C_) {
	const int NUM_=400001;
	static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];
	if (fact[0]==0) {
		inv[1]=fact[0]=factr[0]=1;
		for (int i=2;i<=NUM_;++i) inv[i] = inv[mo % i] * (mo - mo / i) % mo;
		for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo, factr[i]=factr[i-1]*inv[i]%mo;
	}
	if(C_<0 || C_>N_) return 0;
	return factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;
}

template<class T> vec<T> pow(int N,vec<T> C,int maxlen=1<<30) {
	vec<T> R={1};
	while(N) {
		if(N%2) {
			R=MultPoly(R,C,1);
			if(R.size()>maxlen) R.resize(maxlen);
		}
		C=MultPoly(C,C,1);
		if(C.size()>maxlen) C.resize(maxlen);
		N/=2;
	}
	return R;
}

const int NUM_=2000003;
static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];
int D[1010];


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	inv[1]=fact[0]=factr[0]=1;
	for (int i=2;i<=NUM_;++i) inv[i] = inv[mo % i] * (mo - mo / i) % mo;
	for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo, factr[i]=factr[i-1]*inv[i]%mo;
	cin>>N>>M;
	int id=0;
	FOR(i,N) {
		cin>>B[i];
	}
	
	if(N==1) {
		cout<<comb(M,B[0])<<endl;
		return;
	}
	
	int sum=0;
	queue<vec<ll>> Q;
	FOR(i,N-1) {
		y=abs(B[i]-B[i+1]);
		sum+=y;
		if(B[i]>B[i+1]) {
			x=B[i]-B[i+1];
			FOR(j,i+1) B[j]-=x;
		}
		else if(B[i]<B[i+1]) {
			x=B[i+1]-B[i];
			for(j=i+1;j<N;j++) B[j]-=x;
		}
		vector<ll> C(1010);
		FOR(j,501) if(j*2+y<=M) C[j*2+y]=factr[j]*factr[j+y]%mo;
		Q.push(C);
	}
	
	FOR(i,N) if(B[i]<0||B[i]!=B[0]) {
		cout<<0<<endl;
		return;
	}
	
	while(Q.size()>1) {
		auto a=Q.front();
		Q.pop();
		auto b=MultPoly(a,Q.front(),1);
		b.resize(M+1);
		Q.push(b);
		Q.pop();
	}
	vec<ll> V=Q.front();
	ll ret=0;
	FOR(i,B[0]+1) {
		int num=sum+i*2;
		if(num>M) continue;
		ll a=V[num]*factr[B[0]-i]%mo;
		num+=B[0]-i;
		if(num>M) continue;
		a=a*factr[M-num]%mo;
		(ret+=a*fact[M])%=mo;
		
	}
	
	cout<<ret<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0