結果
問題 | No.2983 Christmas Color Grid (Advent Calender ver.) |
ユーザー | 👑 Nachia |
提出日時 | 2024-12-08 17:04:37 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 372 ms / 3,340 ms |
コード長 | 4,842 bytes |
コンパイル時間 | 1,123 ms |
コンパイル使用メモリ | 94,264 KB |
実行使用メモリ | 40,296 KB |
最終ジャッジ日時 | 2024-12-08 17:04:42 |
合計ジャッジ時間 | 5,100 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 372 ms
40,296 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 193 ms
21,740 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 115 ms
13,552 KB |
testcase_10 | AC | 6 ms
5,248 KB |
testcase_11 | AC | 2 ms
5,248 KB |
testcase_12 | AC | 2 ms
5,248 KB |
testcase_13 | AC | 2 ms
5,248 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 2 ms
5,248 KB |
testcase_16 | AC | 3 ms
5,248 KB |
testcase_17 | AC | 4 ms
5,248 KB |
testcase_18 | AC | 4 ms
7,296 KB |
testcase_19 | AC | 5 ms
5,248 KB |
testcase_20 | AC | 12 ms
5,248 KB |
testcase_21 | AC | 27 ms
7,404 KB |
testcase_22 | AC | 25 ms
5,740 KB |
testcase_23 | AC | 133 ms
13,548 KB |
testcase_24 | AC | 19 ms
5,248 KB |
testcase_25 | AC | 186 ms
21,868 KB |
testcase_26 | AC | 19 ms
5,248 KB |
testcase_27 | AC | 372 ms
40,172 KB |
testcase_28 | AC | 186 ms
21,868 KB |
testcase_29 | AC | 23 ms
5,740 KB |
testcase_30 | AC | 114 ms
13,676 KB |
testcase_31 | AC | 6 ms
5,248 KB |
testcase_32 | AC | 10 ms
5,248 KB |
testcase_33 | AC | 24 ms
7,404 KB |
testcase_34 | AC | 2 ms
5,248 KB |
testcase_35 | AC | 2 ms
5,248 KB |
testcase_36 | AC | 3 ms
5,248 KB |
testcase_37 | AC | 3 ms
5,248 KB |
testcase_38 | AC | 3 ms
5,248 KB |
testcase_39 | AC | 3 ms
7,296 KB |
testcase_40 | AC | 2 ms
5,248 KB |
testcase_41 | AC | 2 ms
5,248 KB |
testcase_42 | AC | 1 ms
5,248 KB |
testcase_43 | AC | 2 ms
5,248 KB |
testcase_44 | AC | 2 ms
5,248 KB |
testcase_45 | AC | 2 ms
5,248 KB |
testcase_46 | AC | 2 ms
5,248 KB |
testcase_47 | AC | 2 ms
5,248 KB |
testcase_48 | AC | 2 ms
5,248 KB |
testcase_49 | AC | 2 ms
5,248 KB |
testcase_50 | AC | 2 ms
5,248 KB |
testcase_51 | AC | 2 ms
5,248 KB |
testcase_52 | AC | 2 ms
5,248 KB |
testcase_53 | AC | 2 ms
5,248 KB |
testcase_54 | AC | 2 ms
5,248 KB |
testcase_55 | AC | 1 ms
5,248 KB |
testcase_56 | AC | 2 ms
5,248 KB |
testcase_57 | AC | 2 ms
5,248 KB |
testcase_58 | AC | 2 ms
5,248 KB |
testcase_59 | AC | 2 ms
5,248 KB |
testcase_60 | AC | 2 ms
5,248 KB |
testcase_61 | AC | 2 ms
5,248 KB |
testcase_62 | AC | 2 ms
5,248 KB |
testcase_63 | AC | 2 ms
5,248 KB |
testcase_64 | AC | 2 ms
5,248 KB |
testcase_65 | AC | 2 ms
5,248 KB |
testcase_66 | AC | 2 ms
5,248 KB |
ソースコード
#include <iostream> #include <string> #include <vector> #include <algorithm> using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<int(n); i++) const i64 INF = 1001001001001001001; template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; } template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; #include <atcoder/modint> namespace nachia{ template<class Modint> class Comb{ private: std::vector<Modint> F; std::vector<Modint> iF; public: void extend(int newN){ int prevN = (int)F.size() - 1; if(prevN >= newN) return; F.resize(newN+1); iF.resize(newN+1); for(int i=prevN+1; i<=newN; i++) F[i] = F[i-1] * Modint::raw(i); iF[newN] = F[newN].inv(); for(int i=newN; i>prevN; i--) iF[i-1] = iF[i] * Modint::raw(i); } Comb(int n = 1){ F.assign(2, Modint(1)); iF.assign(2, Modint(1)); extend(n); } Modint factorial(int n) const { return F[n]; } Modint invFactorial(int n) const { return iF[n]; } Modint invOf(int n) const { return iF[n] * F[n-1]; } Modint comb(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return F[n] * iF[r] * iF[n-r]; } Modint invComb(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return iF[n] * F[r] * F[n-r]; } Modint perm(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return F[n] * iF[n-r]; } Modint invPerm(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return iF[n] * F[n-r]; } Modint operator()(int n, int r) const { return comb(n,r); } }; } // namespace nachia namespace nachia{ int Popcount(unsigned long long c) noexcept { #ifdef __GNUC__ return __builtin_popcountll(c); #else c = (c & (~0ull/3)) + ((c >> 1) & (~0ull/3)); c = (c & (~0ull/5)) + ((c >> 2) & (~0ull/5)); c = (c & (~0ull/17)) + ((c >> 4) & (~0ull/17)); c = (c * (~0ull/257)) >> 56; return c; #endif } // please ensure x != 0 int MsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return 63 - __builtin_clzll(x); #else using u64 = unsigned long long; int q = (x >> 32) ? 32 : 0; auto m = x >> q; constexpr u64 hi = 0x88888888; constexpr u64 mi = 0x11111111; m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 35; m = (((m | ~(hi - (x & ~hi))) & hi) * mi) >> 31; q += (m & 0xf) << 2; q += 0x3333333322221100 >> (((x >> q) & 0xf) << 2) & 0xf; return q; #endif } // please ensure x != 0 int LsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return __builtin_ctzll(x); #else return MsbIndex(x & -x); #endif } } using Modint = atcoder::modint; void testcase(){ i64 H, W; cin >> H >> W; i64 K, M; cin >> K >> M; Modint::set_mod(M); vector<i64> adj(H*W); auto Idx = [&](i64 y, i64 x) -> i64 { return y * W + x; }; auto IdxBit = [&](i64 y, i64 x) -> i64 { return i64(1) << Idx(y,x); }; rep(y,H) rep(x,W-1) adj[Idx(y,x)] |= IdxBit(y,x+1); rep(y,H) rep(x,W-1) adj[Idx(y,x+1)] |= IdxBit(y,x); rep(y,H-1) rep(x,W) adj[Idx(y,x)] |= IdxBit(y+1,x); rep(y,H-1) rep(x,W) adj[Idx(y+1,x)] |= IdxBit(y,x); i64 N = H*W; auto comb = nachia::Comb<Modint>(N+1); vector<Modint> ipow2(N+1, 1); rep(i,N) ipow2[i+1] = ipow2[i] / 2; vector<vector<Modint>> F(N+1, vector<Modint>(N+1)); rep(i,N+1) rep(j,N+1) if(i+j <= N){ Modint t1 = Modint(i).pow(K); //int k = N - i - j; //for(i64 f=0; f<=k; f++){ // 非隣接で塗る個数 // Modint t2 = t1 * comb(k, f); // for(i64 e=0; e<=j; e++){ // 隣接で塗る個数 // Modint t3 = t2 * comb(j,e) * comb.invOf((k-f)+(j-e)+1); // t3 *= comb.invComb(N, i+e+f) * ipow2[i+e]; // F[i][j] += t3; // } //} F[i][j] += t1 * ipow2[i+j]; } Modint ans = 0; vector<bool> vis(1<<(N)); vector<i64> bfs; rep(y,H) rep(x,W) bfs.push_back(IdxBit(y,x)); for(i64 v : bfs) vis[v] = 1; rep(i,bfs.size()){ i64 v = bfs[i]; i64 w = 0; rep(j,N) if((v>>j)&1) w |= adj[j]; w -= v & w; int pcv = nachia::Popcount(v); int pcw = nachia::Popcount(w); //cout << "pcv = " << pcv << " , pcw = " << pcw << endl; ans += F[pcv][pcw]; rep(j,N) if((w>>j)&1){ i64 nx = v | i64(1) << j; if(vis[nx]) continue; vis[nx] = 1; bfs.push_back(nx); } } Modint t = 0; for(int i=1; i<=N; i++) t += comb.invOf(i); ans *= t; cout << ans.val() << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }