Submission #1052150

# Submission time Handle Problem Language Result Execution time Memory
1052150 2024-08-10T12:07:36 Z vjudge1 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1215 ms 55412 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pi;
typedef pair<long long,long long> pl;

#define all(s) s.begin(),s.end()
#define F first
#define S second
#define sz(a) a.size()

const ll mod = 1e9+7;
const ll INF = 1e18;
const int inf = 1e9+200;
const int N=1e5 + 10;

int n,q;
ll a[11*N];
ll sub[11*N],sup[11*N];

void solve() {
    cin>>n>>q;
    for(int i=0;i<(1<<n);++i) {
        char cc;
        cin>>cc;
        a[i]=(cc-'0');
    }
    for(int i=0;i<(1<<n);++i) {
        sub[i]=a[i];
        sup[i]=a[i];
    }
    for(int i=0;i<n;++i) {
        for(int mask=0;mask<(1<<n);++mask) {
            if(mask & (1<<i)) {
                sub[mask] += sub[mask^(1<<i)];
            }
        }
    }
    for(int i=0;i<n;++i) {
        for(int mask=(1<<n)-1;mask>=0;--mask) {
            if(!(mask & (1<<i))) {
                sup[mask] += sup[mask^(1<<i)];
            }
        }
    }
    while(q--) {
        string s;
        cin>>s;
        int cnt[3];
        fill(cnt,cnt+3,0);
        ll sum=0;
        vector<int> b;
        reverse(all(s));
        vector<int> b1;
        vector<int> b2;
        for(int i=0;i<n;++i) {
            if(s[i]=='?') {
                cnt[2]++;
                b.push_back(i);
            }
            else if(s[i]=='1') {
                cnt[1]++;
                sum+=(1<<i);
                b1.push_back(i);
            }
            else {
                cnt[0]++;
                b2.push_back(i);
            }
        }
        if(cnt[2]<=6) {
            int ans = 0;
            for(int mask=0;mask<(1<<cnt[2]);++mask) {
                int t=0;
                for(int i=0;i<cnt[2];++i) {
                    if((mask>>i)&1) {
                         t+=(1<<(b[i]));
                    }
                }
                ans+=a[sum + t];
            }
            cout<<ans<<'\n';
        }
        else if(cnt[1]<=6) {
            int ans = 0;
            for(int i=0;i<n;++i) {
                if(s[i]=='?'){
                    sum+=(1<<i);
                }
            }
            for(int mask=0;mask<(1<<cnt[1]);++mask) {
                int t=0;
                for(int i=0;i<cnt[1];++i) {
                    if(!((mask>>i)&1)) {
                        t+=(1<<(b1[i]));
                    }
                }
                if((cnt[1]-__builtin_popcount(mask))&1) {
                    ans-=sub[sum-t];
                }
                else {
                    ans+=sub[sum-t];
                }
            }
            cout<<ans<<'\n';
        }
        else if(cnt[0]<=6) {
            int ans = 0;
            for(int mask=0;mask<(1<<cnt[0]);++mask) {
                int t=0;
                for(int i=0;i<cnt[0];++i) {
                    if(((mask>>i)&1)) {
                        t+=(1<<(b2[i]));
                    }
                }
                if((__builtin_popcount(mask))&1) {
                    ans-=sup[sum+t];
                }
                else {
                    ans+=sup[sum+t];
                }
            }
            cout<<ans<<'\n';
        }
    }
}  

