Submission #920240

# Submission time Handle Problem Language Result Execution time Memory
920240 2024-02-02T10:23:39 Z Unforgettablepl Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1369 ms 33012 KB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;

#define int long long

int DP1[1048576];
int DP2[1048576];
int arr[1048576];
int idx[6];

inline int preprocess(const string &s,char c){
    int ans = 0;
    int cnt = 0;
    #pragma GCC unroll 20
    #pragma GCC ivdep
    for(int bit=0;bit<s.size();bit++){
        if(s[bit]==c){
            idx[cnt++]=bit;
        } else if(s[bit]=='?'){
            if(c=='1')ans|=(1<<bit);
        } else {
            if(s[bit]=='1')ans|=(1<<bit);
        }
    }
    return ans;
}

inline int applymask(int qu,int mask,int cnt){
    #pragma GCC unroll 20
    #pragma GCC ivdep
    for(int bit=0;bit<cnt;bit++){
        if(mask&(1<<bit))qu|=(1<<idx[bit]);
    }
    return qu;
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int l,q;
    cin >> l >> q;
    for(int mask=0;mask<(1<<l);mask++){
        char a;cin>>a;a-='0';
        DP1[mask]=DP2[mask]=arr[mask]=a;
    }
    for(int bit=0;bit<20;bit++){
        for(int mask=0;mask<(1<<20);mask++){
            if(mask&(1<<bit)){
                DP2[mask^(1<<bit)]+=DP2[mask];
            } else {
                DP1[mask|(1<<bit)]+=DP1[mask];
            }
        }
    }
    for(int i=1;i<=q;i++){
        string s;cin>>s;
        reverse(s.begin(),s.end());
        int c1=0,c0=0,cq=0;
        for(char&j:s) {
            if(j=='1')c1++;
            else if(j=='0')c0++;
            else if(j=='?')cq++;
        }
        if(cq<=6){
            int qu = preprocess(s,'?');
            int ans = 0;
            for(int mask=0;mask<(1<<cq);mask++){
                ans+=arr[applymask(qu,mask,cq)];
            }
            cout<<ans<<'\n';
            continue;
        }
        if(c1<=6){
            int qu = preprocess(s,'1');
            int ans = 0;
            for(int mask=0;mask<(1<<c1);mask++){
                if(__builtin_parity(mask)==(c1&1)){
                    ans+=DP1[applymask(qu,mask,c1)];
                } else {
                    ans-=DP1[applymask(qu,mask,c1)];
                }
            }
            cout<<ans<<'\n';
            continue;
        }
        if(c0<=6){
            int qu = preprocess(s,'0');
            int ans = 0;
            for(int mask=0;mask<(1<<c0);mask++){
                if(__builtin_parity(mask)){
                    ans-=DP2[applymask(qu,mask,c0)];
                } else {
                    ans+=DP2[applymask(qu,mask,c0)];
                }
            }
            cout<<ans<<'\n';
            continue;
        }
    }
}

Compilation message

