답안 #704531

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
704531 2023-03-02T09:03:13 Z KenIsGenius Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1377 ms 39712 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ll long long
using namespace std;

#ifdef LOCAL
#define WOSAOJI freopen("in.txt", "r", stdin);
#else
#define WOSAOJI ios_base::sync_with_stdio(0), cin.tie(0);
#endif

#define chmax(a, b) (a) = (a) > (b) ? (a) : (b)
#define chmin(a, b) (a) = (a) < (b) ? (a) : (b)

// O(N*2^N + Q*N/3*2^(N/3) + QN)
// 4e8

int n, q;
string s;
vector<int> tmp;
int dp[1 << 20];
int dp0[1 << 20], dp1[1 << 20];

void solve1() {
    int x = 0;
    for (int i = 0; i < n; i++) {
        x *= 2; if (s[i] != '0') x++;
        if (s[i] == '1') tmp.pb(n - i - 1);
    }
    int ans = 0;
    for (int i = 0; i < 1 << tmp.size(); i++) {
        int now = x;
        for (int j = 0; j < tmp.size(); j++) {
            if (i >> j & 1) now -= 1 << tmp[j];
        }
        if (__builtin_parity(i)) ans -= dp1[now];
        else ans += dp1[now];
    }
    cout << ans << '\n';
}
void solve0() {
    int x = 0;
    for (int i = 0; i < n; i++) {
        x *= 2; if (s[i] == '1') x++;
        if (s[i] == '0') tmp.pb(n - i - 1);
    }
    int ans = 0;
    for (int i = 0; i < 1 << tmp.size(); i++) {
        int now = x;
        for (int j = 0; j < tmp.size(); j++) {
            if (i >> j & 1) now += 1 << tmp[j];
        }
        if (__builtin_parity(i)) ans -= dp0[now];
        else ans += dp0[now];
    }
    cout << ans << '\n';
}
void solveq() {
    int x = 0;
    for (int i = 0; i < n; i++) {
        x *= 2; if (s[i] == '1') x++;
        if (s[i] == '?') tmp.pb(n - i - 1);
    }
    int ans = 0;
    for (int i = 0; i < 1 << tmp.size(); i++) {
        int now = x;
        for (int j = 0; j < tmp.size(); j++) {
            if (i >> j & 1) now += 1 << tmp[j];
        }
        ans += dp[now];
    }
    cout << ans << '\n';
}

signed main() {
    WOSAOJI
    cin >> n >> q >> s;
    for (int i = 0; i < 1 << n; i++) {
        dp[i] = dp0[i] = dp1[i] = s[i] - '0';
    }
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < 1 << 20; i++) {
            if (i >> j & 1) {
                dp1[i] += dp1[i ^ (1 << j)];
            }
        }
        for (int i = (1 << 20) - 1; i >= 0; i--) {
            if (!(i >> j & 1)) {
                dp0[i] += dp0[i ^ (1 << j)];
            }
        }
    }
    while (q--) {
        tmp.clear();
        cin >> s;
        int zc = 0, oc = 0, qc = 0;
        for (char c : s) {
            if (c == '0') zc++;
            if (c == '1') oc++;
            if (c == '?') qc++;
        }
        if (oc <= zc and oc <= qc) {
            solve1();
        }
        else if (zc <= oc and zc <= qc) {
            solve0();
        }
        else {
            solveq();
        }
    }
}

Compilation message

