Submission #694048

# Submission time Handle Problem Language Result Execution time Memory
694048 2023-02-03T16:03:02 Z Shin Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1136 ms 39104 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair

using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
    if (a > b) return a = b, true;
    return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
    if (a < b) return a = b, true;
    return false;
}

int lower[1 << 20];
int upper[1 << 20];
char a[1 << 20];

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, q; cin >> n >> q;
  for (int i = 0; i < 1 << n; i ++) {
    cin >> a[i];
  }
  for (int i = 0; i < 1 << n; i ++) {
    lower[i] = upper[i] = a[i] - '0';
  }
  for (int i = 0; i < n; i ++) {
    for (int mask = 0; mask < 1 << n; mask ++) {
      if (mask >> i & 1) {
        lower[mask] += lower[mask ^ (1 << i)];
      }
    }
  }
  for (int i = 0; i < n; i ++) {
    for (int mask = (1 << n) - 1; ~mask; mask --) {
      if (~mask >> i & 1) {
        upper[mask] += upper[mask ^ (1 << i)];
      }
    }
  }
  while (q --) {
    string s; cin >> s;
    reverse(s.begin(), s.end());
    int mask_a = 0, mask_b = 0, mask_c = 0;
    for (int i = 0; i < n; i ++) {
      if (s[i] == '0') {
        mask_a |= 1 << i;
      } 
      if (s[i] == '1') {
        mask_b |= 1 << i;
      }
      if (s[i] == '?') {
        mask_c |= 1 << i;
      }
    }
    int res = 0;
    if (__builtin_popcount(mask_c) <= 6) {
      for (int mask = mask_c; ~mask; mask = (mask - 1) & mask_c) {
        res += a[mask_b | mask] - '0';
        if (mask == 0) {
          break;
        }
      }
    } else
    if (__builtin_popcount(mask_a) <= 6) {
      for (int mask = mask_a; ~mask; mask = (mask - 1) & mask_a) {
        int cnt = __builtin_popcount(mask);
        res += ((cnt & 1) ? -1 : 1) * upper[mask | mask_b];
        if (mask == 0) {
          break;
        }
      }
    } else {
      for (int mask = mask_b; ~mask; mask = (mask - 1) & mask_b) {
        int cnt = __builtin_popcount(mask);
        res += ((cnt & 1) == (__builtin_popcount(mask_b) & 1) ? 1 : -1) * lower[mask | mask_c];
        if (mask == 0) {
          break;
        }
      }
    }
    cout << res << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 224 ms 15064 KB Output is correct
12 Correct 248 ms 14844 KB Output is correct
13 Correct 223 ms 13920 KB Output is correct
14 Correct 237 ms 14040 KB Output is correct
15 Correct 293 ms 15100 KB Output is correct
16 Correct 236 ms 14276 KB Output is correct
17 Correct 244 ms 14276 KB Output is correct
18 Correct 174 ms 16068 KB Output is correct
19 Correct 191 ms 13168 KB Output is correct
20 Correct 239 ms 14712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 224 ms 15064 KB Output is correct
12 Correct 248 ms 14844 KB Output is correct
13 Correct 223 ms 13920 KB Output is correct
14 Correct 237 ms 14040 KB Output is correct
15 Correct 293 ms 15100 KB Output is correct
16 Correct 236 ms 14276 KB Output is correct
17 Correct 244 ms 14276 KB Output is correct
18 Correct 174 ms 16068 KB Output is correct
19 Correct 191 ms 13168 KB Output is correct
20 Correct 239 ms 14712 KB Output is correct
21 Correct 244 ms 18024 KB Output is correct
22 Correct 327 ms 18192 KB Output is correct
23 Correct 265 ms 17188 KB Output is correct
24 Correct 273 ms 17188 KB Output is correct
25 Correct 250 ms 19096 KB Output is correct
26 Correct 284 ms 17572 KB Output is correct
27 Correct 283 ms 17656 KB Output is correct
28 Correct 183 ms 20172 KB Output is correct
29 Correct 230 ms 16160 KB Output is correct
30 Correct 263 ms 18256 KB Output is correct
31 Correct 322 ms 18068 KB Output is correct
32 Correct 284 ms 18152 KB Output is correct
33 Correct 230 ms 16996 KB Output is correct
34 Correct 265 ms 17052 KB Output is correct
35 Correct 292 ms 17488 KB Output is correct
36 Correct 193 ms 16100 KB Output is correct
37 Correct 312 ms 18132 KB Output is correct
38 Correct 227 ms 16020 KB Output is correct
39 Correct 269 ms 17268 KB Output is correct
40 Correct 253 ms 17104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 68 ms 11800 KB Output is correct
12 Correct 73 ms 11808 KB Output is correct
13 Correct 71 ms 11700 KB Output is correct
14 Correct 106 ms 11696 KB Output is correct
15 Correct 69 ms 11836 KB Output is correct
16 Correct 89 ms 11796 KB Output is correct
17 Correct 83 ms 11708 KB Output is correct
18 Correct 60 ms 11956 KB Output is correct
19 Correct 66 ms 11596 KB Output is correct
20 Correct 69 ms 11852 KB Output is correct
21 Correct 88 ms 11824 KB Output is correct
22 Correct 82 ms 11696 KB Output is correct
23 Correct 68 ms 11696 KB Output is correct
24 Correct 71 ms 11796 KB Output is correct
25 Correct 88 ms 11700 KB Output is correct
26 Correct 62 ms 11684 KB Output is correct
27 Correct 68 ms 11832 KB Output is correct
28 Correct 65 ms 11696 KB Output is correct
29 Correct 69 ms 11772 KB Output is correct
30 Correct 78 ms 11700 KB Output is correct
31 Correct 71 ms 11704 KB Output is correct
32 Correct 90 ms 11812 KB Output is correct
33 Correct 87 ms 11868 KB Output is correct
34 Correct 66 ms 11696 KB Output is correct
35 Correct 75 ms 11724 KB Output is correct
36 Correct 78 ms 11808 KB Output is correct
37 Correct 86 ms 11700 KB Output is correct
38 Correct 86 ms 11800 KB Output is correct
39 Correct 94 ms 11744 KB Output is correct
40 Correct 84 ms 11812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 224 ms 15064 KB Output is correct
12 Correct 248 ms 14844 KB Output is correct
13 Correct 223 ms 13920 KB Output is correct
14 Correct 237 ms 14040 KB Output is correct
15 Correct 293 ms 15100 KB Output is correct
16 Correct 236 ms 14276 KB Output is correct
17 Correct 244 ms 14276 KB Output is correct
18 Correct 174 ms 16068 KB Output is correct
19 Correct 191 ms 13168 KB Output is correct
20 Correct 239 ms 14712 KB Output is correct
21 Correct 244 ms 18024 KB Output is correct
22 Correct 327 ms 18192 KB Output is correct
23 Correct 265 ms 17188 KB Output is correct
24 Correct 273 ms 17188 KB Output is correct
25 Correct 250 ms 19096 KB Output is correct
26 Correct 284 ms 17572 KB Output is correct
27 Correct 283 ms 17656 KB Output is correct
28 Correct 183 ms 20172 KB Output is correct
29 Correct 230 ms 16160 KB Output is correct
30 Correct 263 ms 18256 KB Output is correct
31 Correct 322 ms 18068 KB Output is correct
32 Correct 284 ms 18152 KB Output is correct
33 Correct 230 ms 16996 KB Output is correct
34 Correct 265 ms 17052 KB Output is correct
35 Correct 292 ms 17488 KB Output is correct
36 Correct 193 ms 16100 KB Output is correct
37 Correct 312 ms 18132 KB Output is correct
38 Correct 227 ms 16020 KB Output is correct
39 Correct 269 ms 17268 KB Output is correct
40 Correct 253 ms 17104 KB Output is correct
41 Correct 68 ms 11800 KB Output is correct
42 Correct 73 ms 11808 KB Output is correct
43 Correct 71 ms 11700 KB Output is correct
44 Correct 106 ms 11696 KB Output is correct
45 Correct 69 ms 11836 KB Output is correct
46 Correct 89 ms 11796 KB Output is correct
47 Correct 83 ms 11708 KB Output is correct
48 Correct 60 ms 11956 KB Output is correct
49 Correct 66 ms 11596 KB Output is correct
50 Correct 69 ms 11852 KB Output is correct
51 Correct 88 ms 11824 KB Output is correct
52 Correct 82 ms 11696 KB Output is correct
53 Correct 68 ms 11696 KB Output is correct
54 Correct 71 ms 11796 KB Output is correct
55 Correct 88 ms 11700 KB Output is correct
56 Correct 62 ms 11684 KB Output is correct
57 Correct 68 ms 11832 KB Output is correct
58 Correct 65 ms 11696 KB Output is correct
59 Correct 69 ms 11772 KB Output is correct
60 Correct 78 ms 11700 KB Output is correct
61 Correct 71 ms 11704 KB Output is correct
62 Correct 90 ms 11812 KB Output is correct
63 Correct 87 ms 11868 KB Output is correct
64 Correct 66 ms 11696 KB Output is correct
65 Correct 75 ms 11724 KB Output is correct
66 Correct 78 ms 11808 KB Output is correct
67 Correct 86 ms 11700 KB Output is correct
68 Correct 86 ms 11800 KB Output is correct
69 Correct 94 ms 11744 KB Output is correct
70 Correct 84 ms 11812 KB Output is correct
71 Correct 444 ms 36036 KB Output is correct
72 Correct 406 ms 36252 KB Output is correct
73 Correct 458 ms 34836 KB Output is correct
74 Correct 697 ms 35164 KB Output is correct
75 Correct 472 ms 37120 KB Output is correct
76 Correct 799 ms 35424 KB Output is correct
77 Correct 758 ms 35364 KB Output is correct
78 Correct 309 ms 39104 KB Output is correct
79 Correct 358 ms 33088 KB Output is correct
80 Correct 474 ms 36160 KB Output is correct
81 Correct 670 ms 36124 KB Output is correct
82 Correct 1136 ms 35108 KB Output is correct
83 Correct 420 ms 34232 KB Output is correct
84 Correct 520 ms 35092 KB Output is correct
85 Correct 742 ms 35284 KB Output is correct
86 Correct 304 ms 33104 KB Output is correct
87 Correct 458 ms 36112 KB Output is correct
88 Correct 422 ms 33092 KB Output is correct
89 Correct 451 ms 34804 KB Output is correct
90 Correct 572 ms 35156 KB Output is correct
91 Correct 461 ms 34272 KB Output is correct
92 Correct 796 ms 35428 KB Output is correct
93 Correct 680 ms 35524 KB Output is correct
94 Correct 252 ms 33052 KB Output is correct
95 Correct 652 ms 35288 KB Output is correct
96 Correct 612 ms 35128 KB Output is correct
97 Correct 618 ms 35148 KB Output is correct
98 Correct 613 ms 35136 KB Output is correct
99 Correct 625 ms 35088 KB Output is correct
100 Correct 598 ms 35128 KB Output is correct