結果
問題 |
No.767 配られたジャパリまん
|
ユーザー |
|
提出日時 | 2018-12-15 01:05:30 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,852 bytes |
コンパイル時間 | 2,446 ms |
コンパイル使用メモリ | 204,636 KB |
最終ジャッジ日時 | 2025-01-06 19:24:10 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 6 |
other | AC * 14 TLE * 1 |
ソースコード
#include <bits/stdc++.h> #define show(x) std::cerr << #x << " = " << (x) << std::endl using ll = long long; constexpr ll MOD = 100000007; template <ll mod = MOD> class ModCombination { public: ModCombination(const std::size_t n) : fact(n + 1, 1), inv(n + 1, 1), inv_fact(n + 1, 1) { for (ll i = 2; i <= (ll)n; i++) { fact[i] = (fact[i - 1] * i) % mod, inv[i] = ((mod - (mod / i)) * inv[mod % i]) % mod, inv_fact[i] = (inv_fact[i - 1] * inv[i]) % mod; } } ll factorial(const std::size_t n) const { return fact[n]; } ll inverse(const std::size_t n) const { return inv[n]; } ll inverseFactorial(const std::size_t n) const { return inv_fact[n]; } ll permutation(const std::size_t n, const std::size_t k) const { return (fact[n] * inv_fact[n - k]) % mod; } ll combination(const std::size_t n, const std::size_t k) const { return (((fact[n] * inv_fact[k]) % mod) * inv_fact[n - k]) % mod; } private: std::vector<ll> fact, inv, inv_fact; }; template <typename T, typename A> std::ostream& operator<<(std::ostream& os, const std::vector<T, A>& v) { os << "["; for (const auto& p : v) { os << p << ","; } return (os << "]\n"); } int main() { int H, W, K; std::cin >> H >> W >> K; ModCombination<> mod(H + W); std::vector<int> a(K + 2), b(K + 2); a[0] = b[0] = 0, a[K + 1] = H, b[K + 1] = W; for (int i = 1; i <= K; i++) { std::cin >> a[i] >> b[i]; } std::vector<std::vector<ll>> d(K + 2, std::vector<ll>(K + 2, 0)); for (int i = 0; i < K + 2; i++) { for (int j = 0; j < K + 2; j++) { if (a[i] <= a[j] and b[i] <= b[j]) { d[i][j] = mod.combination(a[j] + b[j] - a[i] - b[i], a[j] - a[i]); } } } std::vector<std::vector<ll>> memo1(K + 2, std::vector<ll>(K + 2, -1)); auto dp1 = [&](auto&& self, const int s, const int g) -> ll { if (memo1[s][g] != -1) { return memo1[s][g]; } ll ans = d[s][g]; for (int i = 1; i <= K; i++) { if (i == s or i == g or d[i][g] == 0) { continue; } const ll minus = self(self, s, i) * d[i][g] % MOD; (ans += (MOD - minus)) %= MOD; } return memo1[s][g] = ans; }; for (int q = 0; q < (1 << K); q++) { auto at = [&](const int j) -> bool { return (q >> j) & 1; }; std::vector<ll> memo2(K + 2, -1); auto dp2 = [&](auto&& self, const int s) { if (memo2[s] != -1) { return memo2[s]; } ll ans = dp1(dp1, s, K + 1); for (int i = 1; i <= K; i++) { if (s == i or d[s][i] == 0 or at(i - 1)) { continue; } (ans += dp1(dp1, s, i) * self(self, i) % MOD) %= MOD; } return memo2[s] = ans; }; std::cout << dp2(dp2, 0) << std::endl; // show(memo2); } // show(memo1); return 0; }