Submission #853890

# Submission time Handle Problem Language Result Execution time Memory
853890 2023-09-25T12:13:24 Z willychan Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1030 ms 43252 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds




int main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n,q;cin>>n>>q;
	vector<int> normal(1<<n);
	vector<int> sum1(1<<n);
	vector<int> sum0(1<<n);
	string s;cin>>s;
	for(int i=0;i<(1<<n);i++){
		normal[i] = s[i]-'0';
		sum1[i]=normal[i];
		sum0[i]=normal[i];
	}
	for(int j=0;j<n;j++){
		for(int i=0;i<(1<<n);i++){
			if((i>>j)&1) sum1[i]+=sum1[i^(1<<j)];
		}
	}
	for(int j=0;j<n;j++){
		for(int i=(1<<n)-1;i>=0;i--){
			if(!((i>>j)&1))	 sum0[i]+=sum0[i^(1<<j)];
		}
	}
	while(q--){
		string qs;cin>>qs;
		int cnt0=0;
		int cnt1=0;
		int cntq=0;
		for(int i=0;i<n;i++){
			cnt0+=(qs[i]=='0');
			cnt1+=(qs[i]=='1');
			cntq+=(qs[i]=='?');
		}
		if(cnt0<=cnt1 && cnt0<=cntq){
			int calc = 0;	
			vector<int> b;
			for(int i=0;i<n;i++){
				if(qs[i]=='1') calc+=(1<<(n-1-i));
				if(qs[i]=='0') b.push_back(i);
			}
			int ans = 0;
			for(int mask=0;mask<(1<<cnt0);mask++){
				int f = calc;	
				int cntbit=0;
				for(int i=0;i<cnt0;i++) if((mask>>i)&1){f+=(1<<(n-1-b[i]));cntbit++;}
				ans+=(((cntbit)&1)?(-1):(1))*sum0[f];
			}
			cout<<ans<<"\n";
			continue;
		}
		if(cnt1<=cnt0 && cnt1<=cntq){
			int calc = 0;	
			vector<int> b;
			for(int i=0;i<n;i++){
				if(qs[i]=='?') calc+=(1<<(n-1-i));
				if(qs[i]=='1') b.push_back(i);
			}
			int ans = 0;
			for(int mask=0;mask<(1<<cnt1);mask++){
				int f = calc;	
				int cntbit=0;
				for(int i=0;i<cnt1;i++) if((mask>>i)&1){f+=(1<<(n-1-b[i]));cntbit++;}
				ans+=(((cnt1^cntbit)&1)?(-1):(1))*sum1[f];
			}
			cout<<ans<<"\n";
			continue;	
		}
		if(cntq<=cnt1 && cntq<=cnt0){
			int calc = 0;
			vector<int> b;
			for(int i=0;i<n;i++){
				if(qs[i]=='1') calc+=(1<<(n-1-i));
				if(qs[i]=='?') b.push_back(i);
			}
			int ans = 0;
			for(int mask=0;mask<(1<<cntq);mask++){
				int f = calc;	
				for(int i=0;i<cntq;i++) if((mask>>i)&1) f+=(1<<(n-1-b[i]));
				ans+=normal[f];
			}
			cout<<ans<<"\n";
			continue;
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 186 ms 15108 KB Output is correct
12 Correct 193 ms 14676 KB Output is correct
13 Correct 263 ms 13904 KB Output is correct
14 Correct 313 ms 14276 KB Output is correct
15 Correct 243 ms 15184 KB Output is correct
16 Correct 306 ms 14360 KB Output is correct
17 Correct 292 ms 14160 KB Output is correct
18 Correct 153 ms 16208 KB Output is correct
19 Correct 180 ms 13136 KB Output is correct
20 Correct 191 ms 14928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 186 ms 15108 KB Output is correct
12 Correct 193 ms 14676 KB Output is correct
13 Correct 263 ms 13904 KB Output is correct
14 Correct 313 ms 14276 KB Output is correct
15 Correct 243 ms 15184 KB Output is correct
16 Correct 306 ms 14360 KB Output is correct
17 Correct 292 ms 14160 KB Output is correct
18 Correct 153 ms 16208 KB Output is correct
19 Correct 180 ms 13136 KB Output is correct
20 Correct 191 ms 14928 KB Output is correct
21 Correct 214 ms 18228 KB Output is correct
22 Correct 227 ms 18256 KB Output is correct
23 Correct 334 ms 17232 KB Output is correct
24 Correct 382 ms 17232 KB Output is correct
25 Correct 290 ms 19268 KB Output is correct
26 Correct 389 ms 18000 KB Output is correct
27 Correct 370 ms 17680 KB Output is correct
28 Correct 160 ms 20048 KB Output is correct
29 Correct 208 ms 16212 KB Output is correct
30 Correct 223 ms 18392 KB Output is correct
31 Correct 336 ms 18256 KB Output is correct
32 Correct 397 ms 18124 KB Output is correct
33 Correct 299 ms 17232 KB Output is correct
34 Correct 379 ms 17304 KB Output is correct
35 Correct 359 ms 17664 KB Output is correct
36 Correct 152 ms 16208 KB Output is correct
37 Correct 210 ms 18184 KB Output is correct
38 Correct 222 ms 16208 KB Output is correct
39 Correct 343 ms 17368 KB Output is correct
40 Correct 393 ms 17072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 55 ms 16116 KB Output is correct
12 Correct 62 ms 16208 KB Output is correct
13 Correct 70 ms 16116 KB Output is correct
14 Correct 94 ms 16116 KB Output is correct
15 Correct 59 ms 16216 KB Output is correct
16 Correct 86 ms 16116 KB Output is correct
17 Correct 78 ms 16116 KB Output is correct
18 Correct 50 ms 16272 KB Output is correct
19 Correct 56 ms 16220 KB Output is correct
20 Correct 55 ms 16116 KB Output is correct
21 Correct 66 ms 16120 KB Output is correct
22 Correct 90 ms 16116 KB Output is correct
23 Correct 63 ms 16116 KB Output is correct
24 Correct 84 ms 16116 KB Output is correct
25 Correct 77 ms 16116 KB Output is correct
26 Correct 51 ms 16116 KB Output is correct
27 Correct 60 ms 16116 KB Output is correct
28 Correct 57 ms 16116 KB Output is correct
29 Correct 68 ms 16120 KB Output is correct
30 Correct 88 ms 16116 KB Output is correct
31 Correct 62 ms 16116 KB Output is correct
32 Correct 84 ms 16116 KB Output is correct
33 Correct 86 ms 16116 KB Output is correct
34 Correct 54 ms 16120 KB Output is correct
35 Correct 69 ms 16124 KB Output is correct
36 Correct 69 ms 16116 KB Output is correct
37 Correct 75 ms 16116 KB Output is correct
38 Correct 71 ms 16116 KB Output is correct
39 Correct 77 ms 16116 KB Output is correct
40 Correct 77 ms 16116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 186 ms 15108 KB Output is correct
12 Correct 193 ms 14676 KB Output is correct
13 Correct 263 ms 13904 KB Output is correct
14 Correct 313 ms 14276 KB Output is correct
15 Correct 243 ms 15184 KB Output is correct
16 Correct 306 ms 14360 KB Output is correct
17 Correct 292 ms 14160 KB Output is correct
18 Correct 153 ms 16208 KB Output is correct
19 Correct 180 ms 13136 KB Output is correct
20 Correct 191 ms 14928 KB Output is correct
21 Correct 214 ms 18228 KB Output is correct
22 Correct 227 ms 18256 KB Output is correct
23 Correct 334 ms 17232 KB Output is correct
24 Correct 382 ms 17232 KB Output is correct
25 Correct 290 ms 19268 KB Output is correct
26 Correct 389 ms 18000 KB Output is correct
27 Correct 370 ms 17680 KB Output is correct
28 Correct 160 ms 20048 KB Output is correct
29 Correct 208 ms 16212 KB Output is correct
30 Correct 223 ms 18392 KB Output is correct
31 Correct 336 ms 18256 KB Output is correct
32 Correct 397 ms 18124 KB Output is correct
33 Correct 299 ms 17232 KB Output is correct
34 Correct 379 ms 17304 KB Output is correct
35 Correct 359 ms 17664 KB Output is correct
36 Correct 152 ms 16208 KB Output is correct
37 Correct 210 ms 18184 KB Output is correct
38 Correct 222 ms 16208 KB Output is correct
39 Correct 343 ms 17368 KB Output is correct
40 Correct 393 ms 17072 KB Output is correct
41 Correct 55 ms 16116 KB Output is correct
42 Correct 62 ms 16208 KB Output is correct
43 Correct 70 ms 16116 KB Output is correct
44 Correct 94 ms 16116 KB Output is correct
45 Correct 59 ms 16216 KB Output is correct
46 Correct 86 ms 16116 KB Output is correct
47 Correct 78 ms 16116 KB Output is correct
48 Correct 50 ms 16272 KB Output is correct
49 Correct 56 ms 16220 KB Output is correct
50 Correct 55 ms 16116 KB Output is correct
51 Correct 66 ms 16120 KB Output is correct
52 Correct 90 ms 16116 KB Output is correct
53 Correct 63 ms 16116 KB Output is correct
54 Correct 84 ms 16116 KB Output is correct
55 Correct 77 ms 16116 KB Output is correct
56 Correct 51 ms 16116 KB Output is correct
57 Correct 60 ms 16116 KB Output is correct
58 Correct 57 ms 16116 KB Output is correct
59 Correct 68 ms 16120 KB Output is correct
60 Correct 88 ms 16116 KB Output is correct
61 Correct 62 ms 16116 KB Output is correct
62 Correct 84 ms 16116 KB Output is correct
63 Correct 86 ms 16116 KB Output is correct
64 Correct 54 ms 16120 KB Output is correct
65 Correct 69 ms 16124 KB Output is correct
66 Correct 69 ms 16116 KB Output is correct
67 Correct 75 ms 16116 KB Output is correct
68 Correct 71 ms 16116 KB Output is correct
69 Correct 77 ms 16116 KB Output is correct
70 Correct 77 ms 16116 KB Output is correct
71 Correct 365 ms 40688 KB Output is correct
72 Correct 362 ms 40440 KB Output is correct
73 Correct 591 ms 38996 KB Output is correct
74 Correct 1030 ms 39616 KB Output is correct
75 Correct 469 ms 41340 KB Output is correct
76 Correct 884 ms 39668 KB Output is correct
77 Correct 781 ms 39668 KB Output is correct
78 Correct 267 ms 43252 KB Output is correct
79 Correct 348 ms 37364 KB Output is correct
80 Correct 356 ms 40436 KB Output is correct
81 Correct 572 ms 40464 KB Output is correct
82 Correct 983 ms 39416 KB Output is correct
83 Correct 465 ms 38648 KB Output is correct
84 Correct 959 ms 39452 KB Output is correct
85 Correct 787 ms 39636 KB Output is correct
86 Correct 275 ms 37208 KB Output is correct
87 Correct 365 ms 40440 KB Output is correct
88 Correct 360 ms 37588 KB Output is correct
89 Correct 596 ms 39208 KB Output is correct
90 Correct 1008 ms 39412 KB Output is correct
91 Correct 454 ms 38644 KB Output is correct
92 Correct 919 ms 39764 KB Output is correct
93 Correct 790 ms 39760 KB Output is correct
94 Correct 262 ms 37364 KB Output is correct
95 Correct 658 ms 39768 KB Output is correct
96 Correct 645 ms 39888 KB Output is correct
97 Correct 649 ms 39512 KB Output is correct
98 Correct 648 ms 39500 KB Output is correct
99 Correct 687 ms 39572 KB Output is correct
100 Correct 644 ms 39668 KB Output is correct