답안 #939364

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939364 2024-03-06T09:57:30 Z a_l_i_r_e_z_a Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
568 ms 34396 KB
// In the name of God
// Hope is last to die
 
#include <bits/stdc++.h>
using namespace std;
 
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
 
const int maxn = 1ll << 20;
const int inf = 1e9 + 7;
int n, q, dp[maxn], pd[maxn], res[70];
string s; 
vector<int> pos;

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> q;
    cin >> s;
    for(int i = 0; i < (1ll << n); i++){
        dp[i] = pd[(1ll << n) - 1 - i] = s[i] - '0';
    }
    for(int i = 0; i < n; i++){
        for(int mask = 0; mask < (1ll << n); mask++){
            if(!(mask & (1ll << i))){
                dp[mask] += dp[mask + (1ll << i)];
                pd[mask] += pd[mask + (1ll << i)];
            }
        }
    }
    while(q--){
        string t; cin >> t;
        reverse(all(t));
        int one = count(all(t), '1');
        int zero = count(all(t), '0');
        int ques = count(all(t), '?');
        int ans = 0;
        pos.clear();
        if(ques < 7){
            int mask = 0;
            for(int i = 0; i < n; i++){
                if(t[i] == '1') mask += 1ll << i;
                if(t[i] == '?') pos.pb(i);
            }
            res[0] = mask;
            ans = s[mask] - '0';
            for(int i = 1; i < (1ll << ques); i++){
                int b = __builtin_ctz(i);
                res[i] = res[i - (1ll << b)] + (1ll << pos[b]);
                ans += s[res[i]] - '0';
            }
        }
        else if(zero < 7){
            int mask = 0;
            for(int i = 0; i < n; i++){
                if(t[i] == '1') mask += 1ll << i;
                if(t[i] == '0') pos.pb(i);
            }
            res[0] = mask;
            ans = dp[mask];
            for(int i = 1; i < (1ll << zero); i++){
                int b = __builtin_ctz(i);
                res[i] = res[i - (1ll << b)] + (1ll << pos[b]);
                if(__builtin_parity(i)) ans -= dp[res[i]];
                else ans += dp[res[i]];
            }
        }
        else{
            int mask = 0;
            for(int i = 0; i < n; i++){
                if(t[i] == '0') mask += 1ll << i;
                if(t[i] == '1') pos.pb(i);
            }
            res[0] = mask;
            ans = pd[mask];
            for(int i = 1; i < (1ll << one); i++){
                int b = __builtin_ctz(i);
                res[i] = res[i - (1ll << b)] + (1ll << pos[b]);
                if(__builtin_parity(i)) ans -= pd[res[i]];
                else ans += pd[res[i]];
            }
        }
        cout << ans << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2524 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 2648 KB Output is correct
7 Correct 1 ms 2396 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 1 ms 2396 KB Output is correct
2 Correct 1 ms 2524 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 2648 KB Output is correct
7 Correct 1 ms 2396 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 233 ms 12164 KB Output is correct
12 Correct 249 ms 11972 KB Output is correct
13 Correct 246 ms 10968 KB Output is correct
14 Correct 218 ms 11088 KB Output is correct
15 Correct 281 ms 12112 KB Output is correct
16 Correct 237 ms 11092 KB Output is correct
17 Correct 248 ms 11264 KB Output is correct
18 Correct 147 ms 13144 KB Output is correct
19 Correct 187 ms 10360 KB Output is correct
20 Correct 237 ms 11936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2524 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 2648 KB Output is correct
7 Correct 1 ms 2396 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 233 ms 12164 KB Output is correct
12 Correct 249 ms 11972 KB Output is correct
13 Correct 246 ms 10968 KB Output is correct
14 Correct 218 ms 11088 KB Output is correct
15 Correct 281 ms 12112 KB Output is correct
16 Correct 237 ms 11092 KB Output is correct
17 Correct 248 ms 11264 KB Output is correct
18 Correct 147 ms 13144 KB Output is correct
19 Correct 187 ms 10360 KB Output is correct
20 Correct 237 ms 11936 KB Output is correct
21 Correct 218 ms 13860 KB Output is correct
22 Correct 306 ms 13868 KB Output is correct
23 Correct 265 ms 12812 KB Output is correct
24 Correct 265 ms 13136 KB Output is correct
25 Correct 239 ms 14676 KB Output is correct
26 Correct 296 ms 13444 KB Output is correct
27 Correct 327 ms 13144 KB Output is correct
28 Correct 166 ms 15564 KB Output is correct
29 Correct 208 ms 11680 KB Output is correct
30 Correct 266 ms 13772 KB Output is correct
31 Correct 332 ms 13652 KB Output is correct
32 Correct 296 ms 13696 KB Output is correct
33 Correct 254 ms 12388 KB Output is correct
34 Correct 269 ms 12576 KB Output is correct
35 Correct 317 ms 13232 KB Output is correct
36 Correct 164 ms 13004 KB Output is correct
37 Correct 319 ms 17100 KB Output is correct
38 Correct 222 ms 15188 KB Output is correct
39 Correct 282 ms 16624 KB Output is correct
40 Correct 311 ms 16272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2524 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 2648 KB Output is correct
7 Correct 1 ms 2396 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 32 ms 12020 KB Output is correct
12 Correct 36 ms 12024 KB Output is correct
13 Correct 38 ms 12016 KB Output is correct
14 Correct 48 ms 12000 KB Output is correct
15 Correct 34 ms 12024 KB Output is correct
16 Correct 47 ms 12148 KB Output is correct
17 Correct 39 ms 11932 KB Output is correct
18 Correct 31 ms 12016 KB Output is correct
19 Correct 33 ms 11760 KB Output is correct
20 Correct 36 ms 12024 KB Output is correct
21 Correct 37 ms 12020 KB Output is correct
22 Correct 42 ms 12016 KB Output is correct
23 Correct 35 ms 11960 KB Output is correct
24 Correct 40 ms 11856 KB Output is correct
25 Correct 42 ms 12020 KB Output is correct
26 Correct 29 ms 12204 KB Output is correct
27 Correct 32 ms 12100 KB Output is correct
28 Correct 34 ms 11768 KB Output is correct
29 Correct 42 ms 12016 KB Output is correct
30 Correct 47 ms 11936 KB Output is correct
31 Correct 34 ms 12028 KB Output is correct
32 Correct 41 ms 12068 KB Output is correct
33 Correct 40 ms 12020 KB Output is correct
34 Correct 29 ms 11764 KB Output is correct
35 Correct 39 ms 12016 KB Output is correct
36 Correct 39 ms 12020 KB Output is correct
37 Correct 46 ms 12024 KB Output is correct
38 Correct 39 ms 11828 KB Output is correct
39 Correct 38 ms 12024 KB Output is correct
40 Correct 39 ms 11864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2524 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 2648 KB Output is correct
7 Correct 1 ms 2396 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 233 ms 12164 KB Output is correct
12 Correct 249 ms 11972 KB Output is correct
13 Correct 246 ms 10968 KB Output is correct
14 Correct 218 ms 11088 KB Output is correct
15 Correct 281 ms 12112 KB Output is correct
16 Correct 237 ms 11092 KB Output is correct
17 Correct 248 ms 11264 KB Output is correct
18 Correct 147 ms 13144 KB Output is correct
19 Correct 187 ms 10360 KB Output is correct
20 Correct 237 ms 11936 KB Output is correct
21 Correct 218 ms 13860 KB Output is correct
22 Correct 306 ms 13868 KB Output is correct
23 Correct 265 ms 12812 KB Output is correct
24 Correct 265 ms 13136 KB Output is correct
25 Correct 239 ms 14676 KB Output is correct
26 Correct 296 ms 13444 KB Output is correct
27 Correct 327 ms 13144 KB Output is correct
28 Correct 166 ms 15564 KB Output is correct
29 Correct 208 ms 11680 KB Output is correct
30 Correct 266 ms 13772 KB Output is correct
31 Correct 332 ms 13652 KB Output is correct
32 Correct 296 ms 13696 KB Output is correct
33 Correct 254 ms 12388 KB Output is correct
34 Correct 269 ms 12576 KB Output is correct
35 Correct 317 ms 13232 KB Output is correct
36 Correct 164 ms 13004 KB Output is correct
37 Correct 319 ms 17100 KB Output is correct
38 Correct 222 ms 15188 KB Output is correct
39 Correct 282 ms 16624 KB Output is correct
40 Correct 311 ms 16272 KB Output is correct
41 Correct 32 ms 12020 KB Output is correct
42 Correct 36 ms 12024 KB Output is correct
43 Correct 38 ms 12016 KB Output is correct
44 Correct 48 ms 12000 KB Output is correct
45 Correct 34 ms 12024 KB Output is correct
46 Correct 47 ms 12148 KB Output is correct
47 Correct 39 ms 11932 KB Output is correct
48 Correct 31 ms 12016 KB Output is correct
49 Correct 33 ms 11760 KB Output is correct
50 Correct 36 ms 12024 KB Output is correct
51 Correct 37 ms 12020 KB Output is correct
52 Correct 42 ms 12016 KB Output is correct
53 Correct 35 ms 11960 KB Output is correct
54 Correct 40 ms 11856 KB Output is correct
55 Correct 42 ms 12020 KB Output is correct
56 Correct 29 ms 12204 KB Output is correct
57 Correct 32 ms 12100 KB Output is correct
58 Correct 34 ms 11768 KB Output is correct
59 Correct 42 ms 12016 KB Output is correct
60 Correct 47 ms 11936 KB Output is correct
61 Correct 34 ms 12028 KB Output is correct
62 Correct 41 ms 12068 KB Output is correct
63 Correct 40 ms 12020 KB Output is correct
64 Correct 29 ms 11764 KB Output is correct
65 Correct 39 ms 12016 KB Output is correct
66 Correct 39 ms 12020 KB Output is correct
67 Correct 46 ms 12024 KB Output is correct
68 Correct 39 ms 11828 KB Output is correct
69 Correct 38 ms 12024 KB Output is correct
70 Correct 39 ms 11864 KB Output is correct
71 Correct 321 ms 31836 KB Output is correct
72 Correct 393 ms 31520 KB Output is correct
73 Correct 389 ms 29908 KB Output is correct
74 Correct 568 ms 30384 KB Output is correct
75 Correct 357 ms 32596 KB Output is correct
76 Correct 529 ms 30760 KB Output is correct
77 Correct 458 ms 30616 KB Output is correct
78 Correct 223 ms 34396 KB Output is correct
79 Correct 335 ms 28432 KB Output is correct
80 Correct 337 ms 31476 KB Output is correct
81 Correct 432 ms 31476 KB Output is correct
82 Correct 555 ms 30352 KB Output is correct
83 Correct 408 ms 29812 KB Output is correct
84 Correct 474 ms 24724 KB Output is correct
85 Correct 452 ms 24832 KB Output is correct
86 Correct 233 ms 22816 KB Output is correct
87 Correct 306 ms 25844 KB Output is correct
88 Correct 317 ms 22812 KB Output is correct
89 Correct 416 ms 24448 KB Output is correct
90 Correct 481 ms 24692 KB Output is correct
91 Correct 338 ms 24220 KB Output is correct
92 Correct 510 ms 25060 KB Output is correct
93 Correct 476 ms 24820 KB Output is correct
94 Correct 226 ms 22700 KB Output is correct
95 Correct 413 ms 25012 KB Output is correct
96 Correct 422 ms 24788 KB Output is correct
97 Correct 408 ms 24676 KB Output is correct
98 Correct 468 ms 24924 KB Output is correct
99 Correct 409 ms 24908 KB Output is correct
100 Correct 454 ms 24760 KB Output is correct