답안 #714118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
714118 2023-03-24T01:27:37 Z becaido Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1594 ms 29808 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int SIZE = 20;
const int MAX = 1 << SIZE;

int n, m, q;
int a[MAX];
string s;
int dp0[MAX], dp1[MAX];

void solve() {
    cin >> n >> q;
    cin >> s;
    m = 1 << n;
    FOR (i, 0, m - 1) {
        a[i] = s[i] - '0';
        dp0[(m - 1) ^ i] = a[i];
        dp1[i] = a[i];
    }
    FOR (i, 0, n - 1) FOR (mask, 0, m - 1) if (mask >> i & 1) {
        dp0[mask] += dp0[mask ^ (1 << i)];
        dp1[mask] += dp1[mask ^ (1 << i)];
    }
    while (q--) {
        cin >> s;
        reverse(s.begin(), s.end());
        int cnt[3] = {};
        for (char c : s) (c == '0' ? cnt[0] : c == '1' ? cnt[1] : cnt[2])++;
        int p;
        FOR (i, 0, 2) if (cnt[i] == *min_element(cnt, cnt + 3)) {
            p = i;
            break;
        }
        vector<int> v;
        FOR (i, 0, n - 1) if ((s[i] == '0' && p == 0) || (s[i] == '1' && p == 1) || (s[i] == '?' && p == 2)) v.pb(i);
        int ans = 0;
        if (p == 2) {
            auto dfs = [&](auto dfs, int i, int mask)->void {
                if (i == v.size()) {
                    ans += a[mask];
                    return;
                }
                dfs(dfs, i + 1, mask);
                dfs(dfs, i + 1, mask | (1 << v[i]));
            };
            int mask = 0;
            FOR (i, 0, n - 1) if (s[i] == '1') mask |= 1 << i;
            dfs(dfs, 0, mask);
        } else if (p == 0) {
            auto dfs = [&](auto dfs, int i, int mask, int cnt)->void {
                if (i == v.size()) {
                    ans += (cnt & 1 ? -dp0[mask] : dp0[mask]);
                    return;
                }
                dfs(dfs, i + 1, mask | (1 << v[i]), cnt - 1);
                dfs(dfs, i + 1, mask, cnt);
            };
            int mask = 0;
            FOR (i, 0, n - 1) if (s[i] == '?') mask |= 1 << i;
            dfs(dfs, 0, mask, cnt[0]);
        } else {
            auto dfs = [&](auto dfs, int i, int mask, int cnt)->void {
                if (i == v.size()) {
                    ans += (cnt & 1 ? -dp1[mask] : dp1[mask]);
                    return;
                }
                dfs(dfs, i + 1, mask | (1 << v[i]), cnt - 1);
                dfs(dfs, i + 1, mask, cnt);
            };
            int mask = 0;
            FOR (i, 0, n - 1) if (s[i] == '?') mask |= 1 << i;
            dfs(dfs, 0, mask, cnt[1]);
        }
        cout << ans << '\n';
    }
}

int main() {
    Waimai;
    solve();
}

Compilation message

