結果

問題 No.1354 Sambo's Treasure
ユーザー QCFiumQCFium
提出日時 2021-01-17 15:42:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,742 bytes
コンパイル時間 1,981 ms
コンパイル使用メモリ 187,940 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-05-07 05:04:34
合計ジャッジ時間 5,633 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 2 ms
5,376 KB
testcase_21 WA -
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 73 ms
5,376 KB
testcase_24 AC 76 ms
5,376 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 73 ms
5,376 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 72 ms
5,376 KB
testcase_34 WA -
testcase_35 AC 77 ms
5,376 KB
testcase_36 WA -
testcase_37 AC 73 ms
5,376 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 73 ms
5,376 KB
testcase_42 WA -
testcase_43 AC 9 ms
6,528 KB
testcase_44 AC 8 ms
6,528 KB
testcase_45 AC 8 ms
6,528 KB
testcase_46 AC 8 ms
6,400 KB
testcase_47 AC 8 ms
6,400 KB
testcase_48 AC 8 ms
6,400 KB
testcase_49 AC 8 ms
6,400 KB
testcase_50 AC 8 ms
6,528 KB
testcase_51 AC 9 ms
6,528 KB
testcase_52 AC 8 ms
6,400 KB
testcase_53 AC 8 ms
6,528 KB
testcase_54 AC 8 ms
6,400 KB
testcase_55 AC 7 ms
6,528 KB
testcase_56 AC 8 ms
6,400 KB
testcase_57 AC 8 ms
6,400 KB
testcase_58 AC 8 ms
6,528 KB
testcase_59 AC 8 ms
6,400 KB
testcase_60 AC 8 ms
6,528 KB
testcase_61 AC 8 ms
6,528 KB
testcase_62 AC 8 ms
6,400 KB
testcase_63 AC 74 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}


