結果

問題 No.2605 Pickup Parentheses
ユーザー 👑 kmjpkmjp
提出日時 2024-01-12 23:04:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 755 ms / 2,000 ms
コード長 2,957 bytes
コンパイル時間 2,531 ms
コンパイル使用メモリ 216,768 KB
実行使用メモリ 30,692 KB
最終ジャッジ日時 2024-03-20 19:50:05
合計ジャッジ時間 16,260 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
13,784 KB
testcase_01 AC 16 ms
13,784 KB
testcase_02 AC 16 ms
13,784 KB
testcase_03 AC 19 ms
13,912 KB
testcase_04 AC 19 ms
14,040 KB
testcase_05 AC 20 ms
14,040 KB
testcase_06 AC 22 ms
14,168 KB
testcase_07 AC 21 ms
14,040 KB
testcase_08 AC 22 ms
14,168 KB
testcase_09 AC 17 ms
13,912 KB
testcase_10 AC 21 ms
14,040 KB
testcase_11 AC 19 ms
14,040 KB
testcase_12 AC 17 ms
13,912 KB
testcase_13 AC 17 ms
13,912 KB
testcase_14 AC 18 ms
14,040 KB
testcase_15 AC 20 ms
14,040 KB
testcase_16 AC 17 ms
13,912 KB
testcase_17 AC 18 ms
13,912 KB
testcase_18 AC 423 ms
25,160 KB
testcase_19 AC 204 ms
19,316 KB
testcase_20 AC 51 ms
15,024 KB
testcase_21 AC 34 ms
14,552 KB
testcase_22 AC 29 ms
15,072 KB
testcase_23 AC 433 ms
24,992 KB
testcase_24 AC 49 ms
15,276 KB
testcase_25 AC 515 ms
26,580 KB
testcase_26 AC 543 ms
26,584 KB
testcase_27 AC 403 ms
24,408 KB
testcase_28 AC 16 ms
13,784 KB
testcase_29 AC 88 ms
16,256 KB
testcase_30 AC 203 ms
19,240 KB
testcase_31 AC 17 ms
13,784 KB
testcase_32 AC 16 ms
13,784 KB
testcase_33 AC 435 ms
25,120 KB
testcase_34 AC 16 ms
13,784 KB
testcase_35 AC 17 ms
13,784 KB
testcase_36 AC 16 ms
13,784 KB
testcase_37 AC 347 ms
21,048 KB
testcase_38 AC 95 ms
18,660 KB
testcase_39 AC 46 ms
15,328 KB
testcase_40 AC 237 ms
23,560 KB
testcase_41 AC 250 ms
26,444 KB
testcase_42 AC 316 ms
25,832 KB
testcase_43 AC 70 ms
16,708 KB
testcase_44 AC 81 ms
18,444 KB
testcase_45 AC 26 ms
14,808 KB
testcase_46 AC 41 ms
15,888 KB
testcase_47 AC 18 ms
13,912 KB
testcase_48 AC 54 ms
17,960 KB
testcase_49 AC 247 ms
24,168 KB
testcase_50 AC 100 ms
18,408 KB
testcase_51 AC 16 ms
13,784 KB
testcase_52 AC 151 ms
19,996 KB
testcase_53 AC 213 ms
23,856 KB
testcase_54 AC 114 ms
19,336 KB
testcase_55 AC 180 ms
23,624 KB
testcase_56 AC 46 ms
16,048 KB
testcase_57 AC 16 ms
13,784 KB
testcase_58 AC 755 ms
30,692 KB
testcase_59 AC 408 ms
25,516 KB
testcase_60 AC 512 ms
26,744 KB
testcase_61 AC 486 ms
26,788 KB
testcase_62 AC 526 ms
26,224 KB
testcase_63 AC 515 ms
26,040 KB
testcase_64 AC 593 ms
27,380 KB
testcase_65 AC 438 ms
25,356 KB
testcase_66 AC 737 ms
28,868 KB
testcase_67 AC 496 ms
26,548 KB
testcase_68 AC 34 ms
15,960 KB
testcase_69 AC 16 ms
13,784 KB
testcase_70 AC 16 ms
13,784 KB
権限があれば一括ダウンロードができます

ソースコード

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 L[202020],R[202020];
const ll mo=998244353;

const int NUM_=400001;
static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];

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+mo)%mo;
				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<=64) { //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);
}


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;
	if(N%2) {
		cout<<0<<endl;
		return;
	}
	
	queue<vec<ll>> Q;
	FOR(i,M) {
		cin>>L[i]>>R[i];
		x=R[i]-L[i]+1;
		if(x%2==0) {
			x/=2;
			vec<ll> A(x+1);
			A[0]=1;
			A[x]=mo-fact[2*x]*modpow(fact[x+1]*fact[x])%mo;
			Q.push(A);
		}
	}
	vec<ll> A(N/2+1);
	FOR(i,N/2+1) A[i]=fact[2*i]*modpow(fact[i+1]*fact[i])%mo;
	Q.push(A);
	while(Q.size()>1) {
		auto a=Q.front();
		Q.pop();
		Q.push(MultPoly(a,Q.front(),1));
		Q.pop();
	}
	vec<ll> V=Q.front();
	cout<<V[N/2]<<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