snake_escaping.cpp: In instantiation of 'solve()::<lambda(auto:23, int, int)> [with auto:23 = solve()::<lambda(auto:23, int, int)>]':
snake_escaping.cpp:67:29:   required from here
snake_escaping.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |                 if (i == v.size()) {
      |                     ~~^~~~~~~~~~~
snake_escaping.cpp: In instantiation of 'solve()::<lambda(auto:24, int, int, int)> [with auto:24 = solve()::<lambda(auto:24, int, int, int)>]':
snake_escaping.cpp:79:37:   required from here
snake_escaping.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |                 if (i == v.size()) {
      |                     ~~^~~~~~~~~~~
snake_escaping.cpp: In instantiation of 'solve()::<lambda(auto:25, int, int, int)> [with auto:25 = solve()::<lambda(auto:25, int, int, int)>]':
snake_escaping.cpp:91:37:   required from here
snake_escaping.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |                 if (i == v.size()) {
      |                     ~~^~~~~~~~~~~
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:68:16: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
   68 |         } else if (p == 0) {
      |                ^~
# 결과 실행 시간 메모리 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 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 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 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 263 ms 4240 KB Output is correct
12 Correct 293 ms 3984 KB Output is correct
13 Correct 420 ms 3216 KB Output is correct
14 Correct 438 ms 3440 KB Output is correct
15 Correct 354 ms 4244 KB Output is correct
16 Correct 458 ms 3500 KB Output is correct
17 Correct 445 ms 3420 KB Output is correct
18 Correct 227 ms 5328 KB Output is correct
19 Correct 247 ms 2268 KB Output is correct
20 Correct 297 ms 3956 KB Output is correct
# 결과 실행 시간 메모리 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 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 263 ms 4240 KB Output is correct
12 Correct 293 ms 3984 KB Output is correct
13 Correct 420 ms 3216 KB Output is correct
14 Correct 438 ms 3440 KB Output is correct
15 Correct 354 ms 4244 KB Output is correct
16 Correct 458 ms 3500 KB Output is correct
17 Correct 445 ms 3420 KB Output is correct
18 Correct 227 ms 5328 KB Output is correct
19 Correct 247 ms 2268 KB Output is correct
20 Correct 297 ms 3956 KB Output is correct
21 Correct 336 ms 4472 KB Output is correct
22 Correct 385 ms 4588 KB Output is correct
23 Correct 560 ms 3552 KB Output is correct
24 Correct 634 ms 3460 KB Output is correct
25 Correct 432 ms 5440 KB Output is correct
26 Correct 618 ms 3876 KB Output is correct
27 Correct 596 ms 3896 KB Output is correct
28 Correct 208 ms 6504 KB Output is correct
29 Correct 331 ms 2384 KB Output is correct
30 Correct 393 ms 4740 KB Output is correct
31 Correct 525 ms 4464 KB Output is correct
32 Correct 568 ms 4676 KB Output is correct
33 Correct 448 ms 3332 KB Output is correct
34 Correct 579 ms 3356 KB Output is correct
35 Correct 608 ms 3824 KB Output is correct
36 Correct 238 ms 2428 KB Output is correct
37 Correct 319 ms 4432 KB Output is correct
38 Correct 380 ms 2356 KB Output is correct
39 Correct 522 ms 3716 KB Output is correct
40 Correct 580 ms 3396 KB Output is correct
# 결과 실행 시간 메모리 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 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 50 ms 13952 KB Output is correct
12 Correct 48 ms 13920 KB Output is correct
13 Correct 73 ms 13912 KB Output is correct
14 Correct 92 ms 13912 KB Output is correct
15 Correct 54 ms 14060 KB Output is correct
16 Correct 84 ms 14044 KB Output is correct
17 Correct 77 ms 13944 KB Output is correct
18 Correct 38 ms 14060 KB Output is correct
19 Correct 63 ms 13788 KB Output is correct
20 Correct 55 ms 13924 KB Output is correct
21 Correct 77 ms 14012 KB Output is correct
22 Correct 74 ms 13964 KB Output is correct
23 Correct 50 ms 13852 KB Output is correct
24 Correct 103 ms 13900 KB Output is correct
25 Correct 82 ms 13876 KB Output is correct
26 Correct 50 ms 13804 KB Output is correct
27 Correct 49 ms 13912 KB Output is correct
28 Correct 51 ms 13800 KB Output is correct
29 Correct 61 ms 13948 KB Output is correct
30 Correct 77 ms 13884 KB Output is correct
31 Correct 54 ms 13888 KB Output is correct
32 Correct 84 ms 13924 KB Output is correct
33 Correct 88 ms 13924 KB Output is correct
34 Correct 33 ms 13800 KB Output is correct
35 Correct 70 ms 13912 KB Output is correct
36 Correct 65 ms 13956 KB Output is correct
37 Correct 76 ms 13920 KB Output is correct
38 Correct 87 ms 13904 KB Output is correct
39 Correct 66 ms 13904 KB Output is correct
40 Correct 67 ms 13936 KB Output is correct
# 결과 실행 시간 메모리 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 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 263 ms 4240 KB Output is correct
12 Correct 293 ms 3984 KB Output is correct
13 Correct 420 ms 3216 KB Output is correct
14 Correct 438 ms 3440 KB Output is correct
15 Correct 354 ms 4244 KB Output is correct
16 Correct 458 ms 3500 KB Output is correct
17 Correct 445 ms 3420 KB Output is correct
18 Correct 227 ms 5328 KB Output is correct
19 Correct 247 ms 2268 KB Output is correct
20 Correct 297 ms 3956 KB Output is correct
21 Correct 336 ms 4472 KB Output is correct
22 Correct 385 ms 4588 KB Output is correct
23 Correct 560 ms 3552 KB Output is correct
24 Correct 634 ms 3460 KB Output is correct
25 Correct 432 ms 5440 KB Output is correct
26 Correct 618 ms 3876 KB Output is correct
27 Correct 596 ms 3896 KB Output is correct
28 Correct 208 ms 6504 KB Output is correct
29 Correct 331 ms 2384 KB Output is correct
30 Correct 393 ms 4740 KB Output is correct
31 Correct 525 ms 4464 KB Output is correct
32 Correct 568 ms 4676 KB Output is correct
33 Correct 448 ms 3332 KB Output is correct
34 Correct 579 ms 3356 KB Output is correct
35 Correct 608 ms 3824 KB Output is correct
36 Correct 238 ms 2428 KB Output is correct
37 Correct 319 ms 4432 KB Output is correct
38 Correct 380 ms 2356 KB Output is correct
39 Correct 522 ms 3716 KB Output is correct
40 Correct 580 ms 3396 KB Output is correct
41 Correct 50 ms 13952 KB Output is correct
42 Correct 48 ms 13920 KB Output is correct
43 Correct 73 ms 13912 KB Output is correct
44 Correct 92 ms 13912 KB Output is correct
45 Correct 54 ms 14060 KB Output is correct
46 Correct 84 ms 14044 KB Output is correct
47 Correct 77 ms 13944 KB Output is correct
48 Correct 38 ms 14060 KB Output is correct
49 Correct 63 ms 13788 KB Output is correct
50 Correct 55 ms 13924 KB Output is correct
51 Correct 77 ms 14012 KB Output is correct
52 Correct 74 ms 13964 KB Output is correct
53 Correct 50 ms 13852 KB Output is correct
54 Correct 103 ms 13900 KB Output is correct
55 Correct 82 ms 13876 KB Output is correct
56 Correct 50 ms 13804 KB Output is correct
57 Correct 49 ms 13912 KB Output is correct
58 Correct 51 ms 13800 KB Output is correct
59 Correct 61 ms 13948 KB Output is correct
60 Correct 77 ms 13884 KB Output is correct
61 Correct 54 ms 13888 KB Output is correct
62 Correct 84 ms 13924 KB Output is correct
63 Correct 88 ms 13924 KB Output is correct
64 Correct 33 ms 13800 KB Output is correct
65 Correct 70 ms 13912 KB Output is correct
66 Correct 65 ms 13956 KB Output is correct
67 Correct 76 ms 13920 KB Output is correct
68 Correct 87 ms 13904 KB Output is correct
69 Correct 66 ms 13904 KB Output is correct
70 Correct 67 ms 13936 KB Output is correct
71 Correct 503 ms 18784 KB Output is correct
72 Correct 525 ms 18852 KB Output is correct
73 Correct 923 ms 17400 KB Output is correct
74 Correct 1138 ms 19724 KB Output is correct
75 Correct 673 ms 27700 KB Output is correct
76 Correct 1143 ms 25632 KB Output is correct
77 Correct 1183 ms 25728 KB Output is correct
78 Correct 291 ms 29808 KB Output is correct
79 Correct 517 ms 23732 KB Output is correct
80 Correct 528 ms 26656 KB Output is correct
81 Correct 853 ms 26284 KB Output is correct
82 Correct 1261 ms 25536 KB Output is correct
83 Correct 646 ms 25092 KB Output is correct
84 Correct 1594 ms 25464 KB Output is correct
85 Correct 1264 ms 25760 KB Output is correct
86 Correct 311 ms 23772 KB Output is correct
87 Correct 530 ms 26572 KB Output is correct
88 Correct 563 ms 23908 KB Output is correct
89 Correct 947 ms 25140 KB Output is correct
90 Correct 1106 ms 25776 KB Output is correct
91 Correct 608 ms 24504 KB Output is correct
92 Correct 1305 ms 25744 KB Output is correct
93 Correct 1173 ms 25860 KB Output is correct
94 Correct 252 ms 23652 KB Output is correct
95 Correct 999 ms 26020 KB Output is correct
96 Correct 973 ms 25920 KB Output is correct
97 Correct 932 ms 26016 KB Output is correct
98 Correct 943 ms 25732 KB Output is correct
99 Correct 999 ms 26064 KB Output is correct
100 Correct 934 ms 26040 KB Output is correct