#include<bits/stdc++.h>
using namespace std;
#define PROB "solve"
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;(i)++)
#define FOD(i,a,b) for(int i=a;i>=b;(i)--)
#define pii pair<int,int>
#define fi first
#define se second
#define SZ(x) int(x.size())
const int N=4e6+9,M=(1<<21)-1;
const ll mod=1e9+7;
int a[N],f[N],g[N];
int n;
bool bit(int p,int i){
return (p>>i)&1;
}
void pp(){
FOR(i,0,n-1)FOR(mask,0,(1<<n)-1){
if(bit(mask,i))f[mask]+=f[mask^(1<<i)];
}
FOR(i,0,n-1)FOD(mask,(1<<n)-1,0){
if(!bit(mask,i))g[mask]+=g[mask^(1<<i)];
}
}
int so0(int s,int t) {
int res=0;
//cout<<s<<" "<<res<<"\n\n";
for(int mask=t;;mask=(mask-1)&t){
//cout<<mask<<" "<<(s|mask)<<" "<<g[s|mask]<<'\n';
if(__builtin_popcount(mask)&1)res-=g[s|mask];
else res+=g[s|mask];
if(mask==0)break;
}
return res;
}
int so1(int s,int t){
int res=0;
for(int mask=t;;mask=(mask-1)&t){
if(__builtin_popcount(mask)&1)res-=f[s^mask];
else res+=f[s^mask];
if(mask==0)break;
}
return res;
}
int so2(int s,int t){
int res=0;
for(int mask=t;;mask=(mask-1)&t){
res+=a[s|mask];
if(mask==0)break;
}
return res;
}
void giai(){
int q;
cin>>n>>q;
FOR(i,0,(1<<n)-1){
char x;cin>>x;
a[i]=x-'0';
f[i]+=x-'0';
g[i]+=x-'0';
}
pp();
FOR(i,1,q){
int s[3]={0,0,0},u=0,u1,k[3]={0,0,0};
FOR(j,0,n-1){
char x;cin>>x;
u*=2;
FOR(t,0,2)k[t]*=2;
if(x=='0'){
s[0]++;
k[0]++;
}
else if(x=='1'){
s[1]++;
u++;
k[1]++;
}
else{
s[2]++;
k[2]++;
}
}
//cout<<u<<" "<<k[0]<<" "<<k[1]<<" "<<k[2]<<'\n';
if(s[0]<=6)cout<<so0(u,k[0])<<'\n';
else if(s[1]<=6)cout<<so1(u|k[2],k[1])<<'\n';
else cout<<so2(u,k[2])<<'\n';
}
}
int main(){
if(fopen(PROB".inp","r")){
freopen(PROB".inp","r",stdin);
freopen(PROB".out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
giai();
}
Compilation message
snake_escaping.cpp: In function 'void giai()':
snake_escaping.cpp:65:30: warning: unused variable 'u1' [-Wunused-variable]
65 | int s[3]={0,0,0},u=0,u1,k[3]={0,0,0};
| ^~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(PROB".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(PROB".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
225 ms |
15040 KB |
Output is correct |
12 |
Correct |
338 ms |
14756 KB |
Output is correct |
13 |
Correct |
356 ms |
14012 KB |
Output is correct |
14 |
Correct |
295 ms |
14096 KB |
Output is correct |
15 |
Correct |
247 ms |
15080 KB |
Output is correct |
16 |
Correct |
280 ms |
14268 KB |
Output is correct |
17 |
Correct |
308 ms |
14140 KB |
Output is correct |
18 |
Correct |
174 ms |
16152 KB |
Output is correct |
19 |
Correct |
315 ms |
12980 KB |
Output is correct |
20 |
Correct |
250 ms |
14788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
225 ms |
15040 KB |
Output is correct |
12 |
Correct |
338 ms |
14756 KB |
Output is correct |
13 |
Correct |
356 ms |
14012 KB |
Output is correct |
14 |
Correct |
295 ms |
14096 KB |
Output is correct |
15 |
Correct |
247 ms |
15080 KB |
Output is correct |
16 |
Correct |
280 ms |
14268 KB |
Output is correct |
17 |
Correct |
308 ms |
14140 KB |
Output is correct |
18 |
Correct |
174 ms |
16152 KB |
Output is correct |
19 |
Correct |
315 ms |
12980 KB |
Output is correct |
20 |
Correct |
250 ms |
14788 KB |
Output is correct |
21 |
Correct |
267 ms |
17576 KB |
Output is correct |
22 |
Correct |
340 ms |
17788 KB |
Output is correct |
23 |
Correct |
498 ms |
16844 KB |
Output is correct |
24 |
Correct |
397 ms |
17072 KB |
Output is correct |
25 |
Correct |
299 ms |
18636 KB |
Output is correct |
26 |
Correct |
362 ms |
17512 KB |
Output is correct |
27 |
Correct |
386 ms |
17464 KB |
Output is correct |
28 |
Correct |
200 ms |
19640 KB |
Output is correct |
29 |
Correct |
458 ms |
15640 KB |
Output is correct |
30 |
Correct |
284 ms |
17776 KB |
Output is correct |
31 |
Correct |
373 ms |
17672 KB |
Output is correct |
32 |
Correct |
378 ms |
17660 KB |
Output is correct |
33 |
Correct |
312 ms |
16952 KB |
Output is correct |
34 |
Correct |
403 ms |
17012 KB |
Output is correct |
35 |
Correct |
407 ms |
17500 KB |
Output is correct |
36 |
Correct |
195 ms |
16124 KB |
Output is correct |
37 |
Correct |
273 ms |
18076 KB |
Output is correct |
38 |
Correct |
419 ms |
16080 KB |
Output is correct |
39 |
Correct |
348 ms |
17212 KB |
Output is correct |
40 |
Correct |
343 ms |
17032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
73 ms |
14892 KB |
Output is correct |
12 |
Correct |
89 ms |
15016 KB |
Output is correct |
13 |
Correct |
78 ms |
14796 KB |
Output is correct |
14 |
Correct |
85 ms |
14796 KB |
Output is correct |
15 |
Correct |
69 ms |
14924 KB |
Output is correct |
16 |
Correct |
82 ms |
14804 KB |
Output is correct |
17 |
Correct |
98 ms |
14868 KB |
Output is correct |
18 |
Correct |
59 ms |
15048 KB |
Output is correct |
19 |
Correct |
71 ms |
14668 KB |
Output is correct |
20 |
Correct |
82 ms |
14896 KB |
Output is correct |
21 |
Correct |
76 ms |
14928 KB |
Output is correct |
22 |
Correct |
82 ms |
14792 KB |
Output is correct |
23 |
Correct |
67 ms |
14796 KB |
Output is correct |
24 |
Correct |
83 ms |
14772 KB |
Output is correct |
25 |
Correct |
80 ms |
14808 KB |
Output is correct |
26 |
Correct |
59 ms |
14680 KB |
Output is correct |
27 |
Correct |
68 ms |
14920 KB |
Output is correct |
28 |
Correct |
78 ms |
14744 KB |
Output is correct |
29 |
Correct |
72 ms |
14812 KB |
Output is correct |
30 |
Correct |
81 ms |
14784 KB |
Output is correct |
31 |
Correct |
70 ms |
14808 KB |
Output is correct |
32 |
Correct |
83 ms |
14808 KB |
Output is correct |
33 |
Correct |
90 ms |
15064 KB |
Output is correct |
34 |
Correct |
59 ms |
14668 KB |
Output is correct |
35 |
Correct |
77 ms |
14884 KB |
Output is correct |
36 |
Correct |
94 ms |
14860 KB |
Output is correct |
37 |
Correct |
79 ms |
14804 KB |
Output is correct |
38 |
Correct |
90 ms |
14924 KB |
Output is correct |
39 |
Correct |
76 ms |
14888 KB |
Output is correct |
40 |
Correct |
80 ms |
14800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
225 ms |
15040 KB |
Output is correct |
12 |
Correct |
338 ms |
14756 KB |
Output is correct |
13 |
Correct |
356 ms |
14012 KB |
Output is correct |
14 |
Correct |
295 ms |
14096 KB |
Output is correct |
15 |
Correct |
247 ms |
15080 KB |
Output is correct |
16 |
Correct |
280 ms |
14268 KB |
Output is correct |
17 |
Correct |
308 ms |
14140 KB |
Output is correct |
18 |
Correct |
174 ms |
16152 KB |
Output is correct |
19 |
Correct |
315 ms |
12980 KB |
Output is correct |
20 |
Correct |
250 ms |
14788 KB |
Output is correct |
21 |
Correct |
267 ms |
17576 KB |
Output is correct |
22 |
Correct |
340 ms |
17788 KB |
Output is correct |
23 |
Correct |
498 ms |
16844 KB |
Output is correct |
24 |
Correct |
397 ms |
17072 KB |
Output is correct |
25 |
Correct |
299 ms |
18636 KB |
Output is correct |
26 |
Correct |
362 ms |
17512 KB |
Output is correct |
27 |
Correct |
386 ms |
17464 KB |
Output is correct |
28 |
Correct |
200 ms |
19640 KB |
Output is correct |
29 |
Correct |
458 ms |
15640 KB |
Output is correct |
30 |
Correct |
284 ms |
17776 KB |
Output is correct |
31 |
Correct |
373 ms |
17672 KB |
Output is correct |
32 |
Correct |
378 ms |
17660 KB |
Output is correct |
33 |
Correct |
312 ms |
16952 KB |
Output is correct |
34 |
Correct |
403 ms |
17012 KB |
Output is correct |
35 |
Correct |
407 ms |
17500 KB |
Output is correct |
36 |
Correct |
195 ms |
16124 KB |
Output is correct |
37 |
Correct |
273 ms |
18076 KB |
Output is correct |
38 |
Correct |
419 ms |
16080 KB |
Output is correct |
39 |
Correct |
348 ms |
17212 KB |
Output is correct |
40 |
Correct |
343 ms |
17032 KB |
Output is correct |
41 |
Correct |
73 ms |
14892 KB |
Output is correct |
42 |
Correct |
89 ms |
15016 KB |
Output is correct |
43 |
Correct |
78 ms |
14796 KB |
Output is correct |
44 |
Correct |
85 ms |
14796 KB |
Output is correct |
45 |
Correct |
69 ms |
14924 KB |
Output is correct |
46 |
Correct |
82 ms |
14804 KB |
Output is correct |
47 |
Correct |
98 ms |
14868 KB |
Output is correct |
48 |
Correct |
59 ms |
15048 KB |
Output is correct |
49 |
Correct |
71 ms |
14668 KB |
Output is correct |
50 |
Correct |
82 ms |
14896 KB |
Output is correct |
51 |
Correct |
76 ms |
14928 KB |
Output is correct |
52 |
Correct |
82 ms |
14792 KB |
Output is correct |
53 |
Correct |
67 ms |
14796 KB |
Output is correct |
54 |
Correct |
83 ms |
14772 KB |
Output is correct |
55 |
Correct |
80 ms |
14808 KB |
Output is correct |
56 |
Correct |
59 ms |
14680 KB |
Output is correct |
57 |
Correct |
68 ms |
14920 KB |
Output is correct |
58 |
Correct |
78 ms |
14744 KB |
Output is correct |
59 |
Correct |
72 ms |
14812 KB |
Output is correct |
60 |
Correct |
81 ms |
14784 KB |
Output is correct |
61 |
Correct |
70 ms |
14808 KB |
Output is correct |
62 |
Correct |
83 ms |
14808 KB |
Output is correct |
63 |
Correct |
90 ms |
15064 KB |
Output is correct |
64 |
Correct |
59 ms |
14668 KB |
Output is correct |
65 |
Correct |
77 ms |
14884 KB |
Output is correct |
66 |
Correct |
94 ms |
14860 KB |
Output is correct |
67 |
Correct |
79 ms |
14804 KB |
Output is correct |
68 |
Correct |
90 ms |
14924 KB |
Output is correct |
69 |
Correct |
76 ms |
14888 KB |
Output is correct |
70 |
Correct |
80 ms |
14800 KB |
Output is correct |
71 |
Correct |
445 ms |
39168 KB |
Output is correct |
72 |
Correct |
479 ms |
39248 KB |
Output is correct |
73 |
Correct |
594 ms |
37788 KB |
Output is correct |
74 |
Correct |
750 ms |
38272 KB |
Output is correct |
75 |
Correct |
471 ms |
40128 KB |
Output is correct |
76 |
Correct |
759 ms |
38432 KB |
Output is correct |
77 |
Correct |
760 ms |
38292 KB |
Output is correct |
78 |
Correct |
299 ms |
42188 KB |
Output is correct |
79 |
Correct |
457 ms |
36076 KB |
Output is correct |
80 |
Correct |
473 ms |
39220 KB |
Output is correct |
81 |
Correct |
609 ms |
39124 KB |
Output is correct |
82 |
Correct |
731 ms |
38184 KB |
Output is correct |
83 |
Correct |
465 ms |
37272 KB |
Output is correct |
84 |
Correct |
761 ms |
38128 KB |
Output is correct |
85 |
Correct |
779 ms |
38356 KB |
Output is correct |
86 |
Correct |
299 ms |
36176 KB |
Output is correct |
87 |
Correct |
444 ms |
39168 KB |
Output is correct |
88 |
Correct |
458 ms |
36172 KB |
Output is correct |
89 |
Correct |
615 ms |
37804 KB |
Output is correct |
90 |
Correct |
554 ms |
38092 KB |
Output is correct |
91 |
Correct |
495 ms |
37312 KB |
Output is correct |
92 |
Correct |
796 ms |
38476 KB |
Output is correct |
93 |
Correct |
696 ms |
38300 KB |
Output is correct |
94 |
Correct |
291 ms |
36156 KB |
Output is correct |
95 |
Correct |
604 ms |
38212 KB |
Output is correct |
96 |
Correct |
725 ms |
38244 KB |
Output is correct |
97 |
Correct |
653 ms |
38200 KB |
Output is correct |
98 |
Correct |
659 ms |
38204 KB |
Output is correct |
99 |
Correct |
656 ms |
38208 KB |
Output is correct |
100 |
Correct |
684 ms |
38436 KB |
Output is correct |