int main() {
    ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--) {
        solve();
    }
    return 0;
} 
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4568 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4568 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 356 ms 19540 KB Output is correct
12 Correct 391 ms 18768 KB Output is correct
13 Correct 305 ms 18244 KB Output is correct
14 Correct 297 ms 18524 KB Output is correct
15 Correct 519 ms 19288 KB Output is correct
16 Correct 351 ms 18712 KB Output is correct
17 Correct 361 ms 18256 KB Output is correct
18 Correct 200 ms 20240 KB Output is correct
19 Correct 234 ms 17232 KB Output is correct
20 Correct 409 ms 19028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4568 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 356 ms 19540 KB Output is correct
12 Correct 391 ms 18768 KB Output is correct
13 Correct 305 ms 18244 KB Output is correct
14 Correct 297 ms 18524 KB Output is correct
15 Correct 519 ms 19288 KB Output is correct
16 Correct 351 ms 18712 KB Output is correct
17 Correct 361 ms 18256 KB Output is correct
18 Correct 200 ms 20240 KB Output is correct
19 Correct 234 ms 17232 KB Output is correct
20 Correct 409 ms 19028 KB Output is correct
21 Correct 844 ms 22092 KB Output is correct
22 Correct 413 ms 22248 KB Output is correct
23 Correct 398 ms 21328 KB Output is correct
24 Correct 384 ms 21240 KB Output is correct
25 Correct 380 ms 23124 KB Output is correct
26 Correct 458 ms 21864 KB Output is correct
27 Correct 485 ms 21584 KB Output is correct
28 Correct 186 ms 24148 KB Output is correct
29 Correct 256 ms 20308 KB Output is correct
30 Correct 605 ms 22400 KB Output is correct
31 Correct 612 ms 22100 KB Output is correct
32 Correct 459 ms 22100 KB Output is correct
33 Correct 346 ms 21032 KB Output is correct
34 Correct 413 ms 21112 KB Output is correct
35 Correct 551 ms 21560 KB Output is correct
36 Correct 191 ms 20268 KB Output is correct
37 Correct 576 ms 22300 KB Output is correct
38 Correct 414 ms 20164 KB Output is correct
39 Correct 502 ms 21428 KB Output is correct
40 Correct 470 ms 21312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4568 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 93 ms 27988 KB Output is correct
12 Correct 71 ms 28172 KB Output is correct
13 Correct 96 ms 27900 KB Output is correct
14 Correct 91 ms 27916 KB Output is correct
15 Correct 76 ms 28164 KB Output is correct
16 Correct 152 ms 28072 KB Output is correct
17 Correct 123 ms 27988 KB Output is correct
18 Correct 53 ms 28172 KB Output is correct
19 Correct 69 ms 27984 KB Output is correct
20 Correct 77 ms 27984 KB Output is correct
21 Correct 112 ms 27988 KB Output is correct
22 Correct 134 ms 28096 KB Output is correct
23 Correct 62 ms 27988 KB Output is correct
24 Correct 86 ms 27988 KB Output is correct
25 Correct 126 ms 27904 KB Output is correct
26 Correct 88 ms 27984 KB Output is correct
27 Correct 99 ms 28156 KB Output is correct
28 Correct 74 ms 27836 KB Output is correct
29 Correct 84 ms 27900 KB Output is correct
30 Correct 86 ms 27904 KB Output is correct
31 Correct 68 ms 27904 KB Output is correct
32 Correct 118 ms 27900 KB Output is correct
33 Correct 97 ms 27900 KB Output is correct
34 Correct 60 ms 27844 KB Output is correct
35 Correct 96 ms 28092 KB Output is correct
36 Correct 100 ms 27908 KB Output is correct
37 Correct 92 ms 28076 KB Output is correct
38 Correct 89 ms 27904 KB Output is correct
39 Correct 88 ms 27904 KB Output is correct
40 Correct 95 ms 27984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4568 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 356 ms 19540 KB Output is correct
12 Correct 391 ms 18768 KB Output is correct
13 Correct 305 ms 18244 KB Output is correct
14 Correct 297 ms 18524 KB Output is correct
15 Correct 519 ms 19288 KB Output is correct
16 Correct 351 ms 18712 KB Output is correct
17 Correct 361 ms 18256 KB Output is correct
18 Correct 200 ms 20240 KB Output is correct
19 Correct 234 ms 17232 KB Output is correct
20 Correct 409 ms 19028 KB Output is correct
21 Correct 844 ms 22092 KB Output is correct
22 Correct 413 ms 22248 KB Output is correct
23 Correct 398 ms 21328 KB Output is correct
24 Correct 384 ms 21240 KB Output is correct
25 Correct 380 ms 23124 KB Output is correct
26 Correct 458 ms 21864 KB Output is correct
27 Correct 485 ms 21584 KB Output is correct
28 Correct 186 ms 24148 KB Output is correct
29 Correct 256 ms 20308 KB Output is correct
30 Correct 605 ms 22400 KB Output is correct
31 Correct 612 ms 22100 KB Output is correct
32 Correct 459 ms 22100 KB Output is correct
33 Correct 346 ms 21032 KB Output is correct
34 Correct 413 ms 21112 KB Output is correct
35 Correct 551 ms 21560 KB Output is correct
36 Correct 191 ms 20268 KB Output is correct
37 Correct 576 ms 22300 KB Output is correct
38 Correct 414 ms 20164 KB Output is correct
39 Correct 502 ms 21428 KB Output is correct
40 Correct 470 ms 21312 KB Output is correct
41 Correct 93 ms 27988 KB Output is correct
42 Correct 71 ms 28172 KB Output is correct
43 Correct 96 ms 27900 KB Output is correct
44 Correct 91 ms 27916 KB Output is correct
45 Correct 76 ms 28164 KB Output is correct
46 Correct 152 ms 28072 KB Output is correct
47 Correct 123 ms 27988 KB Output is correct
48 Correct 53 ms 28172 KB Output is correct
49 Correct 69 ms 27984 KB Output is correct
50 Correct 77 ms 27984 KB Output is correct
51 Correct 112 ms 27988 KB Output is correct
52 Correct 134 ms 28096 KB Output is correct
53 Correct 62 ms 27988 KB Output is correct
54 Correct 86 ms 27988 KB Output is correct
55 Correct 126 ms 27904 KB Output is correct
56 Correct 88 ms 27984 KB Output is correct
57 Correct 99 ms 28156 KB Output is correct
58 Correct 74 ms 27836 KB Output is correct
59 Correct 84 ms 27900 KB Output is correct
60 Correct 86 ms 27904 KB Output is correct
61 Correct 68 ms 27904 KB Output is correct
62 Correct 118 ms 27900 KB Output is correct
63 Correct 97 ms 27900 KB Output is correct
64 Correct 60 ms 27844 KB Output is correct
65 Correct 96 ms 28092 KB Output is correct
66 Correct 100 ms 27908 KB Output is correct
67 Correct 92 ms 28076 KB Output is correct
68 Correct 89 ms 27904 KB Output is correct
69 Correct 88 ms 27904 KB Output is correct
70 Correct 95 ms 27984 KB Output is correct
71 Correct 485 ms 52432 KB Output is correct
72 Correct 609 ms 52540 KB Output is correct
73 Correct 747 ms 50944 KB Output is correct
74 Correct 1215 ms 51232 KB Output is correct
75 Correct 646 ms 53376 KB Output is correct
76 Correct 948 ms 51572 KB Output is correct
77 Correct 847 ms 51560 KB Output is correct
78 Correct 272 ms 55412 KB Output is correct
79 Correct 394 ms 49416 KB Output is correct
80 Correct 460 ms 52560 KB Output is correct
81 Correct 822 ms 52336 KB Output is correct
82 Correct 956 ms 51460 KB Output is correct
83 Correct 493 ms 50564 KB Output is correct
84 Correct 851 ms 51460 KB Output is correct
85 Correct 844 ms 51476 KB Output is correct
86 Correct 246 ms 49444 KB Output is correct
87 Correct 512 ms 52308 KB Output is correct
88 Correct 467 ms 49408 KB Output is correct
89 Correct 667 ms 50980 KB Output is correct
90 Correct 865 ms 51300 KB Output is correct
91 Correct 545 ms 50576 KB Output is correct
92 Correct 1137 ms 51744 KB Output is correct
93 Correct 1036 ms 51504 KB Output is correct
94 Correct 274 ms 49416 KB Output is correct
95 Correct 835 ms 51516 KB Output is correct
96 Correct 774 ms 51536 KB Output is correct
97 Correct 707 ms 51536 KB Output is correct
98 Correct 741 ms 51540 KB Output is correct
99 Correct 755 ms 51540 KB Output is correct
100 Correct 700 ms 51544 KB Output is correct