#include <bits/stdc++.h>
using namespace std;
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0)
#define fori(x,a,b) for (int x=a;x<=b;++x)
#define ford(x,a,b) for (int x=a;x>=b;x--)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define mp make_pair
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define getbit(x,i) ((x>>i)&1)
#define batbit(x,i) (x|(1ll<<i))
#define tatbit(x,i) (x&~(1<<i))
#define gg exit(0);
const int N = (1<<20)+3;
int f1[N],f0[N],cnt[N],val[N],n,t;
main()
{
//freopen("task.inp","r",stdin);
fasty;
cin>>n>>t;
int ma = (1<<n)-1;
fori(i,0,ma)
{
char c;cin>>c;
f0[i] = f1[i] = val[i] = c-'0';
cnt[i] = __builtin_popcount(i);
}
fori(i,0,n-1)
{
fori(j,0,ma) if(getbit(j,i))
{
f0[j]+=f0[(j^(1<<i))];
}
ford(j,ma,0) if(getbit(j,i)) f1[(j^(1<<i))]+=f1[j];
}
while(t--)
{
string s;cin>>s;
int a=0,b=0,c=0;
fori(i,0,s.size()-1)
{
if(s[i]=='0') a = batbit(a,s.size()-i-1);
if(s[i]=='1') b = batbit(b,s.size()-i-1);
if(s[i]=='?') c = batbit(c,s.size()-i-1);
}
int kq = 0;
if(cnt[a]<=n/3)
{
for(int sub=a;;sub=(sub-1)&a)
{
if(cnt[sub]%2) kq-=f1[(sub|b)];//tru di cac th nhieu hon 1 bit la 0
else kq+=f1[(sub|b)];//cong lai vi tru nhieu qua
if(!sub) break;
}
cout<<kq<<"\n";
}
else if(cnt[b]<=n/3)
{
for(int sub=b;;sub=(sub-1)&b)
{
if(cnt[(sub^b)]%2) kq-=f0[(sub|c)];//tuong tu bai vim war
else kq+=f0[(sub|c)];
if(!sub) break;
}
cout<<kq<<"\n";
}
else
{
for(int sub=c;;sub=(sub-1)&c)
{
kq+=val[(sub|b)];
if(!sub) break;
}
cout<<kq<<"\n";
}
}
}
//https://oj.uz/problem/view/JOI18_snake_escaping
//https://github.com/hharryyf/codeforcepractice
//https://codeforces.com/topic/91973/en12
//https://codeforces.com/blog/entry/20284
/* Weak:
- DP (SOS,Broken profiles,Connected component) (usaco guide)
- DP + Probabilities
- Matrix Exponentiation(usaco guide)
- Bao ham loai tru(hackerearth,http://algoogle.hadrori.jp/aoj/1254/)
- Lien Thong Manh(https://codeforces.com/blog/entry/68138)
- Dijkstra
- Geometry(MPOLY,CVXPOLY,MTRIAREA)
- DP Thu tu tu dien(google)
*/
Compilation message
snake_escaping.cpp:25:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
25 | main()
| ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:7:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
7 | #define fori(x,a,b) for (int x=a;x<=b;++x)
......
50 | fori(i,0,s.size()-1)
| ~~~~~~~~~~~~~~
snake_escaping.cpp:50:9: note: in expansion of macro 'fori'
50 | fori(i,0,s.size()-1)
| ^~~~
snake_escaping.cpp:52:49: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
52 | if(s[i]=='0') a = batbit(a,s.size()-i-1);
| ~~~~~~~~~~^~
snake_escaping.cpp:19:30: note: in definition of macro 'batbit'
19 | #define batbit(x,i) (x|(1ll<<i))
| ^
snake_escaping.cpp:53:49: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
53 | if(s[i]=='1') b = batbit(b,s.size()-i-1);
| ~~~~~~~~~~^~
snake_escaping.cpp:19:30: note: in definition of macro 'batbit'
19 | #define batbit(x,i) (x|(1ll<<i))
| ^
snake_escaping.cpp:54:49: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
54 | if(s[i]=='?') c = batbit(c,s.size()-i-1);
| ~~~~~~~~~~^~
snake_escaping.cpp:19:30: note: in definition of macro 'batbit'
19 | #define batbit(x,i) (x|(1ll<<i))
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
251 ms |
15016 KB |
Output is correct |
12 |
Correct |
255 ms |
14724 KB |
Output is correct |
13 |
Correct |
271 ms |
13964 KB |
Output is correct |
14 |
Correct |
282 ms |
14100 KB |
Output is correct |
15 |
Correct |
261 ms |
15136 KB |
Output is correct |
16 |
Correct |
281 ms |
14188 KB |
Output is correct |
17 |
Correct |
289 ms |
14280 KB |
Output is correct |
18 |
Correct |
244 ms |
16108 KB |
Output is correct |
19 |
Correct |
250 ms |
13068 KB |
Output is correct |
20 |
Correct |
259 ms |
14712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
251 ms |
15016 KB |
Output is correct |
12 |
Correct |
255 ms |
14724 KB |
Output is correct |
13 |
Correct |
271 ms |
13964 KB |
Output is correct |
14 |
Correct |
282 ms |
14100 KB |
Output is correct |
15 |
Correct |
261 ms |
15136 KB |
Output is correct |
16 |
Correct |
281 ms |
14188 KB |
Output is correct |
17 |
Correct |
289 ms |
14280 KB |
Output is correct |
18 |
Correct |
244 ms |
16108 KB |
Output is correct |
19 |
Correct |
250 ms |
13068 KB |
Output is correct |
20 |
Correct |
259 ms |
14712 KB |
Output is correct |
21 |
Correct |
337 ms |
18112 KB |
Output is correct |
22 |
Correct |
281 ms |
18376 KB |
Output is correct |
23 |
Correct |
348 ms |
17320 KB |
Output is correct |
24 |
Correct |
372 ms |
17376 KB |
Output is correct |
25 |
Correct |
312 ms |
19108 KB |
Output is correct |
26 |
Correct |
367 ms |
17696 KB |
Output is correct |
27 |
Correct |
368 ms |
17580 KB |
Output is correct |
28 |
Correct |
240 ms |
20164 KB |
Output is correct |
29 |
Correct |
275 ms |
16148 KB |
Output is correct |
30 |
Correct |
276 ms |
18376 KB |
Output is correct |
31 |
Correct |
308 ms |
18116 KB |
Output is correct |
32 |
Correct |
350 ms |
18136 KB |
Output is correct |
33 |
Correct |
275 ms |
16964 KB |
Output is correct |
34 |
Correct |
321 ms |
17192 KB |
Output is correct |
35 |
Correct |
369 ms |
17640 KB |
Output is correct |
36 |
Correct |
211 ms |
16148 KB |
Output is correct |
37 |
Correct |
258 ms |
18180 KB |
Output is correct |
38 |
Correct |
332 ms |
16264 KB |
Output is correct |
39 |
Correct |
326 ms |
17264 KB |
Output is correct |
40 |
Correct |
324 ms |
17092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
101 ms |
19000 KB |
Output is correct |
12 |
Correct |
108 ms |
19008 KB |
Output is correct |
13 |
Correct |
109 ms |
18884 KB |
Output is correct |
14 |
Correct |
142 ms |
19024 KB |
Output is correct |
15 |
Correct |
98 ms |
19004 KB |
Output is correct |
16 |
Correct |
123 ms |
18880 KB |
Output is correct |
17 |
Correct |
122 ms |
18908 KB |
Output is correct |
18 |
Correct |
95 ms |
19144 KB |
Output is correct |
19 |
Correct |
97 ms |
18776 KB |
Output is correct |
20 |
Correct |
91 ms |
19004 KB |
Output is correct |
21 |
Correct |
109 ms |
19008 KB |
Output is correct |
22 |
Correct |
123 ms |
18920 KB |
Output is correct |
23 |
Correct |
125 ms |
18928 KB |
Output is correct |
24 |
Correct |
154 ms |
18932 KB |
Output is correct |
25 |
Correct |
125 ms |
18912 KB |
Output is correct |
26 |
Correct |
84 ms |
18824 KB |
Output is correct |
27 |
Correct |
102 ms |
18988 KB |
Output is correct |
28 |
Correct |
98 ms |
18756 KB |
Output is correct |
29 |
Correct |
102 ms |
18916 KB |
Output is correct |
30 |
Correct |
110 ms |
18884 KB |
Output is correct |
31 |
Correct |
100 ms |
18884 KB |
Output is correct |
32 |
Correct |
130 ms |
18984 KB |
Output is correct |
33 |
Correct |
121 ms |
18912 KB |
Output is correct |
34 |
Correct |
101 ms |
18756 KB |
Output is correct |
35 |
Correct |
112 ms |
18888 KB |
Output is correct |
36 |
Correct |
119 ms |
18936 KB |
Output is correct |
37 |
Correct |
117 ms |
18968 KB |
Output is correct |
38 |
Correct |
115 ms |
18856 KB |
Output is correct |
39 |
Correct |
112 ms |
18904 KB |
Output is correct |
40 |
Correct |
116 ms |
18908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
251 ms |
15016 KB |
Output is correct |
12 |
Correct |
255 ms |
14724 KB |
Output is correct |
13 |
Correct |
271 ms |
13964 KB |
Output is correct |
14 |
Correct |
282 ms |
14100 KB |
Output is correct |
15 |
Correct |
261 ms |
15136 KB |
Output is correct |
16 |
Correct |
281 ms |
14188 KB |
Output is correct |
17 |
Correct |
289 ms |
14280 KB |
Output is correct |
18 |
Correct |
244 ms |
16108 KB |
Output is correct |
19 |
Correct |
250 ms |
13068 KB |
Output is correct |
20 |
Correct |
259 ms |
14712 KB |
Output is correct |
21 |
Correct |
337 ms |
18112 KB |
Output is correct |
22 |
Correct |
281 ms |
18376 KB |
Output is correct |
23 |
Correct |
348 ms |
17320 KB |
Output is correct |
24 |
Correct |
372 ms |
17376 KB |
Output is correct |
25 |
Correct |
312 ms |
19108 KB |
Output is correct |
26 |
Correct |
367 ms |
17696 KB |
Output is correct |
27 |
Correct |
368 ms |
17580 KB |
Output is correct |
28 |
Correct |
240 ms |
20164 KB |
Output is correct |
29 |
Correct |
275 ms |
16148 KB |
Output is correct |
30 |
Correct |
276 ms |
18376 KB |
Output is correct |
31 |
Correct |
308 ms |
18116 KB |
Output is correct |
32 |
Correct |
350 ms |
18136 KB |
Output is correct |
33 |
Correct |
275 ms |
16964 KB |
Output is correct |
34 |
Correct |
321 ms |
17192 KB |
Output is correct |
35 |
Correct |
369 ms |
17640 KB |
Output is correct |
36 |
Correct |
211 ms |
16148 KB |
Output is correct |
37 |
Correct |
258 ms |
18180 KB |
Output is correct |
38 |
Correct |
332 ms |
16264 KB |
Output is correct |
39 |
Correct |
326 ms |
17264 KB |
Output is correct |
40 |
Correct |
324 ms |
17092 KB |
Output is correct |
41 |
Correct |
101 ms |
19000 KB |
Output is correct |
42 |
Correct |
108 ms |
19008 KB |
Output is correct |
43 |
Correct |
109 ms |
18884 KB |
Output is correct |
44 |
Correct |
142 ms |
19024 KB |
Output is correct |
45 |
Correct |
98 ms |
19004 KB |
Output is correct |
46 |
Correct |
123 ms |
18880 KB |
Output is correct |
47 |
Correct |
122 ms |
18908 KB |
Output is correct |
48 |
Correct |
95 ms |
19144 KB |
Output is correct |
49 |
Correct |
97 ms |
18776 KB |
Output is correct |
50 |
Correct |
91 ms |
19004 KB |
Output is correct |
51 |
Correct |
109 ms |
19008 KB |
Output is correct |
52 |
Correct |
123 ms |
18920 KB |
Output is correct |
53 |
Correct |
125 ms |
18928 KB |
Output is correct |
54 |
Correct |
154 ms |
18932 KB |
Output is correct |
55 |
Correct |
125 ms |
18912 KB |
Output is correct |
56 |
Correct |
84 ms |
18824 KB |
Output is correct |
57 |
Correct |
102 ms |
18988 KB |
Output is correct |
58 |
Correct |
98 ms |
18756 KB |
Output is correct |
59 |
Correct |
102 ms |
18916 KB |
Output is correct |
60 |
Correct |
110 ms |
18884 KB |
Output is correct |
61 |
Correct |
100 ms |
18884 KB |
Output is correct |
62 |
Correct |
130 ms |
18984 KB |
Output is correct |
63 |
Correct |
121 ms |
18912 KB |
Output is correct |
64 |
Correct |
101 ms |
18756 KB |
Output is correct |
65 |
Correct |
112 ms |
18888 KB |
Output is correct |
66 |
Correct |
119 ms |
18936 KB |
Output is correct |
67 |
Correct |
117 ms |
18968 KB |
Output is correct |
68 |
Correct |
115 ms |
18856 KB |
Output is correct |
69 |
Correct |
112 ms |
18904 KB |
Output is correct |
70 |
Correct |
116 ms |
18908 KB |
Output is correct |
71 |
Correct |
483 ms |
43308 KB |
Output is correct |
72 |
Correct |
618 ms |
43452 KB |
Output is correct |
73 |
Correct |
909 ms |
41980 KB |
Output is correct |
74 |
Correct |
1269 ms |
42276 KB |
Output is correct |
75 |
Correct |
569 ms |
44252 KB |
Output is correct |
76 |
Correct |
1111 ms |
42696 KB |
Output is correct |
77 |
Correct |
1052 ms |
42488 KB |
Output is correct |
78 |
Correct |
357 ms |
46256 KB |
Output is correct |
79 |
Correct |
541 ms |
40296 KB |
Output is correct |
80 |
Correct |
485 ms |
43376 KB |
Output is correct |
81 |
Correct |
826 ms |
43240 KB |
Output is correct |
82 |
Correct |
1196 ms |
42212 KB |
Output is correct |
83 |
Correct |
555 ms |
41460 KB |
Output is correct |
84 |
Correct |
1230 ms |
42280 KB |
Output is correct |
85 |
Correct |
997 ms |
42636 KB |
Output is correct |
86 |
Correct |
332 ms |
40252 KB |
Output is correct |
87 |
Correct |
529 ms |
43252 KB |
Output is correct |
88 |
Correct |
607 ms |
40260 KB |
Output is correct |
89 |
Correct |
794 ms |
41924 KB |
Output is correct |
90 |
Correct |
870 ms |
42284 KB |
Output is correct |
91 |
Correct |
650 ms |
41384 KB |
Output is correct |
92 |
Correct |
1307 ms |
42608 KB |
Output is correct |
93 |
Correct |
1088 ms |
42536 KB |
Output is correct |
94 |
Correct |
356 ms |
40280 KB |
Output is correct |
95 |
Correct |
936 ms |
42332 KB |
Output is correct |
96 |
Correct |
1012 ms |
42428 KB |
Output is correct |
97 |
Correct |
975 ms |
42344 KB |
Output is correct |
98 |
Correct |
1061 ms |
42336 KB |
Output is correct |
99 |
Correct |
1022 ms |
42428 KB |
Output is correct |
100 |
Correct |
1034 ms |
42384 KB |
Output is correct |