답안 #894754

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894754 2023-12-28T22:04:53 Z coloboxx Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
578 ms 39412 KB
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,sse2,sse3,ssse3,sse4,lzcnt,popcnt")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define ull unsigned ll
#define point pair<int, int>
#define X first
#define Y second
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define show(x) cerr << (#x) << " = " << x << '\n'
#define print(l, r) cerr << "[" << (#l) << ", " << (#r) << "] = ", _print(l, r), cerr << '\n'
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0)
template<class iter>
void _print(iter _first, iter _last) {
    while (_first != _last) {
        std::cerr << *(_first) << ' ';
        ++_first;
    }
}

const int N = 1 << 20;
const int S = 2e7 + 64;
const int INF = 1e9 + 64;
const ll MAX = 2e18 + 64;
const int LOG = 22;
const int MOD = 998244353;
const int P = 79;
const int B = 256;

using namespace std;
using namespace __gnu_pbds;

int dp[N][2];

int pw(int x) {
    return ((x & 1) ? -1 : 1);
}

int32_t main() {
    fast_io;

    int L, n, q;
    cin >> L >> q, n = 1 << L;

    string s;
    cin >> s;

    for (int i = 0; i < n; ++i)
        dp[i][0] = dp[i][1] = s[i] - '0';

    for (int i = 0; i < L; ++i)
        for (int mask = 0; mask < n; ++mask)
            if (mask >> i & 1)
                dp[mask][0] += dp[mask ^ (1 << i)][0];
            else
                dp[mask][1] += dp[mask ^ (1 << i)][1];

    while (q--) {
        string t;
        cin >> t;
        reverse(all(t));

        int base = 0, add = 0;
        int res = 0;

        if (count(all(t), '?') <= 6) {
            for (int i = 0; i < L; ++i)
                if (t[i] == '1') 
                    base |= 1 << i;
                else if (t[i] == '?')
                    add |= 1 << i;

            for (int j = add; ; j = (j - 1) & add) {
                res += s[base | j] - '0';
                if (j == 0)
                    break;
            }
        }
        else if (count(all(t), '0') <= 6) {
            for (int i = 0; i < L; ++i)
                if (t[i] == '1')
                    base |= 1 << i;
                else if (t[i] == '0')
                    add |= 1 << i;

            int cnt = __builtin_popcount(base);

            for (int j = add; ; j = (j - 1) & add) {
                int cur = base | j;
                res += dp[cur][1] * pw(__builtin_popcount(cur) - cnt);
                if (j == 0)
                    break;
            }
        }
        else {
            for (int i = 0; i < L; ++i) {
                if (t[i] != '0')
                    base |= 1 << i;
                if (t[i] == '1')
                    add |= 1 << i;
            }

            int cnt = __builtin_popcount(base);
            
            for (int j = add; ; j = (j - 1) & add) {
                int cur = base & ~j & (n - 1);
                res += dp[cur][0] * pw(cnt - __builtin_popcount(cur));
                if (j == 0)
                    break;
            }
        }

        cout << res << '\n';
    }
}
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 472 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 472 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 191 ms 14960 KB Output is correct
12 Correct 185 ms 14832 KB Output is correct
13 Correct 162 ms 13908 KB Output is correct
14 Correct 161 ms 14156 KB Output is correct
15 Correct 225 ms 15444 KB Output is correct
16 Correct 178 ms 14164 KB Output is correct
17 Correct 190 ms 14236 KB Output is correct
18 Correct 165 ms 16208 KB Output is correct
19 Correct 167 ms 13344 KB Output is correct
20 Correct 214 ms 14788 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 472 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 191 ms 14960 KB Output is correct
12 Correct 185 ms 14832 KB Output is correct
13 Correct 162 ms 13908 KB Output is correct
14 Correct 161 ms 14156 KB Output is correct
15 Correct 225 ms 15444 KB Output is correct
16 Correct 178 ms 14164 KB Output is correct
17 Correct 190 ms 14236 KB Output is correct
18 Correct 165 ms 16208 KB Output is correct
19 Correct 167 ms 13344 KB Output is correct
20 Correct 214 ms 14788 KB Output is correct
21 Correct 220 ms 18008 KB Output is correct
22 Correct 257 ms 18344 KB Output is correct
23 Correct 208 ms 17312 KB Output is correct
24 Correct 186 ms 16980 KB Output is correct
25 Correct 232 ms 19036 KB Output is correct
26 Correct 229 ms 17492 KB Output is correct
27 Correct 233 ms 17492 KB Output is correct
28 Correct 186 ms 20060 KB Output is correct
29 Correct 212 ms 16452 KB Output is correct
30 Correct 239 ms 18128 KB Output is correct
31 Correct 254 ms 18064 KB Output is correct
32 Correct 207 ms 18160 KB Output is correct
33 Correct 207 ms 16976 KB Output is correct
34 Correct 200 ms 17232 KB Output is correct
35 Correct 252 ms 17876 KB Output is correct
36 Correct 147 ms 16212 KB Output is correct
37 Correct 214 ms 18032 KB Output is correct
38 Correct 202 ms 16208 KB Output is correct
39 Correct 242 ms 17152 KB Output is correct
40 Correct 195 ms 16980 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 472 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 34 ms 12028 KB Output is correct
12 Correct 38 ms 11972 KB Output is correct
13 Correct 35 ms 12020 KB Output is correct
14 Correct 48 ms 12024 KB Output is correct
15 Correct 38 ms 12024 KB Output is correct
16 Correct 52 ms 12020 KB Output is correct
17 Correct 37 ms 11860 KB Output is correct
18 Correct 30 ms 12284 KB Output is correct
19 Correct 38 ms 12016 KB Output is correct
20 Correct 34 ms 12084 KB Output is correct
21 Correct 39 ms 12080 KB Output is correct
22 Correct 49 ms 11852 KB Output is correct
23 Correct 37 ms 12024 KB Output is correct
24 Correct 37 ms 12024 KB Output is correct
25 Correct 42 ms 11836 KB Output is correct
26 Correct 28 ms 11932 KB Output is correct
27 Correct 45 ms 12112 KB Output is correct
28 Correct 32 ms 11776 KB Output is correct
29 Correct 34 ms 12024 KB Output is correct
30 Correct 36 ms 12020 KB Output is correct
31 Correct 41 ms 11856 KB Output is correct
32 Correct 39 ms 12016 KB Output is correct
33 Correct 39 ms 11852 KB Output is correct
34 Correct 29 ms 11764 KB Output is correct
35 Correct 39 ms 12004 KB Output is correct
36 Correct 45 ms 11864 KB Output is correct
37 Correct 42 ms 11840 KB Output is correct
38 Correct 39 ms 12004 KB Output is correct
39 Correct 42 ms 12020 KB Output is correct
40 Correct 36 ms 12024 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 472 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 191 ms 14960 KB Output is correct
12 Correct 185 ms 14832 KB Output is correct
13 Correct 162 ms 13908 KB Output is correct
14 Correct 161 ms 14156 KB Output is correct
15 Correct 225 ms 15444 KB Output is correct
16 Correct 178 ms 14164 KB Output is correct
17 Correct 190 ms 14236 KB Output is correct
18 Correct 165 ms 16208 KB Output is correct
19 Correct 167 ms 13344 KB Output is correct
20 Correct 214 ms 14788 KB Output is correct
21 Correct 220 ms 18008 KB Output is correct
22 Correct 257 ms 18344 KB Output is correct
23 Correct 208 ms 17312 KB Output is correct
24 Correct 186 ms 16980 KB Output is correct
25 Correct 232 ms 19036 KB Output is correct
26 Correct 229 ms 17492 KB Output is correct
27 Correct 233 ms 17492 KB Output is correct
28 Correct 186 ms 20060 KB Output is correct
29 Correct 212 ms 16452 KB Output is correct
30 Correct 239 ms 18128 KB Output is correct
31 Correct 254 ms 18064 KB Output is correct
32 Correct 207 ms 18160 KB Output is correct
33 Correct 207 ms 16976 KB Output is correct
34 Correct 200 ms 17232 KB Output is correct
35 Correct 252 ms 17876 KB Output is correct
36 Correct 147 ms 16212 KB Output is correct
37 Correct 214 ms 18032 KB Output is correct
38 Correct 202 ms 16208 KB Output is correct
39 Correct 242 ms 17152 KB Output is correct
40 Correct 195 ms 16980 KB Output is correct
41 Correct 34 ms 12028 KB Output is correct
42 Correct 38 ms 11972 KB Output is correct
43 Correct 35 ms 12020 KB Output is correct
44 Correct 48 ms 12024 KB Output is correct
45 Correct 38 ms 12024 KB Output is correct
46 Correct 52 ms 12020 KB Output is correct
47 Correct 37 ms 11860 KB Output is correct
48 Correct 30 ms 12284 KB Output is correct
49 Correct 38 ms 12016 KB Output is correct
50 Correct 34 ms 12084 KB Output is correct
51 Correct 39 ms 12080 KB Output is correct
52 Correct 49 ms 11852 KB Output is correct
53 Correct 37 ms 12024 KB Output is correct
54 Correct 37 ms 12024 KB Output is correct
55 Correct 42 ms 11836 KB Output is correct
56 Correct 28 ms 11932 KB Output is correct
57 Correct 45 ms 12112 KB Output is correct
58 Correct 32 ms 11776 KB Output is correct
59 Correct 34 ms 12024 KB Output is correct
60 Correct 36 ms 12020 KB Output is correct
61 Correct 41 ms 11856 KB Output is correct
62 Correct 39 ms 12016 KB Output is correct
63 Correct 39 ms 11852 KB Output is correct
64 Correct 29 ms 11764 KB Output is correct
65 Correct 39 ms 12004 KB Output is correct
66 Correct 45 ms 11864 KB Output is correct
67 Correct 42 ms 11840 KB Output is correct
68 Correct 39 ms 12004 KB Output is correct
69 Correct 42 ms 12020 KB Output is correct
70 Correct 36 ms 12024 KB Output is correct
71 Correct 346 ms 36392 KB Output is correct
72 Correct 332 ms 36496 KB Output is correct
73 Correct 316 ms 35064 KB Output is correct
74 Correct 566 ms 35232 KB Output is correct
75 Correct 349 ms 37188 KB Output is correct
76 Correct 531 ms 35568 KB Output is correct
77 Correct 394 ms 35472 KB Output is correct
78 Correct 251 ms 39412 KB Output is correct
79 Correct 288 ms 33424 KB Output is correct
80 Correct 398 ms 36412 KB Output is correct
81 Correct 455 ms 36592 KB Output is correct
82 Correct 578 ms 35168 KB Output is correct
83 Correct 296 ms 34344 KB Output is correct
84 Correct 355 ms 35388 KB Output is correct
85 Correct 393 ms 35568 KB Output is correct
86 Correct 206 ms 33344 KB Output is correct
87 Correct 306 ms 36388 KB Output is correct
88 Correct 285 ms 33268 KB Output is correct
89 Correct 335 ms 35064 KB Output is correct
90 Correct 359 ms 35224 KB Output is correct
91 Correct 265 ms 34516 KB Output is correct
92 Correct 456 ms 35852 KB Output is correct
93 Correct 427 ms 35404 KB Output is correct
94 Correct 251 ms 33004 KB Output is correct
95 Correct 397 ms 35396 KB Output is correct
96 Correct 429 ms 35268 KB Output is correct
97 Correct 477 ms 35320 KB Output is correct
98 Correct 471 ms 35304 KB Output is correct
99 Correct 407 ms 35464 KB Output is correct
100 Correct 381 ms 35512 KB Output is correct