답안 #841142

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
841142 2023-09-01T09:46:30 Z peijar Amusement Park (CEOI19_amusementpark) C++17
100 / 100
2023 ms 608436 KB
#include <bits/extc++.h> /** keep-include */
#include <bits/stdc++.h>
#define int long long
using namespace std;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
  bool first = true;
  string res = "[";
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "]";
  return res;
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << to_string(H);
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

using ll = int;
// To use most bits rather than just the lowest ones:
struct chash { // large odd number for C
  const uint64_t C = ll(4e18 * acos(0)) | 71;
  ll operator()(ll x) const { return __builtin_bswap64(x * C); }
};

using hash_table = __gnu_pbds::gp_hash_table<int, int, chash>;

template <const int32_t MOD> struct ModInt {
  int32_t x;
  ModInt() : x(0) {}
  ModInt(long long u) : x(u % MOD) {
    if (x < 0)
      x += MOD;
  }
  friend bool operator==(const ModInt &a, const ModInt &b) {
    return a.x == b.x;
  }
  friend bool operator!=(const ModInt &a, const ModInt &b) {
    return a.x != b.x;
  }
  friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; }
  friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; }
  friend bool operator<=(const ModInt &a, const ModInt &b) {
    return a.x <= b.x;
  }
  friend bool operator>=(const ModInt &a, const ModInt &b) {
    return a.x >= b.x;
  }
  static ModInt sign(long long k) {
    return ((k & 1) ? ModInt(MOD - 1) : ModInt(1));
  }

  ModInt &operator+=(const ModInt &m) {
    x += m.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &m) {
    x -= m.x;
    if (x < 0LL)
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &m) {
    x = (1LL * x * m.x) % MOD;
    return *this;
  }

  friend ModInt operator-(const ModInt &a) {
    ModInt res(a);
    if (res.x)
      res.x = MOD - res.x;
    return res;
  }
  friend ModInt operator+(const ModInt &a, const ModInt &b) {
    return ModInt(a) += ModInt(b);
  }
  friend ModInt operator-(const ModInt &a, const ModInt &b) {
    return ModInt(a) -= ModInt(b);
  }
  friend ModInt operator*(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b);
  }

  static long long fp(long long u, long long k) {
    long long res = 1LL;
    while (k > 0LL) {
      if (k & 1LL)
        res = (res * u) % MOD;
      u = (u * u) % MOD;
      k /= 2LL;
    }
    return res;
  }

  static constexpr int mod() { return MOD; }

  ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
  ModInt inv() {
    assert(x);
    return ModInt(fp(x, MOD - 2));
  }
  ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); }
  friend ModInt operator/(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b).inv();
  }

  friend ostream &operator<<(ostream &out, const ModInt &a) {
    return out << a.x;
  }
  friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; }
  friend string to_string(ModInt u) { return to_string(u.x); }
};

// const int MOD = 1e9 + 7;
const int MOD = 998244353;
using Mint = ModInt<MOD>;

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  vector<int> adj(n);
  vector<int> bitAdj(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adj[u] |= 1 << v;
    bitAdj[u] |= 1 << v;
    bitAdj[v] |= 1 << u;
  }

  vector<int> areBad(n);
  for (int u = 0; u < n; ++u)
    for (int v = 0; v < u; ++v)
      if (!((1 << v) & bitAdj[u]))
        areBad[u] |= 1 << v;
  vector<vector<tuple<int, Mint, Mint>>> interesting(1 << n);

  interesting[0].emplace_back(0, 1, 0);
  for (int added = 0; added < (1 << n); ++added) {
    sort(interesting[added].begin(), interesting[added].end());
    vector<tuple<int, Mint, Mint>> compressed;
    for (auto [bad, ways, sum] : interesting[added]) {
      if (compressed.empty() or get<0>(compressed.back()) != bad)
        compressed.emplace_back(bad, 0, 0);
      auto &[b, w, s] = compressed.back();
      w += ways;
      s += sum;
    }
    int complement = (1 << n) - 1 - added;
    for (auto [bad, ways, sum] : compressed) {
      for (int nxt = 0; nxt < n; ++nxt)
        if (!((1 << nxt) & added) and !((1 << nxt) & bad)) {
          int nxtBad = bad & (bad ^ bitAdj[nxt]);
          nxtBad |= areBad[nxt] & complement;
          int cntFlip = __builtin_popcount(adj[nxt] & added);
          interesting[added ^ (1 << nxt)].emplace_back(nxtBad, ways,
                                                       cntFlip * ways + sum);
        }
    }
  }
  Mint sol = 0;
  for (auto [bad, ways, sum] : interesting[(1 << n) - 1]) {
    assert(!bad);
    sol += sum;
  }
  cout << sol << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 2 ms 688 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 2 ms 596 KB Output is correct