template<int mod>
struct ModInt{
	int x;
	ModInt () : x(0) {}
	ModInt (int64_t x) : x(x >= 0 ? x % mod : (mod - -x % mod) % mod) {}
	ModInt &operator += (const ModInt &p){
		if ((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator -= (const ModInt &p) {
		if ((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator *= (const ModInt &p) {
		x = (int64_t) x * p.x % mod;
		return *this;
	}
	ModInt &operator /= (const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt &operator ^= (int64_t p) {
		ModInt res = 1;
		for (; p; p >>= 1) {
			if (p & 1) res *= *this;
			*this *= *this;
		}
		return *this = res;
	}
	ModInt operator - () const { return ModInt(-x); }
	ModInt operator + (const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator - (const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator * (const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator / (const ModInt &p) const { return ModInt(*this) /= p; }
	ModInt operator ^ (int64_t p) const { return ModInt(*this) ^= p; }
	bool operator == (const ModInt &p) const { return x == p.x; }
	bool operator != (const ModInt &p) const { return x != p.x; }
	explicit operator int() const { return x; }
	ModInt &operator = (const int p) { x = p; return *this;}
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while (b > 0) {
			t = a / b;
			a -= t * b;
			std::swap(a, b);
			u -= t * v;
			std::swap(u, v);
		}
		return ModInt(u);
	}
	friend std::ostream & operator << (std::ostream &stream, const ModInt<mod> &p) {
		return stream << p.x;
	}
	friend std::istream & operator >> (std::istream &stream, ModInt<mod> &a) {
		int64_t x;
		stream >> x;
		a = ModInt<mod>(x);
		return stream;
	}
};
typedef ModInt<998244353> mint;

template<int mod> struct MComb {
	using mint = ModInt<mod>;
	std::vector<mint> fact;
	std::vector<mint> inv;
	MComb (int n) { // O(n + log(mod))
		fact = std::vector<mint>(n + 1, 1);
		for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * mint(i);
		inv.resize(n + 1);
		inv[n] = fact[n] ^ (mod - 2);
		for (int i = n; i--; ) inv[i] = inv[i + 1] * mint(i + 1);
	}
	mint ncr(int n, int r) {
		return fact[n] * inv[r] * inv[n - r];
	}
	mint npr(int n, int r) {
		return fact[n] * inv[n - r];
	}
	mint nhr(int n, int r) {
		assert(n + r - 1 < (int) fact.size());
		return ncr(n + r - 1, r);
	}
};

std::vector<mint> solve(std::vector<std::pair<int, int> > pts, int h, int w) {
	/*
	bool map[h + 1][w + 1];
	memset(map, 0, sizeof(map));
	for (auto i : pts) map[i.first][i.second] = true;
	std::vector<mint> dp[h + 1][w + 1];
	dp[0][0] = { 1 };
	for (int i = 0; i <= h; i++) for (int j = 0; j <= w; j++) {
		if (map[i][j]) dp[i][j].insert(dp[i][j].begin(), 0);
		
		if (i < h) {
			while (dp[i + 1][j].size() < dp[i][j].size()) dp[i + 1][j].push_back(0);
			for (int k = 0; k < (int) dp[i][j].size(); k++) dp[i + 1][j][k] += dp[i][j][k];
		}
		if (j < w) {
			while (dp[i][j + 1].size() < dp[i][j].size()) dp[i][j + 1].push_back(0);
			for (int k = 0; k < (int) dp[i][j].size(); k++) dp[i][j + 1][k] += dp[i][j][k];
		}
	}
	return dp[h][w];*/
	
	pts.insert(pts.begin(), {0, 0});
	pts.push_back({h, w});
	int n = pts.size();
	
	MComb<998244353> com(h + w);
	
	std::vector<mint> dp[n];
	dp[0] = { 1 };
	for (int i = 0; i < n; i++) {
		/*
		for (auto j : dp[i]) std::cerr << j << " ";
		std::cerr << std::endl;*/
		mint tmp = 0;
		for (int j = dp[i].size(); --j; ) {
			dp[i][j] -= tmp;
			tmp += dp[i][j];
		}
		/*
		for (auto j : dp[i]) std::cerr << j << " ";
		std::cerr << std::endl;*/
		
		for (int j = i + 1; j < n; j++) {
			if (pts[j].second < pts[i].second) continue;
			mint times = com.ncr(pts[j].first - pts[i].first + pts[j].second - pts[i].second, pts[j].first - pts[i].first);
			while (dp[j].size() <= dp[i].size()) dp[j].push_back(0);
			for (int k = 0; k < (int) dp[i].size(); k++) dp[j][k + 1] += dp[i][k] * times;
		}
	}
	auto res = dp[n - 1];
	res.erase(res.begin());
	return res;
}

int main() {
	int n = ri();
	int m = ri();
	int l = ri();
	int k = ri();
	std::vector<std::pair<int, int> > check_pts(m);
	for (auto &i : check_pts) i.first = ri(), i.second = ri();
	check_pts.insert(check_pts.begin(), std::pair<int, int>(0, 0));
	check_pts.push_back({n, n});
	std::vector<std::pair<int, int> > tigers(l);
	for (auto &i : tigers) i.first = ri(), i.second = ri();
	
	
	std::sort(tigers.begin(), tigers.end());
	{
		std::vector<std::pair<int, int> > tmp;
		for (int i = 0; i < l; i++) {
			int x = tigers[i].first;
			int y = tigers[i].second;
			auto itr = std::upper_bound(check_pts.begin(), check_pts.end(), std::pair<int, int>{x, 1000000000});
			if (y < std::prev(itr)->second || y > itr->second) continue;
			tmp.push_back(tigers[i]);
		}
		tigers = tmp;
		l = tigers.size();
	}
	int head = 0;
	std::vector<mint> dp{1};
	for (int i = 1; i < (int) check_pts.size(); i++) {
		int old_head = head;
		while (head < l && tigers[head].first <= check_pts[i].first) head++;
		std::vector<std::pair<int, int> > pts;
		for (int j = old_head; j < head; j++) pts.push_back({tigers[j].first - check_pts[i - 1].first, tigers[j].second - check_pts[i - 1].second});
		auto tmp = solve(pts, check_pts[i].first - check_pts[i - 1].first, check_pts[i].second - check_pts[i - 1].second);
		
		/*
		for (auto i: tmp) std::cerr << i << " ";
		std::cerr << std::endl;*/
		
		std::vector<mint> next(dp.size() + tmp.size() - 1);
		for (int i = 0; i < (int) dp.size(); i++) for (int j = 0; j < (int) tmp.size(); j++) next[i + j] += dp[i] * tmp[j];
		dp = next;
	}
	mint res = 0;
	for (int i = 0; i < (int) dp.size() && i <= k; i++) res += dp[i];
	std::cout << res << std::endl;
	return 0;
}
0