Submission #965920

# Submission time Handle Problem Language Result Execution time Memory
965920 2024-04-19T07:56:41 Z four_specks Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
604 ms 43428 KB
#include <bits/stdc++.h>

using namespace std;

namespace {}

void solve() {
  int h, q;
  cin >> h >> q;
  uint32_t n = 1 << h;
  string a;
  cin >> a;
  vector<int> b(n);
  for (uint32_t x = 0; x < n; x++) {
    b[x] = a[x] - '0';
  }
  vector dp_sub = b, dp_super = b;
  for (int s = 0; s < h; s++) {
    for (uint32_t x = n - 1; x + 1 > 0; x--) {
      if (x >> s & 1) {
        dp_sub[x] += dp_sub[x ^ (1u << s)];
      }
    }
    for (uint32_t x = 0; x < n; x++) {
      if (!(x >> s & 1)) {
        dp_super[x] += dp_super[x | (1u << s)];
      }
    }
  }
  for (int i = 0; i < q; i++) {
    string t;
    cin >> t;
    reverse(t.begin(), t.end());
    array<uint32_t, 3> v;
    v.fill(0);
    for (int s = 0; s < h; s++) {
      if (t[s] == '0') {
        v[0] |= 1u << s;
      } else if (t[s] == '1') {
        v[1] |= 1u << s;
      } else {
        v[2] |= 1u << s;
      }
    }
    int ans = 0;
    {
      auto it = min_element(v.begin(), v.end(), [&](uint32_t x, uint32_t y) -> bool {
        return __builtin_popcount(x) < __builtin_popcount(y);
      });
      int p = distance(v.begin(), it);
      for (uint32_t x = v[p];; x = (x - 1) & v[p]) {
        if (p == 2) {
          ans += b[v[1] | x];
        } else {
          int term = p == 1 ? dp_sub[(v[1] ^ x) | v[2]] : dp_super[v[1] | x];
          if (__builtin_parity(x) == 0) {
            ans += term;
          } else {
            ans -= term;
          }
        }
        if (x == 0) {
          break;
        }
      }
    }
    cout << ans << '\n';
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  solve();

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 173 ms 15164 KB Output is correct
12 Correct 178 ms 14672 KB Output is correct
13 Correct 205 ms 13908 KB Output is correct
14 Correct 210 ms 13960 KB Output is correct
15 Correct 192 ms 15164 KB Output is correct
16 Correct 231 ms 14144 KB Output is correct
17 Correct 218 ms 14272 KB Output is correct
18 Correct 138 ms 16028 KB Output is correct
19 Correct 169 ms 13032 KB Output is correct
20 Correct 217 ms 14632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 173 ms 15164 KB Output is correct
12 Correct 178 ms 14672 KB Output is correct
13 Correct 205 ms 13908 KB Output is correct
14 Correct 210 ms 13960 KB Output is correct
15 Correct 192 ms 15164 KB Output is correct
16 Correct 231 ms 14144 KB Output is correct
17 Correct 218 ms 14272 KB Output is correct
18 Correct 138 ms 16028 KB Output is correct
19 Correct 169 ms 13032 KB Output is correct
20 Correct 217 ms 14632 KB Output is correct
21 Correct 205 ms 18000 KB Output is correct
22 Correct 212 ms 18316 KB Output is correct
23 Correct 250 ms 17220 KB Output is correct
24 Correct 258 ms 17236 KB Output is correct
25 Correct 213 ms 19092 KB Output is correct
26 Correct 265 ms 17744 KB Output is correct
27 Correct 252 ms 17676 KB Output is correct
28 Correct 163 ms 20148 KB Output is correct
29 Correct 200 ms 15996 KB Output is correct
30 Correct 228 ms 18216 KB Output is correct
31 Correct 237 ms 18060 KB Output is correct
32 Correct 255 ms 18004 KB Output is correct
33 Correct 214 ms 16956 KB Output is correct
34 Correct 251 ms 17228 KB Output is correct
35 Correct 253 ms 17560 KB Output is correct
36 Correct 149 ms 16104 KB Output is correct
37 Correct 191 ms 18256 KB Output is correct
38 Correct 206 ms 16212 KB Output is correct
39 Correct 230 ms 17232 KB Output is correct
40 Correct 254 ms 17100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 62 ms 16112 KB Output is correct
12 Correct 63 ms 16204 KB Output is correct
13 Correct 63 ms 15968 KB Output is correct
14 Correct 72 ms 15952 KB Output is correct
15 Correct 55 ms 16172 KB Output is correct
16 Correct 71 ms 16076 KB Output is correct
17 Correct 77 ms 16116 KB Output is correct
18 Correct 52 ms 16344 KB Output is correct
19 Correct 60 ms 15860 KB Output is correct
20 Correct 65 ms 16208 KB Output is correct
21 Correct 63 ms 16188 KB Output is correct
22 Correct 67 ms 16116 KB Output is correct
23 Correct 66 ms 16116 KB Output is correct
24 Correct 68 ms 15956 KB Output is correct
25 Correct 78 ms 16120 KB Output is correct
26 Correct 53 ms 15972 KB Output is correct
27 Correct 56 ms 16236 KB Output is correct
28 Correct 61 ms 15860 KB Output is correct
29 Correct 59 ms 16112 KB Output is correct
30 Correct 61 ms 15948 KB Output is correct
31 Correct 58 ms 16116 KB Output is correct
32 Correct 71 ms 15952 KB Output is correct
33 Correct 70 ms 16120 KB Output is correct
34 Correct 54 ms 15920 KB Output is correct
35 Correct 63 ms 16116 KB Output is correct
36 Correct 61 ms 15964 KB Output is correct
37 Correct 63 ms 15952 KB Output is correct
38 Correct 63 ms 15972 KB Output is correct
39 Correct 61 ms 15956 KB Output is correct
40 Correct 69 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 173 ms 15164 KB Output is correct
12 Correct 178 ms 14672 KB Output is correct
13 Correct 205 ms 13908 KB Output is correct
14 Correct 210 ms 13960 KB Output is correct
15 Correct 192 ms 15164 KB Output is correct
16 Correct 231 ms 14144 KB Output is correct
17 Correct 218 ms 14272 KB Output is correct
18 Correct 138 ms 16028 KB Output is correct
19 Correct 169 ms 13032 KB Output is correct
20 Correct 217 ms 14632 KB Output is correct
21 Correct 205 ms 18000 KB Output is correct
22 Correct 212 ms 18316 KB Output is correct
23 Correct 250 ms 17220 KB Output is correct
24 Correct 258 ms 17236 KB Output is correct
25 Correct 213 ms 19092 KB Output is correct
26 Correct 265 ms 17744 KB Output is correct
27 Correct 252 ms 17676 KB Output is correct
28 Correct 163 ms 20148 KB Output is correct
29 Correct 200 ms 15996 KB Output is correct
30 Correct 228 ms 18216 KB Output is correct
31 Correct 237 ms 18060 KB Output is correct
32 Correct 255 ms 18004 KB Output is correct
33 Correct 214 ms 16956 KB Output is correct
34 Correct 251 ms 17228 KB Output is correct
35 Correct 253 ms 17560 KB Output is correct
36 Correct 149 ms 16104 KB Output is correct
37 Correct 191 ms 18256 KB Output is correct
38 Correct 206 ms 16212 KB Output is correct
39 Correct 230 ms 17232 KB Output is correct
40 Correct 254 ms 17100 KB Output is correct
41 Correct 62 ms 16112 KB Output is correct
42 Correct 63 ms 16204 KB Output is correct
43 Correct 63 ms 15968 KB Output is correct
44 Correct 72 ms 15952 KB Output is correct
45 Correct 55 ms 16172 KB Output is correct
46 Correct 71 ms 16076 KB Output is correct
47 Correct 77 ms 16116 KB Output is correct
48 Correct 52 ms 16344 KB Output is correct
49 Correct 60 ms 15860 KB Output is correct
50 Correct 65 ms 16208 KB Output is correct
51 Correct 63 ms 16188 KB Output is correct
52 Correct 67 ms 16116 KB Output is correct
53 Correct 66 ms 16116 KB Output is correct
54 Correct 68 ms 15956 KB Output is correct
55 Correct 78 ms 16120 KB Output is correct
56 Correct 53 ms 15972 KB Output is correct
57 Correct 56 ms 16236 KB Output is correct
58 Correct 61 ms 15860 KB Output is correct
59 Correct 59 ms 16112 KB Output is correct
60 Correct 61 ms 15948 KB Output is correct
61 Correct 58 ms 16116 KB Output is correct
62 Correct 71 ms 15952 KB Output is correct
63 Correct 70 ms 16120 KB Output is correct
64 Correct 54 ms 15920 KB Output is correct
65 Correct 63 ms 16116 KB Output is correct
66 Correct 61 ms 15964 KB Output is correct
67 Correct 63 ms 15952 KB Output is correct
68 Correct 63 ms 15972 KB Output is correct
69 Correct 61 ms 15956 KB Output is correct
70 Correct 69 ms 15956 KB Output is correct
71 Correct 343 ms 40324 KB Output is correct
72 Correct 334 ms 40580 KB Output is correct
73 Correct 412 ms 38904 KB Output is correct
74 Correct 521 ms 39396 KB Output is correct
75 Correct 352 ms 41512 KB Output is correct
76 Correct 581 ms 39740 KB Output is correct
77 Correct 604 ms 39664 KB Output is correct
78 Correct 233 ms 43428 KB Output is correct
79 Correct 337 ms 37300 KB Output is correct
80 Correct 387 ms 40600 KB Output is correct
81 Correct 426 ms 40280 KB Output is correct
82 Correct 530 ms 39284 KB Output is correct
83 Correct 315 ms 38608 KB Output is correct
84 Correct 582 ms 39416 KB Output is correct
85 Correct 508 ms 39524 KB Output is correct
86 Correct 234 ms 37392 KB Output is correct
87 Correct 310 ms 40432 KB Output is correct
88 Correct 317 ms 37216 KB Output is correct
89 Correct 371 ms 39188 KB Output is correct
90 Correct 428 ms 39260 KB Output is correct
91 Correct 330 ms 38636 KB Output is correct
92 Correct 565 ms 39924 KB Output is correct
93 Correct 533 ms 39512 KB Output is correct
94 Correct 213 ms 37360 KB Output is correct
95 Correct 449 ms 39436 KB Output is correct
96 Correct 464 ms 39684 KB Output is correct
97 Correct 437 ms 39512 KB Output is correct
98 Correct 420 ms 39412 KB Output is correct
99 Correct 531 ms 39536 KB Output is correct
100 Correct 445 ms 39504 KB Output is correct