結果

問題 No.2522 Fall in love, Girls!
ユーザー 👑 kmjpkmjp
提出日時 2023-10-28 00:37:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 995 ms / 2,000 ms
コード長 2,096 bytes
コンパイル時間 2,305 ms
コンパイル使用メモリ 206,728 KB
実行使用メモリ 204,172 KB
最終ジャッジ日時 2023-10-28 00:37:47
合計ジャッジ時間 10,519 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
39,664 KB
testcase_01 AC 59 ms
39,656 KB
testcase_02 AC 60 ms
37,608 KB
testcase_03 AC 995 ms
202,120 KB
testcase_04 AC 58 ms
37,608 KB
testcase_05 AC 58 ms
39,656 KB
testcase_06 AC 61 ms
39,976 KB
testcase_07 AC 65 ms
41,724 KB
testcase_08 AC 65 ms
42,984 KB
testcase_09 AC 59 ms
37,608 KB
testcase_10 AC 332 ms
204,172 KB
testcase_11 AC 83 ms
54,032 KB
testcase_12 AC 60 ms
37,608 KB
testcase_13 AC 337 ms
204,172 KB
testcase_14 AC 942 ms
204,168 KB
testcase_15 AC 985 ms
204,168 KB
testcase_16 AC 927 ms
204,168 KB
testcase_17 AC 97 ms
51,944 KB
testcase_18 AC 81 ms
46,824 KB
testcase_19 AC 94 ms
51,944 KB
testcase_20 AC 66 ms
42,984 KB
testcase_21 AC 69 ms
44,420 KB
testcase_22 AC 62 ms
42,024 KB
testcase_23 AC 77 ms
46,824 KB
testcase_24 AC 94 ms
51,944 KB
testcase_25 AC 91 ms
65,308 KB
testcase_26 AC 110 ms
74,868 KB
testcase_27 AC 63 ms
41,712 KB
testcase_28 AC 243 ms
135,460 KB
testcase_29 AC 62 ms
41,864 KB
testcase_30 AC 96 ms
51,944 KB
testcase_31 AC 66 ms
44,420 KB
testcase_32 AC 85 ms
58,548 KB
testcase_33 AC 61 ms
41,704 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,K;
int ma[22];
int need[1010101];
int X[444],Y[444];
ll dp[1<<20][20];
const ll mo=998244353;
const int NUM_=1400001;
static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];
ll comb(ll N_, ll C_) {
	if(C_<0 || C_>N_) return 0;
	return factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;
}

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>>K;
	FOR(i,K) {
		cin>>X[i]>>Y[i];
		X[i]--,Y[i]--;
		need[X[i]]=need[Y[i]]=1;
	}
	int A=0,B=0;
	vector<int> V;
	FOR(i,N) {
		if(need[i]) {
			V.push_back(i);
			if(i<M) A++;
			else B++;
		}
	}
	int AL=M-A,BL=(N-M)-B;
	int C=V.size();
	FOR(i,K) {
		X[i]=lower_bound(ALL(V),X[i])-V.begin();
		Y[i]=lower_bound(ALL(V),Y[i])-V.begin();
		ma[Y[i]]|=1<<X[i];
	}
	
	FOR(i,C) dp[1<<i][i]=1;
	int mask;
	FOR(mask,1<<C) FOR(i,C) if(dp[mask][i]) {
		FOR(x,C) if((mask&(1<<x))==0&&(mask&ma[x])==0) (dp[mask|(1<<x)][x]+=dp[mask][i])%=mo;
	}
	
	ll ret=0;
	if(C==0) {
		ret=BL*fact[N-1]%mo;
	}
	
	FOR(i,C) {
		if(BL) {
			(ret+=dp[(1<<C)-1][i]*BL%mo*fact[N-C-1]%mo*comb(N-1,C))%=mo;
		}
		if(V[i]>=M) {
			(ret+=dp[(1<<C)-1][i]*fact[N-C]%mo*comb(N-1,C-1))%=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