59 Correct 2 ms 468 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 1 ms 468 KB Output is correct
62 Correct 1 ms 468 KB Output is correct
63 Correct 1 ms 468 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 2 ms 688 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 2 ms 596 KB Output is correct
59 Correct 2 ms 468 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 1 ms 468 KB Output is correct
62 Correct 1 ms 468 KB Output is correct
63 Correct 1 ms 468 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 31 ms 10276 KB Output is correct
66 Correct 46 ms 14880 KB Output is correct
67 Correct 44 ms 14448 KB Output is correct
68 Correct 47 ms 15128 KB Output is correct
69 Correct 10 ms 4308 KB Output is correct
70 Correct 16 ms 5848 KB Output is correct
71 Correct 15 ms 5816 KB Output is correct
72 Correct 15 ms 5844 KB Output is correct
73 Correct 17 ms 6512 KB Output is correct
74 Correct 19 ms 6572 KB Output is correct
75 Correct 17 ms 6516 KB Output is correct
76 Correct 16 ms 6568 KB Output is correct
77 Correct 69 ms 19996 KB Output is correct
78 Correct 86 ms 26448 KB Output is correct
79 Correct 115 ms 36704 KB Output is correct
80 Correct 103 ms 33740 KB Output is correct
81 Correct 120 ms 38112 KB Output is correct
82 Correct 119 ms 37440 KB Output is correct
83 Correct 120 ms 37860 KB Output is correct
84 Correct 85 ms 27700 KB Output is correct
85 Correct 23 ms 9164 KB Output is correct
86 Correct 33 ms 12548 KB Output is correct
87 Correct 38 ms 12524 KB Output is correct
88 Correct 36 ms 12648 KB Output is correct
89 Correct 40 ms 14124 KB Output is correct
90 Correct 45 ms 14064 KB Output is correct
91 Correct 48 ms 14148 KB Output is correct
92 Correct 36 ms 14264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 2 ms 688 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 2 ms 596 KB Output is correct
59 Correct 2 ms 468 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 1 ms 468 KB Output is correct
62 Correct 1 ms 468 KB Output is correct
63 Correct 1 ms 468 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 31 ms 10276 KB Output is correct
66 Correct 46 ms 14880 KB Output is correct
67 Correct 44 ms 14448 KB Output is correct
68 Correct 47 ms 15128 KB Output is correct
69 Correct 10 ms 4308 KB Output is correct
70 Correct 16 ms 5848 KB Output is correct
71 Correct 15 ms 5816 KB Output is correct
72 Correct 15 ms 5844 KB Output is correct
73 Correct 17 ms 6512 KB Output is correct
74 Correct 19 ms 6572 KB Output is correct
75 Correct 17 ms 6516 KB Output is correct
76 Correct 16 ms 6568 KB Output is correct
77 Correct 69 ms 19996 KB Output is correct
78 Correct 86 ms 26448 KB Output is correct
79 Correct 115 ms 36704 KB Output is correct
80 Correct 103 ms 33740 KB Output is correct
81 Correct 120 ms 38112 KB Output is correct
82 Correct 119 ms 37440 KB Output is correct
83 Correct 120 ms 37860 KB Output is correct
84 Correct 85 ms 27700 KB Output is correct
85 Correct 23 ms 9164 KB Output is correct
86 Correct 33 ms 12548 KB Output is correct
87 Correct 38 ms 12524 KB Output is correct
88 Correct 36 ms 12648 KB Output is correct
89 Correct 40 ms 14124 KB Output is correct
90 Correct 45 ms 14064 KB Output is correct
91 Correct 48 ms 14148 KB Output is correct
92 Correct 36 ms 14264 KB Output is correct
93 Correct 438 ms 131928 KB Output is correct
94 Correct 137 ms 42912 KB Output is correct
95 Correct 67 ms 21608 KB Output is correct
96 Correct 464 ms 150604 KB Output is correct
97 Correct 444 ms 147724 KB Output is correct
98 Correct 402 ms 128548 KB Output is correct
99 Correct 95 ms 26920 KB Output is correct
100 Correct 176 ms 49848 KB Output is correct
101 Correct 270 ms 79076 KB Output is correct
102 Correct 321 ms 90664 KB Output is correct
103 Correct 22 ms 7508 KB Output is correct
104 Correct 27 ms 9180 KB Output is correct
105 Correct 87 ms 25308 KB Output is correct
106 Correct 371 ms 104820 KB Output is correct
107 Correct 44 ms 14620 KB Output is correct
108 Correct 193 ms 53824 KB Output is correct
109 Correct 828 ms 229128 KB Output is correct
110 Correct 1924 ms 561980 KB Output is correct
111 Correct 316 ms 96532 KB Output is correct
112 Correct 273 ms 89516 KB Output is correct
113 Correct 270 ms 88600 KB Output is correct
114 Correct 57 ms 19460 KB Output is correct
115 Correct 75 ms 27248 KB Output is correct
116 Correct 81 ms 27316 KB Output is correct
117 Correct 78 ms 27436 KB Output is correct
118 Correct 113 ms 30972 KB Output is correct
119 Correct 103 ms 31020 KB Output is correct
120 Correct 88 ms 31100 KB Output is correct
121 Correct 85 ms 30992 KB Output is correct
122 Correct 501 ms 147996 KB Output is correct
123 Correct 774 ms 228788 KB Output is correct
124 Correct 742 ms 233404 KB Output is correct
125 Correct 718 ms 227336 KB Output is correct
126 Correct 695 ms 210024 KB Output is correct
127 Correct 482 ms 154108 KB Output is correct
128 Correct 349 ms 106708 KB Output is correct
129 Correct 220 ms 69960 KB Output is correct
130 Correct 83 ms 31524 KB Output is correct
131 Correct 129 ms 42420 KB Output is correct
132 Correct 178 ms 59960 KB Output is correct
133 Correct 198 ms 59944 KB Output is correct
134 Correct 174 ms 60196 KB Output is correct
135 Correct 200 ms 68140 KB Output is correct
136 Correct 206 ms 68112 KB Output is correct
137 Correct 211 ms 68532 KB Output is correct
138 Correct 203 ms 68600 KB Output is correct
139 Correct 209 ms 71244 KB Output is correct
140 Correct 258 ms 71424 KB Output is correct
141 Correct 225 ms 71364 KB Output is correct
142 Correct 1799 ms 547704 KB Output is correct
143 Correct 1879 ms 562132 KB Output is correct
144 Correct 1415 ms 439568 KB Output is correct
145 Correct 1100 ms 347984 KB Output is correct
146 Correct 609 ms 190100 KB Output is correct
147 Correct 203 ms 65828 KB Output is correct
148 Correct 1756 ms 532324 KB Output is correct
149 Correct 1742 ms 553352 KB Output is correct
150 Correct 2023 ms 608436 KB Output is correct
151 Correct 374 ms 119568 KB Output is correct
152 Correct 388 ms 124940 KB Output is correct
153 Correct 482 ms 150516 KB Output is correct
154 Correct 466 ms 150504 KB Output is correct
155 Correct 500 ms 150680 KB Output is correct
156 Correct 483 ms 150872 KB Output is correct
157 Correct 487 ms 150652 KB Output is correct
158 Correct 507 ms 151340 KB Output is correct
159 Correct 530 ms 157424 KB Output is correct
160 Correct 511 ms 157892 KB Output is correct
161 Correct 512 ms 157560 KB Output is correct
162 Correct 496 ms 158228 KB Output is correct
163 Correct 501 ms 158544 KB Output is correct
164 Correct 506 ms 158744 KB Output is correct
165 Correct 508 ms 159228 KB Output is correct
166 Correct 505 ms 159572 KB Output is correct
167 Correct 504 ms 158824 KB Output is correct