snake_escaping.cpp: In function 'void solve1()':
snake_escaping.cpp:36:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |         for (int j = 0; j < tmp.size(); j++) {
      |                         ~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'void solve0()':
snake_escaping.cpp:53:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for (int j = 0; j < tmp.size(); j++) {
      |                         ~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'void solveq()':
snake_escaping.cpp:70:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         for (int j = 0; j < tmp.size(); j++) {
      |                         ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 8532 KB Output is correct
2 Correct 28 ms 8532 KB Output is correct
3 Correct 26 ms 8532 KB Output is correct
4 Correct 25 ms 8532 KB Output is correct
5 Correct 26 ms 8544 KB Output is correct
6 Correct 25 ms 8532 KB Output is correct
7 Correct 25 ms 8528 KB Output is correct
8 Correct 25 ms 8532 KB Output is correct
9 Correct 25 ms 8532 KB Output is correct
10 Correct 25 ms 8532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 8532 KB Output is correct
2 Correct 28 ms 8532 KB Output is correct
3 Correct 26 ms 8532 KB Output is correct
4 Correct 25 ms 8532 KB Output is correct
5 Correct 26 ms 8544 KB Output is correct
6 Correct 25 ms 8532 KB Output is correct
7 Correct 25 ms 8528 KB Output is correct
8 Correct 25 ms 8532 KB Output is correct
9 Correct 25 ms 8532 KB Output is correct
10 Correct 25 ms 8532 KB Output is correct
11 Correct 255 ms 23228 KB Output is correct
12 Correct 235 ms 22916 KB Output is correct
13 Correct 319 ms 22128 KB Output is correct
14 Correct 322 ms 22276 KB Output is correct
15 Correct 284 ms 23164 KB Output is correct
16 Correct 395 ms 22360 KB Output is correct
17 Correct 351 ms 22396 KB Output is correct
18 Correct 178 ms 24220 KB Output is correct
19 Correct 244 ms 21212 KB Output is correct
20 Correct 251 ms 22860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 8532 KB Output is correct
2 Correct 28 ms 8532 KB Output is correct
3 Correct 26 ms 8532 KB Output is correct
4 Correct 25 ms 8532 KB Output is correct
5 Correct 26 ms 8544 KB Output is correct
6 Correct 25 ms 8532 KB Output is correct
7 Correct 25 ms 8528 KB Output is correct
8 Correct 25 ms 8532 KB Output is correct
9 Correct 25 ms 8532 KB Output is correct
10 Correct 25 ms 8532 KB Output is correct
11 Correct 255 ms 23228 KB Output is correct
12 Correct 235 ms 22916 KB Output is correct
13 Correct 319 ms 22128 KB Output is correct
14 Correct 322 ms 22276 KB Output is correct
15 Correct 284 ms 23164 KB Output is correct
16 Correct 395 ms 22360 KB Output is correct
17 Correct 351 ms 22396 KB Output is correct
18 Correct 178 ms 24220 KB Output is correct
19 Correct 244 ms 21212 KB Output is correct
20 Correct 251 ms 22860 KB Output is correct
21 Correct 273 ms 26288 KB Output is correct
22 Correct 329 ms 26420 KB Output is correct
23 Correct 393 ms 25404 KB Output is correct
24 Correct 428 ms 25220 KB Output is correct
25 Correct 325 ms 27252 KB Output is correct
26 Correct 436 ms 25756 KB Output is correct
27 Correct 417 ms 25720 KB Output is correct
28 Correct 211 ms 28264 KB Output is correct
29 Correct 284 ms 24224 KB Output is correct
30 Correct 313 ms 26388 KB Output is correct
31 Correct 412 ms 26264 KB Output is correct
32 Correct 429 ms 26320 KB Output is correct
33 Correct 402 ms 25056 KB Output is correct
34 Correct 450 ms 25232 KB Output is correct
35 Correct 440 ms 25716 KB Output is correct
36 Correct 221 ms 24224 KB Output is correct
37 Correct 283 ms 26300 KB Output is correct
38 Correct 330 ms 24232 KB Output is correct
39 Correct 422 ms 25448 KB Output is correct
40 Correct 463 ms 25200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 8532 KB Output is correct
2 Correct 28 ms 8532 KB Output is correct
3 Correct 26 ms 8532 KB Output is correct
4 Correct 25 ms 8532 KB Output is correct
5 Correct 26 ms 8544 KB Output is correct
6 Correct 25 ms 8532 KB Output is correct
7 Correct 25 ms 8528 KB Output is correct
8 Correct 25 ms 8532 KB Output is correct
9 Correct 25 ms 8532 KB Output is correct
10 Correct 25 ms 8532 KB Output is correct
11 Correct 81 ms 16064 KB Output is correct
12 Correct 86 ms 16076 KB Output is correct
13 Correct 92 ms 15980 KB Output is correct
14 Correct 102 ms 15952 KB Output is correct
15 Correct 71 ms 16008 KB Output is correct
16 Correct 110 ms 16064 KB Output is correct
17 Correct 96 ms 15936 KB Output is correct
18 Correct 59 ms 16192 KB Output is correct
19 Correct 66 ms 15900 KB Output is correct
20 Correct 88 ms 16068 KB Output is correct
21 Correct 80 ms 16024 KB Output is correct
22 Correct 106 ms 15944 KB Output is correct
23 Correct 77 ms 15980 KB Output is correct
24 Correct 122 ms 15960 KB Output is correct
25 Correct 103 ms 15972 KB Output is correct
26 Correct 57 ms 15852 KB Output is correct
27 Correct 65 ms 16064 KB Output is correct
28 Correct 66 ms 15848 KB Output is correct
29 Correct 111 ms 15932 KB Output is correct
30 Correct 114 ms 15964 KB Output is correct
31 Correct 77 ms 15936 KB Output is correct
32 Correct 118 ms 15980 KB Output is correct
33 Correct 112 ms 15948 KB Output is correct
34 Correct 61 ms 15864 KB Output is correct
35 Correct 90 ms 16012 KB Output is correct
36 Correct 87 ms 15960 KB Output is correct
37 Correct 85 ms 15936 KB Output is correct
38 Correct 89 ms 15944 KB Output is correct
39 Correct 90 ms 16020 KB Output is correct
40 Correct 90 ms 15964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 8532 KB Output is correct
2 Correct 28 ms 8532 KB Output is correct
3 Correct 26 ms 8532 KB Output is correct
4 Correct 25 ms 8532 KB Output is correct
5 Correct 26 ms 8544 KB Output is correct
6 Correct 25 ms 8532 KB Output is correct
7 Correct 25 ms 8528 KB Output is correct
8 Correct 25 ms 8532 KB Output is correct
9 Correct 25 ms 8532 KB Output is correct
10 Correct 25 ms 8532 KB Output is correct
11 Correct 255 ms 23228 KB Output is correct
12 Correct 235 ms 22916 KB Output is correct
13 Correct 319 ms 22128 KB Output is correct
14 Correct 322 ms 22276 KB Output is correct
15 Correct 284 ms 23164 KB Output is correct
16 Correct 395 ms 22360 KB Output is correct
17 Correct 351 ms 22396 KB Output is correct
18 Correct 178 ms 24220 KB Output is correct
19 Correct 244 ms 21212 KB Output is correct
20 Correct 251 ms 22860 KB Output is correct
21 Correct 273 ms 26288 KB Output is correct
22 Correct 329 ms 26420 KB Output is correct
23 Correct 393 ms 25404 KB Output is correct
24 Correct 428 ms 25220 KB Output is correct
25 Correct 325 ms 27252 KB Output is correct
26 Correct 436 ms 25756 KB Output is correct
27 Correct 417 ms 25720 KB Output is correct
28 Correct 211 ms 28264 KB Output is correct
29 Correct 284 ms 24224 KB Output is correct
30 Correct 313 ms 26388 KB Output is correct
31 Correct 412 ms 26264 KB Output is correct
32 Correct 429 ms 26320 KB Output is correct
33 Correct 402 ms 25056 KB Output is correct
34 Correct 450 ms 25232 KB Output is correct
35 Correct 440 ms 25716 KB Output is correct
36 Correct 221 ms 24224 KB Output is correct
37 Correct 283 ms 26300 KB Output is correct
38 Correct 330 ms 24232 KB Output is correct
39 Correct 422 ms 25448 KB Output is correct
40 Correct 463 ms 25200 KB Output is correct
41 Correct 81 ms 16064 KB Output is correct
42 Correct 86 ms 16076 KB Output is correct
43 Correct 92 ms 15980 KB Output is correct
44 Correct 102 ms 15952 KB Output is correct
45 Correct 71 ms 16008 KB Output is correct
46 Correct 110 ms 16064 KB Output is correct
47 Correct 96 ms 15936 KB Output is correct
48 Correct 59 ms 16192 KB Output is correct
49 Correct 66 ms 15900 KB Output is correct
50 Correct 88 ms 16068 KB Output is correct
51 Correct 80 ms 16024 KB Output is correct
52 Correct 106 ms 15944 KB Output is correct
53 Correct 77 ms 15980 KB Output is correct
54 Correct 122 ms 15960 KB Output is correct
55 Correct 103 ms 15972 KB Output is correct
56 Correct 57 ms 15852 KB Output is correct
57 Correct 65 ms 16064 KB Output is correct
58 Correct 66 ms 15848 KB Output is correct
59 Correct 111 ms 15932 KB Output is correct
60 Correct 114 ms 15964 KB Output is correct
61 Correct 77 ms 15936 KB Output is correct
62 Correct 118 ms 15980 KB Output is correct
63 Correct 112 ms 15948 KB Output is correct
64 Correct 61 ms 15864 KB Output is correct
65 Correct 90 ms 16012 KB Output is correct
66 Correct 87 ms 15960 KB Output is correct
67 Correct 85 ms 15936 KB Output is correct
68 Correct 89 ms 15944 KB Output is correct
69 Correct 90 ms 16020 KB Output is correct
70 Correct 90 ms 15964 KB Output is correct
71 Correct 466 ms 36128 KB Output is correct
72 Correct 553 ms 36380 KB Output is correct
73 Correct 764 ms 35280 KB Output is correct
74 Correct 1177 ms 35648 KB Output is correct
75 Correct 507 ms 37644 KB Output is correct
76 Correct 1320 ms 36020 KB Output is correct
77 Correct 1078 ms 35880 KB Output is correct
78 Correct 257 ms 39712 KB Output is correct
79 Correct 413 ms 33660 KB Output is correct
80 Correct 436 ms 36852 KB Output is correct
81 Correct 680 ms 36812 KB Output is correct
82 Correct 1216 ms 35624 KB Output is correct
83 Correct 493 ms 34868 KB Output is correct
84 Correct 1377 ms 35776 KB Output is correct
85 Correct 1044 ms 35856 KB Output is correct
86 Correct 256 ms 33888 KB Output is correct
87 Correct 516 ms 37012 KB Output is correct
88 Correct 480 ms 34312 KB Output is correct
89 Correct 835 ms 36220 KB Output is correct
90 Correct 1254 ms 36696 KB Output is correct
91 Correct 683 ms 36096 KB Output is correct
92 Correct 1191 ms 37316 KB Output is correct
93 Correct 1040 ms 37128 KB Output is correct
94 Correct 241 ms 34908 KB Output is correct
95 Correct 865 ms 37196 KB Output is correct
96 Correct 860 ms 37136 KB Output is correct
97 Correct 875 ms 37084 KB Output is correct
98 Correct 852 ms 37088 KB Output is correct
99 Correct 889 ms 37176 KB Output is correct
100 Correct 887 ms 37140 KB Output is correct