結果

問題 No.1354 Sambo's Treasure
ユーザー 👑 kmjpkmjp
提出日時 2021-01-17 14:54:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,016 ms / 2,000 ms
コード長 2,780 bytes
コンパイル時間 2,476 ms
コンパイル使用メモリ 211,872 KB
実行使用メモリ 104,468 KB
最終ジャッジ日時 2023-08-22 08:17:15
合計ジャッジ時間 21,430 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
22,532 KB
testcase_01 AC 19 ms
22,272 KB
testcase_02 AC 20 ms
22,284 KB
testcase_03 AC 20 ms
22,276 KB
testcase_04 AC 19 ms
22,508 KB
testcase_05 AC 19 ms
22,656 KB
testcase_06 AC 19 ms
22,304 KB
testcase_07 AC 19 ms
22,264 KB
testcase_08 AC 4 ms
11,696 KB
testcase_09 AC 20 ms
22,340 KB
testcase_10 AC 20 ms
22,360 KB
testcase_11 AC 20 ms
22,284 KB
testcase_12 AC 20 ms
22,356 KB
testcase_13 AC 20 ms
22,280 KB
testcase_14 AC 20 ms
22,288 KB
testcase_15 AC 20 ms
22,312 KB
testcase_16 AC 20 ms
22,764 KB
testcase_17 AC 20 ms
22,288 KB
testcase_18 AC 20 ms
22,296 KB
testcase_19 AC 20 ms
22,380 KB
testcase_20 AC 20 ms
22,400 KB
testcase_21 AC 20 ms
22,556 KB
testcase_22 AC 19 ms
22,320 KB
testcase_23 AC 31 ms
14,756 KB
testcase_24 AC 30 ms
14,752 KB
testcase_25 AC 1,016 ms
104,368 KB
testcase_26 AC 1,013 ms
104,188 KB
testcase_27 AC 1,016 ms
104,376 KB
testcase_28 AC 30 ms
14,716 KB
testcase_29 AC 1,015 ms
104,244 KB
testcase_30 AC 1,014 ms
104,192 KB
testcase_31 AC 1,015 ms
104,232 KB
testcase_32 AC 1,012 ms
104,180 KB
testcase_33 AC 30 ms
14,748 KB
testcase_34 AC 1,012 ms
104,192 KB
testcase_35 AC 31 ms
14,904 KB
testcase_36 AC 1,015 ms
104,236 KB
testcase_37 AC 31 ms
14,752 KB
testcase_38 AC 1,015 ms
104,180 KB
testcase_39 AC 1,015 ms
104,184 KB
testcase_40 AC 1,013 ms
104,468 KB
testcase_41 AC 31 ms
14,772 KB
testcase_42 AC 1,015 ms
104,176 KB
testcase_43 AC 54 ms
30,456 KB
testcase_44 AC 53 ms
30,316 KB
testcase_45 AC 58 ms
30,380 KB
testcase_46 AC 63 ms
30,436 KB
testcase_47 AC 61 ms
30,472 KB
testcase_48 AC 71 ms
30,436 KB
testcase_49 AC 56 ms
30,404 KB
testcase_50 AC 57 ms
30,432 KB
testcase_51 AC 69 ms
30,544 KB
testcase_52 AC 64 ms
30,424 KB
testcase_53 AC 66 ms
30,440 KB
testcase_54 AC 60 ms
30,392 KB
testcase_55 AC 54 ms
30,328 KB
testcase_56 AC 68 ms
30,432 KB
testcase_57 AC 65 ms
30,748 KB
testcase_58 AC 65 ms
30,628 KB
testcase_59 AC 61 ms
30,464 KB
testcase_60 AC 66 ms
30,424 KB
testcase_61 AC 57 ms
30,424 KB
testcase_62 AC 70 ms
30,756 KB
testcase_63 AC 1,016 ms
104,312 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,L,K;
int X[202020],Y[202020];
vector<int> S[202020];
ll dp[202020][103];
ll pat[103][103][103];
const ll mo=998244353;
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;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M>>L>>K;
	FOR(i,M) {
		cin>>X[i+1]>>Y[i+1];
		S[i].push_back(i);
		S[i].push_back(i+1);
	}
	X[M+1]=Y[M+1]=N;
	S[M].push_back(M);
	S[M].push_back(M+1);
	FOR(i,L) {
		j=i+M+2;
		cin>>X[j]>>Y[j];
		x=lower_bound(X,X+M+2,X[j])-X;
		if(X[x]==X[j]) {
			if(Y[x]==Y[j]) K--;
			if(Y[j]<Y[x]&&Y[x-1]<=Y[j]) S[x-1].push_back(j);
			if(Y[j]>Y[x]&&Y[j]<=Y[x+1]) S[x].push_back(j);
		}
		else {
			if(Y[j]>=Y[x-1]&&Y[j]<=Y[x]) S[x-1].push_back(j);
		}
	}
	if(K<0) return _P("0\n");
	K=min(K,L);
	
	dp[0][K]=1;
	FOR(i,M+1) {
		vector<pair<int,int>> C;
		FORR(s,S[i]) C.push_back({X[s]+Y[s],s});
		sort(ALL(C));
		FOR(x,C.size()) FOR(y,C.size()) FOR(r,C.size()+1) pat[x][y][r]=0;
		FOR(x,C.size()) pat[x][x][0]=1;
		for(int len=1;len<=C.size();len++) {
			for(int f=0;f+len<C.size();f++) {
				int t=f+len;
				//direct
				int a=C[f].second;
				int b=C[t].second;
				if(X[a]>X[b]||Y[a]>Y[b]) continue;
				pat[f][t][1]=comb(Y[b]+X[b]-Y[a]-X[a],Y[b]-Y[a]);
				for(int m=f+1;m<t;m++) {
					int x=C[m].second;
					if(X[a]>X[x]||Y[a]>Y[x]) continue;
					if(X[x]>X[b]||Y[x]>Y[b]) continue;
					(pat[f][t][1]+=mo-pat[f][m][1]*comb(Y[b]+X[b]-Y[x]-X[x],Y[b]-Y[x])%mo)%=mo;
					for(j=1;j<=C.size()-1;j++) (pat[f][t][1+j]+=pat[f][m][1]*pat[m][t][j])%=mo;
				}
			}
		}
		FOR(x,101) for(y=1;y<=x+1;y++) (dp[i+1][x-(y-1)]+=dp[i][x]*pat[0][C.size()-1][y])%=mo;
	}
	
	ll ret=0;
	FOR(x,K+1) ret+=dp[M+1][x];
	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