답안 #1043045

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1043045 2024-08-03T19:18:27 Z ooscode Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1521 ms 43344 KB
#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define F first
#define S second
#define pb push_back
#define all(x) x.begin() , x.end()
#define debug while(1) cout << "Test\n"

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

const int N = (1ll << 20) + 10;
const int INF = 1e18 + 10;
const int mod = 1e9;

int n , q;
int sub[N];
int sup[N];
int a[N];

int main() {
    int n , q;
    cin >> n >> q;
    int l = n; n = (1ll << n);

    string s;
    cin >> s;
    for(int i = 0 ; i < n ; i++) {
        a[i] = s[i] - '0';
        sub[i] = a[i];
        sup[i] = a[i];
    }

    for(int i = 0 ; i < l ; i++) {
        for(int msk = n-1 ; ~msk ; msk--) if((1ll << i) & msk) sub[msk] += sub[msk ^ (1ll << i)];
        for(int msk = 0 ; msk < n ; msk++) if(!((1ll << i) & msk)) sup[msk] += sup[msk ^ (1ll << i)];
    }

    while(q--) {
        string t;
        cin >> t;
        
        int mask[3] = {0};
        int cnt[3] = {0};

        for(int i = 0 ; i < l ; i++) {
            int ty;
            if(t[t.size()-i-1] == '0') ty = 0;
            else if(t[t.size()-i-1] == '1') ty = 1;
            else ty = 2;

            mask[ty] |= (1ll << i);
            cnt[ty]++;
        }

        if(cnt[2] <= 6) {
            int ans = 0;
            for(int msk = mask[2] ; ; msk = (msk - 1) & mask[2]) {
                // cout << msk << " qu\n";
                ans += a[msk | mask[1]];
                if(msk == 0) break;
            }
            cout << ans << "\n";
            continue;
        }

        if(cnt[1] <= 6) {
            int ans = 0;
            for(int msk = mask[1] ; ; msk = (msk - 1) & mask[1]) {
                if(__builtin_popcount(msk) & 1) ans -= sub[(mask[1] ^ msk) | mask[2]];
                else ans += sub[(mask[1] ^ msk) | mask[2]];
                if(msk == 0) break;
            }
            cout << ans << "\n";
            continue;
        }

        int ans = 0;
        for(int msk = mask[0] ; ; msk = (msk - 1) & mask[0]) {
            if(__builtin_popcount(msk) & 1) ans -= sup[msk | mask[1]];
            else ans += sup[msk | mask[1]];
            if(msk == 0) break;
        }
        cout << ans << "\n";
    }
}

Compilation message

snake_escaping.cpp:16:22: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   16 | const int INF = 1e18 + 10;
      |                 ~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2396 KB Output is correct
