답안 #941296

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
941296 2024-03-08T13:14:22 Z Amirreza_Fakhri Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
528 ms 38908 KB
// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")

const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 20;

int l, q, dp1[1<<maxn], dp2[1<<maxn];
string s, t;

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> l >> q >> s;
    for (int i = 0; i < (1<<l); i++) dp1[i] = dp2[(1<<l)-i-1] = s[i]-'0';
    for (int i = 0; i < l; i++) {
        for (int j = 0; j < (1<<l); j++) {
            if (j&(1<<i)) dp1[j] += dp1[j^(1<<i)], dp2[j] += dp2[j^(1<<i)];
        }
    }
    while (q--) {
        int cnt0 = 0, cnt1 = 0, cnt2 = 0, m1 = 0, m2 = 0, ans = 0;
        cin >> t;
        reverse(all(t));
        for (int i = 0; i < l; i++) {
            if (t[i] == '0') cnt0++;
            else if (t[i] == '1') cnt1++;
            else cnt2++;
        }
        // cout << cnt0 << ' ' << cnt1 << ' ' << cnt2 << '\n';
        if (cnt0 <= min(cnt1, cnt2)) {
            // cout << "HI1\n";
            for (int i = 0; i < l; i++) {
                if (t[i] == '0') m1 |= (1<<i);
                else if (t[i] == '?') m2 |= (1<<i);
            }
            for (int sm = m1; sm; sm = (sm-1)&m1) {
                if (__builtin_parity(m1^sm)) ans -= dp2[sm^m2];
                else ans += dp2[sm^m2];
            }
            if (__builtin_parity(m1)) ans -= dp2[m2];
            else ans += dp2[m2];
            cout << ans << '\n';
        }
        else if (cnt1 <= min(cnt0, cnt2)) {
            // cout << "HI2\n";
            for (int i = 0; i < l; i++) {
                if (t[i] == '1') m1 |= (1<<i);
                else if (t[i] == '?') m2 |= (1<<i);
            }
            for (int sm = m1; sm; sm = (sm-1)&m1) {
                if (__builtin_parity(m1^sm)) ans -= dp1[sm^m2];
                else ans += dp1[sm^m2];
            }
            if (__builtin_parity(m1)) ans -= dp1[m2];
            else ans += dp1[m2];
            cout << ans << '\n';
        }
        else {
            // cout << "HI3\n";
            for (int i = 0; i < l; i++) {
                if (t[i] == '?') m1 |= (1<<i);
                else if (t[i] == '1') m2 |= (1<<i);
            }
            for (int sm = m1; sm; sm = (sm-1)&m1) ans += s[sm^m2]-'0';
            ans += s[m2]-'0';
            cout << ans << '\n';
        }
    }
    return 0;
}

























# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2444 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2444 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 177 ms 17264 KB Output is correct
12 Correct 177 ms 17168 KB Output is correct
13 Correct 208 ms 16160 KB Output is correct
14 Correct 216 ms 16292 KB Output is correct
15 Correct 190 ms 17184 KB Output is correct
16 Correct 234 ms 16392 KB Output is correct
17 Correct 225 ms 16208 KB Output is correct
18 Correct 145 ms 18580 KB Output is correct
19 Correct 173 ms 15184 KB Output is correct
20 Correct 183 ms 16916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2444 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 177 ms 17264 KB Output is correct
12 Correct 177 ms 17168 KB Output is correct
13 Correct 208 ms 16160 KB Output is correct
14 Correct 216 ms 16292 KB Output is correct
15 Correct 190 ms 17184 KB Output is correct
16 Correct 234 ms 16392 KB Output is correct
17 Correct 225 ms 16208 KB Output is correct
18 Correct 145 ms 18580 KB Output is correct
19 Correct 173 ms 15184 KB Output is correct
20 Correct 183 ms 16916 KB Output is correct
21 Correct 198 ms 20008 KB Output is correct
22 Correct 202 ms 20276 KB Output is correct
23 Correct 263 ms 19280 KB Output is correct
24 Correct 279 ms 19024 KB Output is correct
25 Correct 232 ms 21076 KB Output is correct
26 Correct 280 ms 19540 KB Output is correct
27 Correct 274 ms 19512 KB Output is correct
28 Correct 163 ms 22312 KB Output is correct
29 Correct 196 ms 18176 KB Output is correct
30 Correct 222 ms 20172 KB Output is correct
31 Correct 250 ms 20156 KB Output is correct
32 Correct 287 ms 20044 KB Output is correct
33 Correct 227 ms 19024 KB Output is correct
34 Correct 279 ms 19024 KB Output is correct
35 Correct 280 ms 19532 KB Output is correct
36 Correct 185 ms 17988 KB Output is correct
37 Correct 188 ms 20036 KB Output is correct
38 Correct 212 ms 18020 KB Output is correct
39 Correct 248 ms 19284 KB Output is correct
40 Correct 275 ms 19024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2444 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 40 ms 12020 KB Output is correct
12 Correct 35 ms 12024 KB Output is correct
13 Correct 40 ms 12272 KB Output is correct
14 Correct 46 ms 12024 KB Output is correct
15 Correct 36 ms 12016 KB Output is correct
16 Correct 44 ms 12016 KB Output is correct
17 Correct 41 ms 12020 KB Output is correct
18 Correct 33 ms 12092 KB Output is correct
19 Correct 45 ms 11864 KB Output is correct
20 Correct 34 ms 12020 KB Output is correct
21 Correct 41 ms 12016 KB Output is correct
22 Correct 47 ms 12016 KB Output is correct
23 Correct 36 ms 11776 KB Output is correct
24 Correct 45 ms 12044 KB Output is correct
25 Correct 45 ms 11956 KB Output is correct
26 Correct 30 ms 11956 KB Output is correct
27 Correct 35 ms 12024 KB Output is correct
28 Correct 35 ms 11760 KB Output is correct
29 Correct 39 ms 12016 KB Output is correct
30 Correct 43 ms 12020 KB Output is correct
31 Correct 42 ms 11760 KB Output is correct
32 Correct 45 ms 12012 KB Output is correct
33 Correct 48 ms 11852 KB Output is correct
34 Correct 30 ms 11764 KB Output is correct
35 Correct 51 ms 12020 KB Output is correct
36 Correct 40 ms 12012 KB Output is correct
37 Correct 40 ms 12016 KB Output is correct
38 Correct 42 ms 12024 KB Output is correct
39 Correct 46 ms 11852 KB Output is correct
40 Correct 39 ms 11824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2444 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 177 ms 17264 KB Output is correct
12 Correct 177 ms 17168 KB Output is correct
13 Correct 208 ms 16160 KB Output is correct
14 Correct 216 ms 16292 KB Output is correct
15 Correct 190 ms 17184 KB Output is correct
16 Correct 234 ms 16392 KB Output is correct
17 Correct 225 ms 16208 KB Output is correct
18 Correct 145 ms 18580 KB Output is correct
19 Correct 173 ms 15184 KB Output is correct
20 Correct 183 ms 16916 KB Output is correct
21 Correct 198 ms 20008 KB Output is correct
22 Correct 202 ms 20276 KB Output is correct
23 Correct 263 ms 19280 KB Output is correct
24 Correct 279 ms 19024 KB Output is correct
25 Correct 232 ms 21076 KB Output is correct
26 Correct 280 ms 19540 KB Output is correct
27 Correct 274 ms 19512 KB Output is correct
28 Correct 163 ms 22312 KB Output is correct
29 Correct 196 ms 18176 KB Output is correct
30 Correct 222 ms 20172 KB Output is correct
31 Correct 250 ms 20156 KB Output is correct
32 Correct 287 ms 20044 KB Output is correct
33 Correct 227 ms 19024 KB Output is correct
34 Correct 279 ms 19024 KB Output is correct
35 Correct 280 ms 19532 KB Output is correct
36 Correct 185 ms 17988 KB Output is correct
37 Correct 188 ms 20036 KB Output is correct
38 Correct 212 ms 18020 KB Output is correct
39 Correct 248 ms 19284 KB Output is correct
40 Correct 275 ms 19024 KB Output is correct
41 Correct 40 ms 12020 KB Output is correct
42 Correct 35 ms 12024 KB Output is correct
43 Correct 40 ms 12272 KB Output is correct
44 Correct 46 ms 12024 KB Output is correct
45 Correct 36 ms 12016 KB Output is correct
46 Correct 44 ms 12016 KB Output is correct
47 Correct 41 ms 12020 KB Output is correct
48 Correct 33 ms 12092 KB Output is correct
49 Correct 45 ms 11864 KB Output is correct
50 Correct 34 ms 12020 KB Output is correct
51 Correct 41 ms 12016 KB Output is correct
52 Correct 47 ms 12016 KB Output is correct
53 Correct 36 ms 11776 KB Output is correct
54 Correct 45 ms 12044 KB Output is correct
55 Correct 45 ms 11956 KB Output is correct
56 Correct 30 ms 11956 KB Output is correct
57 Correct 35 ms 12024 KB Output is correct
58 Correct 35 ms 11760 KB Output is correct
59 Correct 39 ms 12016 KB Output is correct
60 Correct 43 ms 12020 KB Output is correct
61 Correct 42 ms 11760 KB Output is correct
62 Correct 45 ms 12012 KB Output is correct
63 Correct 48 ms 11852 KB Output is correct
64 Correct 30 ms 11764 KB Output is correct
65 Correct 51 ms 12020 KB Output is correct
66 Correct 40 ms 12012 KB Output is correct
67 Correct 40 ms 12016 KB Output is correct
68 Correct 42 ms 12024 KB Output is correct
69 Correct 46 ms 11852 KB Output is correct
70 Correct 39 ms 11824 KB Output is correct
71 Correct 290 ms 36196 KB Output is correct
72 Correct 300 ms 36596 KB Output is correct
73 Correct 395 ms 34804 KB Output is correct
74 Correct 505 ms 35292 KB Output is correct
75 Correct 322 ms 37368 KB Output is correct
76 Correct 528 ms 35584 KB Output is correct
77 Correct 451 ms 35324 KB Output is correct
78 Correct 208 ms 38908 KB Output is correct
79 Correct 285 ms 33596 KB Output is correct
80 Correct 291 ms 36336 KB Output is correct
81 Correct 388 ms 36284 KB Output is correct
82 Correct 522 ms 35316 KB Output is correct
83 Correct 301 ms 34288 KB Output is correct
84 Correct 477 ms 35280 KB Output is correct
85 Correct 446 ms 35448 KB Output is correct
86 Correct 203 ms 33188 KB Output is correct
87 Correct 288 ms 36512 KB Output is correct
88 Correct 285 ms 33096 KB Output is correct
89 Correct 378 ms 34808 KB Output is correct
90 Correct 442 ms 35232 KB Output is correct
91 Correct 315 ms 34348 KB Output is correct
92 Correct 527 ms 35700 KB Output is correct
93 Correct 430 ms 35328 KB Output is correct
94 Correct 187 ms 33324 KB Output is correct
95 Correct 423 ms 35316 KB Output is correct
96 Correct 399 ms 35316 KB Output is correct
97 Correct 387 ms 35616 KB Output is correct
98 Correct 383 ms 35316 KB Output is correct
99 Correct 388 ms 35268 KB Output is correct
100 Correct 390 ms 35796 KB Output is correct