Submission #649571

# Submission time Handle Problem Language Result Execution time Memory
649571 2022-10-10T16:20:03 Z Alex_tz307 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
788 ms 39408 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int l, q;
  string s;
  cin >> l >> q >> s;
  int n = (1 << l);
  vector<int> sub(n), super(n);
  for (int mask = 0; mask < n; ++mask) {
    sub[mask] = (s[mask] - '0');
    super[mask] = (s[mask] - '0');
  }
  for (int bit = 0; bit < l; ++bit) {
    for (int mask = 0; mask < n; ++mask) {
      if (mask & (1 << bit)) {
        sub[mask] += sub[mask ^ (1 << bit)];
      } else {
        super[mask] += super[mask | (1 << bit)];
      }
    }
  }
  for (int qq = 0; qq < q; ++qq) {
    string t;
    cin >> t;
    int mask0 = 0, mask1 = 0, maskq = 0;
    for (int i = 0; i < l; ++i) {
      if (t[i] == '0') {
        mask0 |= (1 << (l - i - 1));
      } else if (t[i] == '1') {
        mask1 |= (1 << (l - i - 1));
      } else {
        maskq |= (1 << (l - i - 1));
      }
    }
    int sum = 0, mask;
    if (__builtin_popcount(maskq) <= l / 3) {
      mask = maskq;
      while (mask) {
        sum += (s[mask1 | mask] - '0');
        mask = (mask - 1) & maskq;
      }
      sum += (s[mask1] - '0');
    } else if (__builtin_popcount(mask0) <= l / 3) {
      mask = mask0;
      while (mask) {
        if (__builtin_popcount(mask) % 2 == 0) {
          sum += super[mask | mask1];
        } else {
          sum -= super[mask | mask1];
        }
        mask = (mask - 1) & mask0;
      }
      sum += super[mask1];
    } else {
      int N = __builtin_popcount(mask1);
      mask = mask1;
      while (mask) {
        if ((N - __builtin_popcount(mask)) % 2 == 0) {
          sum += sub[mask | maskq];
        } else {
          sum -= sub[mask | maskq];
        }
        mask = (mask - 1) & mask1;
      }
      if (N % 2 == 0) {
        sum += sub[maskq];
      } else {
        sum -= sub[maskq];
      }
    }
    cout << sum << '\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 332 KB Output is correct
9 Correct 1 ms 340 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 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 205 ms 15032 KB Output is correct
12 Correct 232 ms 14724 KB Output is correct
13 Correct 236 ms 13996 KB Output is correct
14 Correct 236 ms 14024 KB Output is correct
15 Correct 227 ms 15056 KB Output is correct
16 Correct 246 ms 14152 KB Output is correct
17 Correct 261 ms 14188 KB Output is correct
18 Correct 180 ms 16128 KB Output is correct
19 Correct 211 ms 13004 KB Output is correct
20 Correct 219 ms 14812 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 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 205 ms 15032 KB Output is correct
12 Correct 232 ms 14724 KB Output is correct
13 Correct 236 ms 13996 KB Output is correct
14 Correct 236 ms 14024 KB Output is correct
15 Correct 227 ms 15056 KB Output is correct
16 Correct 246 ms 14152 KB Output is correct
17 Correct 261 ms 14188 KB Output is correct
18 Correct 180 ms 16128 KB Output is correct
19 Correct 211 ms 13004 KB Output is correct
20 Correct 219 ms 14812 KB Output is correct
21 Correct 236 ms 17996 KB Output is correct
22 Correct 246 ms 18348 KB Output is correct
23 Correct 282 ms 17256 KB Output is correct
24 Correct 270 ms 17016 KB Output is correct
25 Correct 253 ms 19148 KB Output is correct
26 Correct 300 ms 17612 KB Output is correct
27 Correct 282 ms 17476 KB Output is correct
28 Correct 190 ms 20036 KB Output is correct
29 Correct 226 ms 16168 KB Output is correct
30 Correct 245 ms 18256 KB Output is correct
31 Correct 285 ms 18212 KB Output is correct
32 Correct 314 ms 18204 KB Output is correct
33 Correct 238 ms 16940 KB Output is correct
34 Correct 281 ms 17100 KB Output is correct
35 Correct 285 ms 17608 KB Output is correct
36 Correct 187 ms 16024 KB Output is correct
37 Correct 228 ms 18036 KB Output is correct
38 Correct 236 ms 15984 KB Output is correct
39 Correct 275 ms 17204 KB Output is correct
40 Correct 266 ms 17100 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 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 52 ms 12064 KB Output is correct
12 Correct 52 ms 11984 KB Output is correct
13 Correct 48 ms 11812 KB Output is correct
14 Correct 66 ms 11840 KB Output is correct
15 Correct 47 ms 11988 KB Output is correct
16 Correct 60 ms 11860 KB Output is correct
17 Correct 62 ms 12000 KB Output is correct
18 Correct 41 ms 12072 KB Output is correct
19 Correct 45 ms 11760 KB Output is correct
20 Correct 52 ms 11916 KB Output is correct
21 Correct 50 ms 11984 KB Output is correct
22 Correct 84 ms 11840 KB Output is correct
23 Correct 44 ms 11792 KB Output is correct
24 Correct 54 ms 11856 KB Output is correct
25 Correct 61 ms 11940 KB Output is correct
26 Correct 41 ms 11728 KB Output is correct
27 Correct 45 ms 11932 KB Output is correct
28 Correct 45 ms 11736 KB Output is correct
29 Correct 45 ms 11836 KB Output is correct
30 Correct 50 ms 11860 KB Output is correct
31 Correct 45 ms 11880 KB Output is correct
32 Correct 73 ms 11932 KB Output is correct
33 Correct 59 ms 11892 KB Output is correct
34 Correct 41 ms 11744 KB Output is correct
35 Correct 59 ms 11844 KB Output is correct
36 Correct 57 ms 11904 KB Output is correct
37 Correct 56 ms 11860 KB Output is correct
38 Correct 56 ms 11844 KB Output is correct
39 Correct 52 ms 11840 KB Output is correct
40 Correct 53 ms 11864 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 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 205 ms 15032 KB Output is correct
12 Correct 232 ms 14724 KB Output is correct
13 Correct 236 ms 13996 KB Output is correct
14 Correct 236 ms 14024 KB Output is correct
15 Correct 227 ms 15056 KB Output is correct
16 Correct 246 ms 14152 KB Output is correct
17 Correct 261 ms 14188 KB Output is correct
18 Correct 180 ms 16128 KB Output is correct
19 Correct 211 ms 13004 KB Output is correct
20 Correct 219 ms 14812 KB Output is correct
21 Correct 236 ms 17996 KB Output is correct
22 Correct 246 ms 18348 KB Output is correct
23 Correct 282 ms 17256 KB Output is correct
24 Correct 270 ms 17016 KB Output is correct
25 Correct 253 ms 19148 KB Output is correct
26 Correct 300 ms 17612 KB Output is correct
27 Correct 282 ms 17476 KB Output is correct
28 Correct 190 ms 20036 KB Output is correct
29 Correct 226 ms 16168 KB Output is correct
30 Correct 245 ms 18256 KB Output is correct
31 Correct 285 ms 18212 KB Output is correct
32 Correct 314 ms 18204 KB Output is correct
33 Correct 238 ms 16940 KB Output is correct
34 Correct 281 ms 17100 KB Output is correct
35 Correct 285 ms 17608 KB Output is correct
36 Correct 187 ms 16024 KB Output is correct
37 Correct 228 ms 18036 KB Output is correct
38 Correct 236 ms 15984 KB Output is correct
39 Correct 275 ms 17204 KB Output is correct
40 Correct 266 ms 17100 KB Output is correct
41 Correct 52 ms 12064 KB Output is correct
42 Correct 52 ms 11984 KB Output is correct
43 Correct 48 ms 11812 KB Output is correct
44 Correct 66 ms 11840 KB Output is correct
45 Correct 47 ms 11988 KB Output is correct
46 Correct 60 ms 11860 KB Output is correct
47 Correct 62 ms 12000 KB Output is correct
48 Correct 41 ms 12072 KB Output is correct
49 Correct 45 ms 11760 KB Output is correct
50 Correct 52 ms 11916 KB Output is correct
51 Correct 50 ms 11984 KB Output is correct
52 Correct 84 ms 11840 KB Output is correct
53 Correct 44 ms 11792 KB Output is correct
54 Correct 54 ms 11856 KB Output is correct
55 Correct 61 ms 11940 KB Output is correct
56 Correct 41 ms 11728 KB Output is correct
57 Correct 45 ms 11932 KB Output is correct
58 Correct 45 ms 11736 KB Output is correct
59 Correct 45 ms 11836 KB Output is correct
60 Correct 50 ms 11860 KB Output is correct
61 Correct 45 ms 11880 KB Output is correct
62 Correct 73 ms 11932 KB Output is correct
63 Correct 59 ms 11892 KB Output is correct
64 Correct 41 ms 11744 KB Output is correct
65 Correct 59 ms 11844 KB Output is correct
66 Correct 57 ms 11904 KB Output is correct
67 Correct 56 ms 11860 KB Output is correct
68 Correct 56 ms 11844 KB Output is correct
69 Correct 52 ms 11840 KB Output is correct
70 Correct 53 ms 11864 KB Output is correct
71 Correct 382 ms 36216 KB Output is correct
72 Correct 420 ms 36308 KB Output is correct
73 Correct 444 ms 35024 KB Output is correct
74 Correct 766 ms 35220 KB Output is correct
75 Correct 411 ms 37268 KB Output is correct
76 Correct 635 ms 35552 KB Output is correct
77 Correct 647 ms 35436 KB Output is correct
78 Correct 265 ms 39408 KB Output is correct
79 Correct 329 ms 33176 KB Output is correct
80 Correct 417 ms 36356 KB Output is correct
81 Correct 503 ms 36292 KB Output is correct
82 Correct 655 ms 35228 KB Output is correct
83 Correct 363 ms 34340 KB Output is correct
84 Correct 471 ms 35176 KB Output is correct
85 Correct 633 ms 35652 KB Output is correct
86 Correct 254 ms 33252 KB Output is correct
87 Correct 362 ms 36208 KB Output is correct
88 Correct 354 ms 33300 KB Output is correct
89 Correct 409 ms 34924 KB Output is correct
90 Correct 473 ms 35304 KB Output is correct
91 Correct 382 ms 34408 KB Output is correct
92 Correct 788 ms 35572 KB Output is correct
93 Correct 683 ms 35456 KB Output is correct
94 Correct 264 ms 33256 KB Output is correct
95 Correct 577 ms 35412 KB Output is correct
96 Correct 675 ms 35368 KB Output is correct
97 Correct 577 ms 35360 KB Output is correct
98 Correct 560 ms 35332 KB Output is correct
99 Correct 564 ms 35292 KB Output is correct
100 Correct 566 ms 35468 KB Output is correct