Submission #673466

# Submission time Handle Problem Language Result Execution time Memory
673466 2022-12-20T16:21:15 Z stevancv Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
821 ms 39276 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e6 + 2;
const int mod = 1e9 + 7;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin >> n >> q;
    int m = 1 << n;
    string s; cin >> s;
    vector<int> sum(m), asum(m);
    for (int i = 0; i < m; i++) sum[i] = asum[i] = s[i] - '0';
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            if ((1 << j) & i) sum[i] += sum[i ^ (1 << j)];
            else asum[i] += asum[i ^ (1 << j)];
        }
    }
    while (q--) {
        string t; cin >> t;
        reverse(t.begin(), t.end());
        int x = 0, y = 0, z = 0;
        for (int i = 0; i < n; i++) {
            if (t[i] == '1') x += 1 << i;
            if (t[i] == '0') y += 1 << i;
            if (t[i] == '?') z += 1 << i;
        }
        int xx = __builtin_popcount(x);
        if (xx <= 6) {
            int ans = 0;
            for (int smask = x; ; smask = (smask - 1) & x) {
                if ((xx - __builtin_popcount(smask)) & 1) ans -= sum[smask | z];
                else ans += sum[smask | z];
                if (smask == 0) break;
            }
            cout << ans << en;
            continue;
        }
        int yy = __builtin_popcount(y);
        if (yy <= 6) {
            int ans = 0;
            for (int smask = y; ; smask = (smask - 1) & y) {
                if ((yy - __builtin_popcount(smask)) & 1) ans -= asum[(smask ^ y) | x];
                else ans += asum[(smask ^ y) | x];
                if (smask == 0) break;
            }
            cout << ans << en;
            continue;
        }
        int ans = 0;
        for (int smask = z; ; smask = (smask - 1) & z) {
            ans += s[smask | x] - '0';
            if (smask == 0) break;
        }
        cout << ans << en;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 310 ms 14984 KB Output is correct
12 Correct 195 ms 14736 KB Output is correct
13 Correct 240 ms 13980 KB Output is correct
14 Correct 248 ms 14084 KB Output is correct
15 Correct 214 ms 15104 KB Output is correct
16 Correct 279 ms 14260 KB Output is correct
17 Correct 272 ms 14156 KB Output is correct
18 Correct 171 ms 16164 KB Output is correct
19 Correct 295 ms 13052 KB Output is correct
20 Correct 295 ms 14668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 310 ms 14984 KB Output is correct
12 Correct 195 ms 14736 KB Output is correct
13 Correct 240 ms 13980 KB Output is correct
14 Correct 248 ms 14084 KB Output is correct
15 Correct 214 ms 15104 KB Output is correct
16 Correct 279 ms 14260 KB Output is correct
17 Correct 272 ms 14156 KB Output is correct
18 Correct 171 ms 16164 KB Output is correct
19 Correct 295 ms 13052 KB Output is correct
20 Correct 295 ms 14668 KB Output is correct
21 Correct 419 ms 18000 KB Output is correct
22 Correct 220 ms 18268 KB Output is correct
23 Correct 296 ms 17228 KB Output is correct
24 Correct 303 ms 17100 KB Output is correct
25 Correct 255 ms 19020 KB Output is correct
26 Correct 345 ms 17580 KB Output is correct
27 Correct 315 ms 17536 KB Output is correct
28 Correct 180 ms 20032 KB Output is correct
29 Correct 447 ms 16156 KB Output is correct
30 Correct 293 ms 18248 KB Output is correct
31 Correct 296 ms 18000 KB Output is correct
32 Correct 297 ms 18008 KB Output is correct
33 Correct 265 ms 16940 KB Output is correct
34 Correct 329 ms 17236 KB Output is correct
35 Correct 324 ms 17564 KB Output is correct
36 Correct 174 ms 16076 KB Output is correct
37 Correct 212 ms 17996 KB Output is correct
38 Correct 357 ms 16072 KB Output is correct
39 Correct 441 ms 17244 KB Output is correct
40 Correct 340 ms 17080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 42 ms 11968 KB Output is correct
12 Correct 40 ms 11984 KB Output is correct
13 Correct 47 ms 11840 KB Output is correct
14 Correct 57 ms 11840 KB Output is correct
15 Correct 41 ms 11972 KB Output is correct
16 Correct 63 ms 11844 KB Output is correct
17 Correct 60 ms 11828 KB Output is correct
18 Correct 35 ms 12108 KB Output is correct
19 Correct 39 ms 11724 KB Output is correct
20 Correct 47 ms 11976 KB Output is correct
21 Correct 47 ms 11884 KB Output is correct
22 Correct 55 ms 12012 KB Output is correct
23 Correct 47 ms 11884 KB Output is correct
24 Correct 64 ms 11844 KB Output is correct
25 Correct 61 ms 11856 KB Output is correct
26 Correct 35 ms 11752 KB Output is correct
27 Correct 40 ms 11876 KB Output is correct
28 Correct 44 ms 11764 KB Output is correct
29 Correct 50 ms 11852 KB Output is correct
30 Correct 45 ms 11888 KB Output is correct
31 Correct 40 ms 11800 KB Output is correct
32 Correct 53 ms 11908 KB Output is correct
33 Correct 59 ms 11944 KB Output is correct
34 Correct 34 ms 11808 KB Output is correct
35 Correct 50 ms 11852 KB Output is correct
36 Correct 51 ms 11880 KB Output is correct
37 Correct 52 ms 11944 KB Output is correct
38 Correct 52 ms 11844 KB Output is correct
39 Correct 55 ms 11936 KB Output is correct
40 Correct 54 ms 11936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 310 ms 14984 KB Output is correct
12 Correct 195 ms 14736 KB Output is correct
13 Correct 240 ms 13980 KB Output is correct
14 Correct 248 ms 14084 KB Output is correct
15 Correct 214 ms 15104 KB Output is correct
16 Correct 279 ms 14260 KB Output is correct
17 Correct 272 ms 14156 KB Output is correct
18 Correct 171 ms 16164 KB Output is correct
19 Correct 295 ms 13052 KB Output is correct
20 Correct 295 ms 14668 KB Output is correct
21 Correct 419 ms 18000 KB Output is correct
22 Correct 220 ms 18268 KB Output is correct
23 Correct 296 ms 17228 KB Output is correct
24 Correct 303 ms 17100 KB Output is correct
25 Correct 255 ms 19020 KB Output is correct
26 Correct 345 ms 17580 KB Output is correct
27 Correct 315 ms 17536 KB Output is correct
28 Correct 180 ms 20032 KB Output is correct
29 Correct 447 ms 16156 KB Output is correct
30 Correct 293 ms 18248 KB Output is correct
31 Correct 296 ms 18000 KB Output is correct
32 Correct 297 ms 18008 KB Output is correct
33 Correct 265 ms 16940 KB Output is correct
34 Correct 329 ms 17236 KB Output is correct
35 Correct 324 ms 17564 KB Output is correct
36 Correct 174 ms 16076 KB Output is correct
37 Correct 212 ms 17996 KB Output is correct
38 Correct 357 ms 16072 KB Output is correct
39 Correct 441 ms 17244 KB Output is correct
40 Correct 340 ms 17080 KB Output is correct
41 Correct 42 ms 11968 KB Output is correct
42 Correct 40 ms 11984 KB Output is correct
43 Correct 47 ms 11840 KB Output is correct
44 Correct 57 ms 11840 KB Output is correct
45 Correct 41 ms 11972 KB Output is correct
46 Correct 63 ms 11844 KB Output is correct
47 Correct 60 ms 11828 KB Output is correct
48 Correct 35 ms 12108 KB Output is correct
49 Correct 39 ms 11724 KB Output is correct
50 Correct 47 ms 11976 KB Output is correct
51 Correct 47 ms 11884 KB Output is correct
52 Correct 55 ms 12012 KB Output is correct
53 Correct 47 ms 11884 KB Output is correct
54 Correct 64 ms 11844 KB Output is correct
55 Correct 61 ms 11856 KB Output is correct
56 Correct 35 ms 11752 KB Output is correct
57 Correct 40 ms 11876 KB Output is correct
58 Correct 44 ms 11764 KB Output is correct
59 Correct 50 ms 11852 KB Output is correct
60 Correct 45 ms 11888 KB Output is correct
61 Correct 40 ms 11800 KB Output is correct
62 Correct 53 ms 11908 KB Output is correct
63 Correct 59 ms 11944 KB Output is correct
64 Correct 34 ms 11808 KB Output is correct
65 Correct 50 ms 11852 KB Output is correct
66 Correct 51 ms 11880 KB Output is correct
67 Correct 52 ms 11944 KB Output is correct
68 Correct 52 ms 11844 KB Output is correct
69 Correct 55 ms 11936 KB Output is correct
70 Correct 54 ms 11936 KB Output is correct
71 Correct 333 ms 36204 KB Output is correct
72 Correct 331 ms 36424 KB Output is correct
73 Correct 453 ms 35012 KB Output is correct
74 Correct 594 ms 35268 KB Output is correct
75 Correct 368 ms 37228 KB Output is correct
76 Correct 821 ms 35684 KB Output is correct
77 Correct 710 ms 35416 KB Output is correct
78 Correct 242 ms 39276 KB Output is correct
79 Correct 305 ms 33168 KB Output is correct
80 Correct 381 ms 36520 KB Output is correct
81 Correct 468 ms 36204 KB Output is correct
82 Correct 639 ms 35168 KB Output is correct
83 Correct 415 ms 34512 KB Output is correct
84 Correct 738 ms 35180 KB Output is correct
85 Correct 732 ms 35508 KB Output is correct
86 Correct 237 ms 33260 KB Output is correct
87 Correct 342 ms 36268 KB Output is correct
88 Correct 379 ms 33180 KB Output is correct
89 Correct 493 ms 34892 KB Output is correct
90 Correct 432 ms 35180 KB Output is correct
91 Correct 372 ms 34336 KB Output is correct
92 Correct 648 ms 35584 KB Output is correct
93 Correct 744 ms 35472 KB Output is correct
94 Correct 229 ms 33288 KB Output is correct
95 Correct 677 ms 35328 KB Output is correct
96 Correct 610 ms 35372 KB Output is correct
97 Correct 609 ms 35292 KB Output is correct
98 Correct 605 ms 35388 KB Output is correct
99 Correct 594 ms 35292 KB Output is correct
100 Correct 607 ms 35320 KB Output is correct