結果

問題 No.1354 Sambo's Treasure
ユーザー jupiro
提出日時 2021-01-19 13:37:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 4,210 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 140,244 KB
最終ジャッジ日時 2025-01-18 02:38:29
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
constexpr long long  mod = 998244353;

struct mint {
	long long x;
	mint(long long x = 0) :x((x% mod + mod) % mod) {}
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};

const int MAX = (int)1e6;
long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}

mint COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return mint(fac[n] * (finv[k] * finv[n - k] % mod) % mod);
}

mint LCOM(ll n, ll k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	if (k > n / 2) k = n - k;
	mint res = 1;
	for (int i = 0; i < k; i++) res *= (n - i);
	res *= finv[k];
	return res;
}

int main()
{

	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);

	COMinit();
	int n, m, L, K; cin >> n >> m >> L >> K;
	vector<pair<int, int>> cp(m + 2); for (int i = 1; i <= m; i++) cin >> cp[i].first >> cp[i].second;
	cp[m + 1] = { n,n };
	vector<pair<int, int>> tg(L); for (auto& p : tg) cin >> p.first >> p.second;
	sort(tg.begin(), tg.end());
	int tcur = 0;
	vector dp1(2, vector<mint>(L + 1));
	int cur = 0;
	int nxt = 1;
	dp1[0][0] = 1;
	for (int i = 0; i + 1 < (int)cp.size(); i++) {
		vector<pair<int, int>> vp; vp.emplace_back(cp[i]);
		while (tcur < L and tg[tcur] <= cp[i + 1]) vp.emplace_back(tg[tcur++]);
		vp.emplace_back(cp[i + 1]);
		int sz = (int)vp.size();
		vector dp2(sz, vector<mint>(sz + 1));
		dp2[0][0] = 1;
		for (int j = 1; j < sz; j++) {
			for (int k = sz; k >= 0; k--) {
				for (int l = 0; l < j; l++) {
					int dx = vp[j].first - vp[l].first;
					int dy = vp[j].second - vp[l].second;
					if (dx < 0 or dy < 0) continue;
					if (j + 1 < sz and k > 0) dp2[j][k] += dp2[l][k - 1] * COM(dx + dy, dx);
					if (j + 1 == sz) dp2[j][k] += dp2[l][k] * COM(dx + dy, dx);
				}
				if ((j + 1 < sz and k > 0) or j + 1 == sz) for (int l = k + 1; l <= sz; l++) dp2[j][k] -= dp2[j][l];
			}
		}
		for (int j = 0; j <= L; j++) dp1[nxt][j] = 0;
		for (int j = 0; j <= L; j++) {
			for (int k = 0; k <= sz; k++) {
				if (j + k <= L) {
					dp1[nxt][j + k] += dp1[cur][j] * dp2.back()[k];
				}
			}
		}
		swap(cur, nxt);
	}
	mint res = 0; for (int i = 0; i <= min(L, K); i++) res += dp1[cur][i];
	cout << res.x << "\n";
}
0