結果

問題 No.2243 Coaching Schedule
ユーザー 👑 kmjpkmjp
提出日時 2023-03-11 00:41:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 649 ms / 4,000 ms
コード長 3,029 bytes
コンパイル時間 2,961 ms
コンパイル使用メモリ 219,144 KB
実行使用メモリ 22,420 KB
最終ジャッジ日時 2023-10-18 09:23:26
合計ジャッジ時間 9,810 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
14,368 KB
testcase_01 AC 16 ms
14,368 KB
testcase_02 AC 16 ms
14,368 KB
testcase_03 AC 17 ms
14,364 KB
testcase_04 AC 17 ms
14,368 KB
testcase_05 AC 122 ms
22,400 KB
testcase_06 AC 165 ms
22,400 KB
testcase_07 AC 164 ms
22,400 KB
testcase_08 AC 164 ms
22,400 KB
testcase_09 AC 196 ms
22,400 KB
testcase_10 AC 199 ms
22,400 KB
testcase_11 AC 197 ms
22,400 KB
testcase_12 AC 649 ms
22,420 KB
testcase_13 AC 529 ms
22,416 KB
testcase_14 AC 534 ms
22,416 KB
testcase_15 AC 526 ms
22,416 KB
testcase_16 AC 112 ms
22,400 KB
testcase_17 AC 55 ms
16,416 KB
testcase_18 AC 143 ms
21,840 KB
testcase_19 AC 179 ms
22,248 KB
testcase_20 AC 128 ms
18,616 KB
testcase_21 AC 83 ms
18,148 KB
testcase_22 AC 78 ms
17,992 KB
testcase_23 AC 24 ms
14,652 KB
testcase_24 AC 97 ms
18,576 KB
testcase_25 AC 29 ms
15,028 KB
testcase_26 AC 48 ms
16,084 KB
testcase_27 AC 102 ms
18,640 KB
testcase_28 AC 31 ms
15,096 KB
testcase_29 AC 154 ms
21,856 KB
testcase_30 AC 155 ms
22,176 KB
testcase_31 AC 159 ms
22,280 KB
testcase_32 AC 77 ms
17,896 KB
testcase_33 AC 50 ms
16,156 KB
testcase_34 AC 143 ms
21,792 KB
testcase_35 AC 159 ms
22,084 KB
testcase_36 AC 151 ms
22,140 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 M,N,A[202020];
const ll mo=998244353;
int C[101010];
map<int,int> D;
ll P[101010];
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;
}

ll comb(ll N_, ll C_) {
	if(C_<0 || C_>N_) return 0;
	return factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;
}

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);
}

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>>M>>N;
	FOR(i,N) {
		cin>>x;
		C[x-1]++;
	}
	FOR(i,M) D[C[i]]++;
	
	FOR(i,N+1) P[i]=1;
	FORR2(a,b,D) {
		FOR(i,N+1) {
			ll c=modpow(comb(i,a)*fact[a],b);
			P[i]=P[i]*c%mo;
		}
	}
	
	vector<ll> G,F;
	FOR(i,N+1) G.push_back(P[i]*factr[i]%mo);
	FOR(i,N+1) {
		F.push_back(factr[i]);
		if(i%2) F.back()=mo-F.back();
	}
	
	auto H=MultPoly(G,F,1);
	H.resize(N+1);
	ll ret=0;
	FOR(i,N+1) ret+=fact[i]*H[i]%mo;
	
	cout<<ret%mo<<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