snake_escaping.cpp: In function 'long long int preprocess(const string&, char)':
snake_escaping.cpp:18:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for(int bit=0;bit<s.size();bit++){
      |                   ~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 46 ms 18520 KB Output is correct
2 Correct 39 ms 18744 KB Output is correct
3 Correct 30 ms 18740 KB Output is correct
4 Correct 25 ms 18520 KB Output is correct
5 Correct 32 ms 18520 KB Output is correct
6 Correct 28 ms 18520 KB Output is correct
7 Correct 36 ms 18740 KB Output is correct
8 Correct 27 ms 18520 KB Output is correct
9 Correct 36 ms 18520 KB Output is correct
10 Correct 30 ms 18744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 18520 KB Output is correct
2 Correct 39 ms 18744 KB Output is correct
3 Correct 30 ms 18740 KB Output is correct
4 Correct 25 ms 18520 KB Output is correct
5 Correct 32 ms 18520 KB Output is correct
6 Correct 28 ms 18520 KB Output is correct
7 Correct 36 ms 18740 KB Output is correct
8 Correct 27 ms 18520 KB Output is correct
9 Correct 36 ms 18520 KB Output is correct
10 Correct 30 ms 18744 KB Output is correct
11 Correct 334 ms 22740 KB Output is correct
12 Correct 353 ms 22528 KB Output is correct
13 Correct 307 ms 21584 KB Output is correct
14 Correct 318 ms 21752 KB Output is correct
15 Correct 435 ms 22608 KB Output is correct
16 Correct 354 ms 21812 KB Output is correct
17 Correct 359 ms 21840 KB Output is correct
18 Correct 218 ms 23632 KB Output is correct
19 Correct 237 ms 20816 KB Output is correct
20 Correct 334 ms 22352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 18520 KB Output is correct
2 Correct 39 ms 18744 KB Output is correct
3 Correct 30 ms 18740 KB Output is correct
4 Correct 25 ms 18520 KB Output is correct
5 Correct 32 ms 18520 KB Output is correct
6 Correct 28 ms 18520 KB Output is correct
7 Correct 36 ms 18740 KB Output is correct
8 Correct 27 ms 18520 KB Output is correct
9 Correct 36 ms 18520 KB Output is correct
10 Correct 30 ms 18744 KB Output is correct
11 Correct 334 ms 22740 KB Output is correct
12 Correct 353 ms 22528 KB Output is correct
13 Correct 307 ms 21584 KB Output is correct
14 Correct 318 ms 21752 KB Output is correct
15 Correct 435 ms 22608 KB Output is correct
16 Correct 354 ms 21812 KB Output is correct
17 Correct 359 ms 21840 KB Output is correct
18 Correct 218 ms 23632 KB Output is correct
19 Correct 237 ms 20816 KB Output is correct
20 Correct 334 ms 22352 KB Output is correct
21 Correct 491 ms 22816 KB Output is correct
22 Correct 388 ms 22876 KB Output is correct
23 Correct 403 ms 22080 KB Output is correct
24 Correct 404 ms 21860 KB Output is correct
25 Correct 353 ms 23888 KB Output is correct
26 Correct 435 ms 22608 KB Output is correct
27 Correct 425 ms 22612 KB Output is correct
28 Correct 239 ms 24656 KB Output is correct
29 Correct 298 ms 20748 KB Output is correct
30 Correct 445 ms 22856 KB Output is correct
31 Correct 517 ms 22608 KB Output is correct
32 Correct 434 ms 22608 KB Output is correct
33 Correct 350 ms 21584 KB Output is correct
34 Correct 406 ms 21584 KB Output is correct
35 Correct 425 ms 22260 KB Output is correct
36 Correct 227 ms 20672 KB Output is correct
37 Correct 473 ms 22756 KB Output is correct
38 Correct 337 ms 20756 KB Output is correct
39 Correct 431 ms 21948 KB Output is correct
40 Correct 421 ms 21872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 18520 KB Output is correct
2 Correct 39 ms 18744 KB Output is correct
3 Correct 30 ms 18740 KB Output is correct
4 Correct 25 ms 18520 KB Output is correct
5 Correct 32 ms 18520 KB Output is correct
6 Correct 28 ms 18520 KB Output is correct
7 Correct 36 ms 18740 KB Output is correct
8 Correct 27 ms 18520 KB Output is correct
9 Correct 36 ms 18520 KB Output is correct
10 Correct 30 ms 18744 KB Output is correct
11 Correct 58 ms 25316 KB Output is correct
12 Correct 61 ms 25168 KB Output is correct
13 Correct 65 ms 25240 KB Output is correct
14 Correct 78 ms 25308 KB Output is correct
15 Correct 57 ms 25172 KB Output is correct
16 Correct 126 ms 25168 KB Output is correct
17 Correct 112 ms 25092 KB Output is correct
18 Correct 50 ms 25424 KB Output is correct
19 Correct 56 ms 24984 KB Output is correct
20 Correct 56 ms 25168 KB Output is correct
21 Correct 64 ms 25168 KB Output is correct
22 Correct 67 ms 25168 KB Output is correct
23 Correct 55 ms 25168 KB Output is correct
24 Correct 73 ms 25200 KB Output is correct
25 Correct 89 ms 25296 KB Output is correct
26 Correct 49 ms 24920 KB Output is correct
27 Correct 54 ms 25168 KB Output is correct
28 Correct 55 ms 24920 KB Output is correct
29 Correct 66 ms 25172 KB Output is correct
30 Correct 69 ms 25168 KB Output is correct
31 Correct 59 ms 25168 KB Output is correct
32 Correct 84 ms 25168 KB Output is correct
33 Correct 89 ms 25168 KB Output is correct
34 Correct 48 ms 24924 KB Output is correct
35 Correct 85 ms 25168 KB Output is correct
36 Correct 75 ms 25092 KB Output is correct
37 Correct 79 ms 25168 KB Output is correct
38 Correct 76 ms 25248 KB Output is correct
39 Correct 80 ms 25168 KB Output is correct
40 Correct 90 ms 25324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 18520 KB Output is correct
2 Correct 39 ms 18744 KB Output is correct
3 Correct 30 ms 18740 KB Output is correct
4 Correct 25 ms 18520 KB Output is correct
5 Correct 32 ms 18520 KB Output is correct
6 Correct 28 ms 18520 KB Output is correct
7 Correct 36 ms 18740 KB Output is correct
8 Correct 27 ms 18520 KB Output is correct
9 Correct 36 ms 18520 KB Output is correct
10 Correct 30 ms 18744 KB Output is correct
11 Correct 334 ms 22740 KB Output is correct
12 Correct 353 ms 22528 KB Output is correct
13 Correct 307 ms 21584 KB Output is correct
14 Correct 318 ms 21752 KB Output is correct
15 Correct 435 ms 22608 KB Output is correct
16 Correct 354 ms 21812 KB Output is correct
17 Correct 359 ms 21840 KB Output is correct
18 Correct 218 ms 23632 KB Output is correct
19 Correct 237 ms 20816 KB Output is correct
20 Correct 334 ms 22352 KB Output is correct
21 Correct 491 ms 22816 KB Output is correct
22 Correct 388 ms 22876 KB Output is correct
23 Correct 403 ms 22080 KB Output is correct
24 Correct 404 ms 21860 KB Output is correct
25 Correct 353 ms 23888 KB Output is correct
26 Correct 435 ms 22608 KB Output is correct
27 Correct 425 ms 22612 KB Output is correct
28 Correct 239 ms 24656 KB Output is correct
29 Correct 298 ms 20748 KB Output is correct
30 Correct 445 ms 22856 KB Output is correct
31 Correct 517 ms 22608 KB Output is correct
32 Correct 434 ms 22608 KB Output is correct
33 Correct 350 ms 21584 KB Output is correct
34 Correct 406 ms 21584 KB Output is correct
35 Correct 425 ms 22260 KB Output is correct
36 Correct 227 ms 20672 KB Output is correct
37 Correct 473 ms 22756 KB Output is correct
38 Correct 337 ms 20756 KB Output is correct
39 Correct 431 ms 21948 KB Output is correct
40 Correct 421 ms 21872 KB Output is correct
41 Correct 58 ms 25316 KB Output is correct
42 Correct 61 ms 25168 KB Output is correct
43 Correct 65 ms 25240 KB Output is correct
44 Correct 78 ms 25308 KB Output is correct
45 Correct 57 ms 25172 KB Output is correct
46 Correct 126 ms 25168 KB Output is correct
47 Correct 112 ms 25092 KB Output is correct
48 Correct 50 ms 25424 KB Output is correct
49 Correct 56 ms 24984 KB Output is correct
50 Correct 56 ms 25168 KB Output is correct
51 Correct 64 ms 25168 KB Output is correct
52 Correct 67 ms 25168 KB Output is correct
53 Correct 55 ms 25168 KB Output is correct
54 Correct 73 ms 25200 KB Output is correct
55 Correct 89 ms 25296 KB Output is correct
56 Correct 49 ms 24920 KB Output is correct
57 Correct 54 ms 25168 KB Output is correct
58 Correct 55 ms 24920 KB Output is correct
59 Correct 66 ms 25172 KB Output is correct
60 Correct 69 ms 25168 KB Output is correct
61 Correct 59 ms 25168 KB Output is correct
62 Correct 84 ms 25168 KB Output is correct
63 Correct 89 ms 25168 KB Output is correct
64 Correct 48 ms 24924 KB Output is correct
65 Correct 85 ms 25168 KB Output is correct
66 Correct 75 ms 25092 KB Output is correct
67 Correct 79 ms 25168 KB Output is correct
68 Correct 76 ms 25248 KB Output is correct
69 Correct 80 ms 25168 KB Output is correct
70 Correct 90 ms 25324 KB Output is correct
71 Correct 418 ms 30288 KB Output is correct
72 Correct 444 ms 30076 KB Output is correct
73 Correct 572 ms 28684 KB Output is correct
74 Correct 697 ms 29028 KB Output is correct
75 Correct 448 ms 31056 KB Output is correct
76 Correct 1369 ms 29292 KB Output is correct
77 Correct 1075 ms 29156 KB Output is correct
78 Correct 335 ms 33012 KB Output is correct
79 Correct 394 ms 27216 KB Output is correct
80 Correct 451 ms 30288 KB Output is correct
81 Correct 564 ms 30068 KB Output is correct
82 Correct 695 ms 29012 KB Output is correct
83 Correct 436 ms 28088 KB Output is correct
84 Correct 677 ms 28992 KB Output is correct
85 Correct 1096 ms 29264 KB Output is correct
86 Correct 282 ms 27224 KB Output is correct
87 Correct 416 ms 29836 KB Output is correct
88 Correct 396 ms 26980 KB Output is correct
89 Correct 551 ms 28752 KB Output is correct
90 Correct 685 ms 29212 KB Output is correct
91 Correct 444 ms 28496 KB Output is correct
92 Correct 859 ms 29776 KB Output is correct
93 Correct 1073 ms 29264 KB Output is correct
94 Correct 294 ms 27216 KB Output is correct
95 Correct 885 ms 29028 KB Output is correct
96 Correct 916 ms 29008 KB Output is correct
97 Correct 877 ms 29104 KB Output is correct
98 Correct 878 ms 29204 KB Output is correct
99 Correct 903 ms 29064 KB Output is correct
100 Correct 913 ms 29264 KB Output is correct