2 Correct 1 ms 452 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2396 KB Output is correct
2 Correct 1 ms 452 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1154 ms 15196 KB Output is correct
12 Correct 1164 ms 14888 KB Output is correct
13 Correct 1120 ms 13916 KB Output is correct
14 Correct 1122 ms 14124 KB Output is correct
15 Correct 1157 ms 17232 KB Output is correct
16 Correct 1129 ms 16296 KB Output is correct
17 Correct 1159 ms 14352 KB Output is correct
18 Correct 1139 ms 18204 KB Output is correct
19 Correct 1108 ms 13104 KB Output is correct
20 Correct 1128 ms 14752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2396 KB Output is correct
2 Correct 1 ms 452 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1154 ms 15196 KB Output is correct
12 Correct 1164 ms 14888 KB Output is correct
13 Correct 1120 ms 13916 KB Output is correct
14 Correct 1122 ms 14124 KB Output is correct
15 Correct 1157 ms 17232 KB Output is correct
16 Correct 1129 ms 16296 KB Output is correct
17 Correct 1159 ms 14352 KB Output is correct
18 Correct 1139 ms 18204 KB Output is correct
19 Correct 1108 ms 13104 KB Output is correct
20 Correct 1128 ms 14752 KB Output is correct
21 Correct 1204 ms 18256 KB Output is correct
22 Correct 1263 ms 20404 KB Output is correct
23 Correct 1224 ms 17488 KB Output is correct
24 Correct 1166 ms 17236 KB Output is correct
25 Correct 1209 ms 21176 KB Output is correct
26 Correct 1184 ms 17668 KB Output is correct
27 Correct 1209 ms 19684 KB Output is correct
28 Correct 1155 ms 20248 KB Output is correct
29 Correct 1176 ms 16208 KB Output is correct
30 Correct 1202 ms 20304 KB Output is correct
31 Correct 1199 ms 20044 KB Output is correct
32 Correct 1226 ms 18256 KB Output is correct
33 Correct 1167 ms 16936 KB Output is correct
34 Correct 1164 ms 17232 KB Output is correct
35 Correct 1222 ms 17476 KB Output is correct
36 Correct 1141 ms 16076 KB Output is correct
37 Correct 1263 ms 20152 KB Output is correct
38 Correct 1203 ms 16068 KB Output is correct
39 Correct 1183 ms 19244 KB Output is correct
40 Correct 1159 ms 17232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2396 KB Output is correct
2 Correct 1 ms 452 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 97 ms 16112 KB Output is correct
12 Correct 96 ms 16072 KB Output is correct
13 Correct 98 ms 15936 KB Output is correct
14 Correct 108 ms 15980 KB Output is correct
15 Correct 98 ms 16196 KB Output is correct
16 Correct 106 ms 15940 KB Output is correct
17 Correct 100 ms 15940 KB Output is correct
18 Correct 108 ms 16188 KB Output is correct
19 Correct 97 ms 15936 KB Output is correct
20 Correct 102 ms 16196 KB Output is correct
21 Correct 100 ms 15968 KB Output is correct
22 Correct 99 ms 15932 KB Output is correct
23 Correct 117 ms 15940 KB Output is correct
24 Correct 111 ms 15936 KB Output is correct
25 Correct 104 ms 15940 KB Output is correct
26 Correct 92 ms 15932 KB Output is correct
27 Correct 96 ms 16112 KB Output is correct
28 Correct 99 ms 15944 KB Output is correct
29 Correct 101 ms 15940 KB Output is correct
30 Correct 120 ms 16068 KB Output is correct
31 Correct 95 ms 15944 KB Output is correct
32 Correct 102 ms 15940 KB Output is correct
33 Correct 108 ms 15936 KB Output is correct
34 Correct 91 ms 15928 KB Output is correct
35 Correct 97 ms 16076 KB Output is correct
36 Correct 99 ms 16092 KB Output is correct
37 Correct 102 ms 15944 KB Output is correct
38 Correct 112 ms 15932 KB Output is correct
39 Correct 112 ms 15936 KB Output is correct
40 Correct 99 ms 15940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2396 KB Output is correct
2 Correct 1 ms 452 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1154 ms 15196 KB Output is correct
12 Correct 1164 ms 14888 KB Output is correct
13 Correct 1120 ms 13916 KB Output is correct
14 Correct 1122 ms 14124 KB Output is correct
15 Correct 1157 ms 17232 KB Output is correct
16 Correct 1129 ms 16296 KB Output is correct
17 Correct 1159 ms 14352 KB Output is correct
18 Correct 1139 ms 18204 KB Output is correct
19 Correct 1108 ms 13104 KB Output is correct
20 Correct 1128 ms 14752 KB Output is correct
21 Correct 1204 ms 18256 KB Output is correct
22 Correct 1263 ms 20404 KB Output is correct
23 Correct 1224 ms 17488 KB Output is correct
24 Correct 1166 ms 17236 KB Output is correct
25 Correct 1209 ms 21176 KB Output is correct
26 Correct 1184 ms 17668 KB Output is correct
27 Correct 1209 ms 19684 KB Output is correct
28 Correct 1155 ms 20248 KB Output is correct
29 Correct 1176 ms 16208 KB Output is correct
30 Correct 1202 ms 20304 KB Output is correct
31 Correct 1199 ms 20044 KB Output is correct
32 Correct 1226 ms 18256 KB Output is correct
33 Correct 1167 ms 16936 KB Output is correct
34 Correct 1164 ms 17232 KB Output is correct
35 Correct 1222 ms 17476 KB Output is correct
36 Correct 1141 ms 16076 KB Output is correct
37 Correct 1263 ms 20152 KB Output is correct
38 Correct 1203 ms 16068 KB Output is correct
39 Correct 1183 ms 19244 KB Output is correct
40 Correct 1159 ms 17232 KB Output is correct
41 Correct 97 ms 16112 KB Output is correct
42 Correct 96 ms 16072 KB Output is correct
43 Correct 98 ms 15936 KB Output is correct
44 Correct 108 ms 15980 KB Output is correct
45 Correct 98 ms 16196 KB Output is correct
46 Correct 106 ms 15940 KB Output is correct
47 Correct 100 ms 15940 KB Output is correct
48 Correct 108 ms 16188 KB Output is correct
49 Correct 97 ms 15936 KB Output is correct
50 Correct 102 ms 16196 KB Output is correct
51 Correct 100 ms 15968 KB Output is correct
52 Correct 99 ms 15932 KB Output is correct
53 Correct 117 ms 15940 KB Output is correct
54 Correct 111 ms 15936 KB Output is correct
55 Correct 104 ms 15940 KB Output is correct
56 Correct 92 ms 15932 KB Output is correct
57 Correct 96 ms 16112 KB Output is correct
58 Correct 99 ms 15944 KB Output is correct
59 Correct 101 ms 15940 KB Output is correct
60 Correct 120 ms 16068 KB Output is correct
61 Correct 95 ms 15944 KB Output is correct
62 Correct 102 ms 15940 KB Output is correct
63 Correct 108 ms 15936 KB Output is correct
64 Correct 91 ms 15928 KB Output is correct
65 Correct 97 ms 16076 KB Output is correct
66 Correct 99 ms 16092 KB Output is correct
67 Correct 102 ms 15944 KB Output is correct
68 Correct 112 ms 15932 KB Output is correct
69 Correct 112 ms 15936 KB Output is correct
70 Correct 99 ms 15940 KB Output is correct
71 Correct 1324 ms 40316 KB Output is correct
72 Correct 1361 ms 40512 KB Output is correct
73 Correct 1394 ms 38980 KB Output is correct
74 Correct 1485 ms 39240 KB Output is correct
75 Correct 1397 ms 41444 KB Output is correct
76 Correct 1505 ms 39760 KB Output is correct
77 Correct 1456 ms 39628 KB Output is correct
78 Correct 1312 ms 43344 KB Output is correct
79 Correct 1403 ms 37428 KB Output is correct
80 Correct 1469 ms 40516 KB Output is correct
81 Correct 1521 ms 40416 KB Output is correct
82 Correct 1484 ms 39232 KB Output is correct
83 Correct 1373 ms 38464 KB Output is correct
84 Correct 1439 ms 39216 KB Output is correct
85 Correct 1509 ms 39480 KB Output is correct
86 Correct 1281 ms 37188 KB Output is correct
87 Correct 1369 ms 40260 KB Output is correct
88 Correct 1411 ms 37436 KB Output is correct
89 Correct 1414 ms 38972 KB Output is correct
90 Correct 1457 ms 39300 KB Output is correct
91 Correct 1379 ms 38472 KB Output is correct
92 Correct 1505 ms 39744 KB Output is correct
93 Correct 1484 ms 39588 KB Output is correct
94 Correct 1360 ms 37304 KB Output is correct
95 Correct 1431 ms 39484 KB Output is correct
96 Correct 1477 ms 39484 KB Output is correct
97 Correct 1445 ms 39476 KB Output is correct
98 Correct 1435 ms 39432 KB Output is correct
99 Correct 1438 ms 39488 KB Output is correct
100 Correct 1449 ms 39512 KB Output is correct