#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcount((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (20)
int L, Q;
string S;
int sum0[1<<MAXN], sum1[1<<MAXN], num[1<<MAXN];
string gug(ll x, ll place) {
-- place;
assert(x <= (1<<(place+1))-1);
string res="";
for(ll i=place; i>=0; --i) if((1<<i)&x) res+='1'; else res+='0';
return res;
}
int main()
{
FAST
cin>>L>>Q;
cin>>S;
FOR(i,0,(1<<L)-1) {
sum0[(~i)&((1<<L)-1)] += S[i]-'0';
sum1[i] += S[i]-'0';
num[i]=btinpct(i);
}
FOR(j,0,L-1) {
FOR(i,0,(1<<L)-1) {
if( ((1<<j)&i) == 0 ) sum0[i] += sum0[i^(1<<j)], sum1[i] += sum1[i^(1<<j)];
}
}
FOR(ii,0,Q-1) {
string Q; cin>>Q;
reverse(all(Q)); assert(Q.size()==L);
int a=0, b=0, c=0, na=0, nb=0, nc=0;
FOR(i,0,L-1) {
if(Q[i] == '0') a += 1<<i, ++na;
if(Q[i] == '1') b += 1<<i, ++nb;
if(Q[i] == '?') c += 1<<i, ++nc;
}
int ans=0;
if((na <= nb && na <= nc)) { // a
ans += sum1[b]; // a will not clash bit with b, cos in Q[pos] only can be 1 character lol
for(int j=a; j>0; j=(j-1)&a) { assert((j&b)==0);
if(num[j] & 1) ans -= sum1[j|b];
else ans += sum1[j|b];
}
} else if(nb <= na && nb <= nc) { // b
ans += sum0[a];
for(int j=b; j>0; j=(j-1)&b) {
if(num[j] & 1) ans -= sum0[j|a];
else ans += sum0[j|a];
}
} else { // c
for(int i=c; i>0; i=(i-1)&c) ans += S[i|b]-'0';// (i&b) == 0
ans += S[b]-'0';
}
cout<<ans<<'\n';
}
}
Compilation message
In file included from /usr/include/c++/7/cassert:44:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
from snake_escaping.cpp:1:
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:59:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
reverse(all(Q)); assert(Q.size()==L);
~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
247 ms |
15068 KB |
Output is correct |
12 |
Correct |
258 ms |
14840 KB |
Output is correct |
13 |
Correct |
290 ms |
14036 KB |
Output is correct |
14 |
Correct |
408 ms |
14172 KB |
Output is correct |
15 |
Correct |
272 ms |
15224 KB |
Output is correct |
16 |
Correct |
292 ms |
14456 KB |
Output is correct |
17 |
Correct |
297 ms |
14276 KB |
Output is correct |
18 |
Correct |
209 ms |
16168 KB |
Output is correct |
19 |
Correct |
230 ms |
13048 KB |
Output is correct |
20 |
Correct |
263 ms |
14840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
247 ms |
15068 KB |
Output is correct |
12 |
Correct |
258 ms |
14840 KB |
Output is correct |
13 |
Correct |
290 ms |
14036 KB |
Output is correct |
14 |
Correct |
408 ms |
14172 KB |
Output is correct |
15 |
Correct |
272 ms |
15224 KB |
Output is correct |
16 |
Correct |
292 ms |
14456 KB |
Output is correct |
17 |
Correct |
297 ms |
14276 KB |
Output is correct |
18 |
Correct |
209 ms |
16168 KB |
Output is correct |
19 |
Correct |
230 ms |
13048 KB |
Output is correct |
20 |
Correct |
263 ms |
14840 KB |
Output is correct |
21 |
Correct |
288 ms |
18108 KB |
Output is correct |
22 |
Correct |
310 ms |
18360 KB |
Output is correct |
23 |
Correct |
325 ms |
17320 KB |
Output is correct |
24 |
Correct |
354 ms |
17400 KB |
Output is correct |
25 |
Correct |
298 ms |
19152 KB |
Output is correct |
26 |
Correct |
347 ms |
17656 KB |
Output is correct |
27 |
Correct |
344 ms |
17828 KB |
Output is correct |
28 |
Correct |
227 ms |
20216 KB |
Output is correct |
29 |
Correct |
276 ms |
16124 KB |
Output is correct |
30 |
Correct |
305 ms |
18376 KB |
Output is correct |
31 |
Correct |
322 ms |
18204 KB |
Output is correct |
32 |
Correct |
352 ms |
18424 KB |
Output is correct |
33 |
Correct |
288 ms |
17144 KB |
Output is correct |
34 |
Correct |
339 ms |
17248 KB |
Output is correct |
35 |
Correct |
338 ms |
17628 KB |
Output is correct |
36 |
Correct |
199 ms |
16224 KB |
Output is correct |
37 |
Correct |
285 ms |
18248 KB |
Output is correct |
38 |
Correct |
275 ms |
16236 KB |
Output is correct |
39 |
Correct |
315 ms |
17356 KB |
Output is correct |
40 |
Correct |
350 ms |
17272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
75 ms |
16144 KB |
Output is correct |
12 |
Correct |
76 ms |
16152 KB |
Output is correct |
13 |
Correct |
82 ms |
16024 KB |
Output is correct |
14 |
Correct |
94 ms |
16024 KB |
Output is correct |
15 |
Correct |
76 ms |
16152 KB |
Output is correct |
16 |
Correct |
93 ms |
16024 KB |
Output is correct |
17 |
Correct |
93 ms |
16156 KB |
Output is correct |
18 |
Correct |
69 ms |
16280 KB |
Output is correct |
19 |
Correct |
74 ms |
16024 KB |
Output is correct |
20 |
Correct |
76 ms |
16148 KB |
Output is correct |
21 |
Correct |
84 ms |
16280 KB |
Output is correct |
22 |
Correct |
93 ms |
16148 KB |
Output is correct |
23 |
Correct |
76 ms |
16024 KB |
Output is correct |
24 |
Correct |
96 ms |
16124 KB |
Output is correct |
25 |
Correct |
93 ms |
16068 KB |
Output is correct |
26 |
Correct |
66 ms |
16020 KB |
Output is correct |
27 |
Correct |
75 ms |
16152 KB |
Output is correct |
28 |
Correct |
74 ms |
15972 KB |
Output is correct |
29 |
Correct |
82 ms |
16144 KB |
Output is correct |
30 |
Correct |
86 ms |
16024 KB |
Output is correct |
31 |
Correct |
82 ms |
16168 KB |
Output is correct |
32 |
Correct |
96 ms |
16024 KB |
Output is correct |
33 |
Correct |
94 ms |
16152 KB |
Output is correct |
34 |
Correct |
68 ms |
15892 KB |
Output is correct |
35 |
Correct |
87 ms |
16092 KB |
Output is correct |
36 |
Correct |
88 ms |
16152 KB |
Output is correct |
37 |
Correct |
97 ms |
16152 KB |
Output is correct |
38 |
Correct |
84 ms |
16152 KB |
Output is correct |
39 |
Correct |
84 ms |
16152 KB |
Output is correct |
40 |
Correct |
84 ms |
16072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
247 ms |
15068 KB |
Output is correct |
12 |
Correct |
258 ms |
14840 KB |
Output is correct |
13 |
Correct |
290 ms |
14036 KB |
Output is correct |
14 |
Correct |
408 ms |
14172 KB |
Output is correct |
15 |
Correct |
272 ms |
15224 KB |
Output is correct |
16 |
Correct |
292 ms |
14456 KB |
Output is correct |
17 |
Correct |
297 ms |
14276 KB |
Output is correct |
18 |
Correct |
209 ms |
16168 KB |
Output is correct |
19 |
Correct |
230 ms |
13048 KB |
Output is correct |
20 |
Correct |
263 ms |
14840 KB |
Output is correct |
21 |
Correct |
288 ms |
18108 KB |
Output is correct |
22 |
Correct |
310 ms |
18360 KB |
Output is correct |
23 |
Correct |
325 ms |
17320 KB |
Output is correct |
24 |
Correct |
354 ms |
17400 KB |
Output is correct |
25 |
Correct |
298 ms |
19152 KB |
Output is correct |
26 |
Correct |
347 ms |
17656 KB |
Output is correct |
27 |
Correct |
344 ms |
17828 KB |
Output is correct |
28 |
Correct |
227 ms |
20216 KB |
Output is correct |
29 |
Correct |
276 ms |
16124 KB |
Output is correct |
30 |
Correct |
305 ms |
18376 KB |
Output is correct |
31 |
Correct |
322 ms |
18204 KB |
Output is correct |
32 |
Correct |
352 ms |
18424 KB |
Output is correct |
33 |
Correct |
288 ms |
17144 KB |
Output is correct |
34 |
Correct |
339 ms |
17248 KB |
Output is correct |
35 |
Correct |
338 ms |
17628 KB |
Output is correct |
36 |
Correct |
199 ms |
16224 KB |
Output is correct |
37 |
Correct |
285 ms |
18248 KB |
Output is correct |
38 |
Correct |
275 ms |
16236 KB |
Output is correct |
39 |
Correct |
315 ms |
17356 KB |
Output is correct |
40 |
Correct |
350 ms |
17272 KB |
Output is correct |
41 |
Correct |
75 ms |
16144 KB |
Output is correct |
42 |
Correct |
76 ms |
16152 KB |
Output is correct |
43 |
Correct |
82 ms |
16024 KB |
Output is correct |
44 |
Correct |
94 ms |
16024 KB |
Output is correct |
45 |
Correct |
76 ms |
16152 KB |
Output is correct |
46 |
Correct |
93 ms |
16024 KB |
Output is correct |
47 |
Correct |
93 ms |
16156 KB |
Output is correct |
48 |
Correct |
69 ms |
16280 KB |
Output is correct |
49 |
Correct |
74 ms |
16024 KB |
Output is correct |
50 |
Correct |
76 ms |
16148 KB |
Output is correct |
51 |
Correct |
84 ms |
16280 KB |
Output is correct |
52 |
Correct |
93 ms |
16148 KB |
Output is correct |
53 |
Correct |
76 ms |
16024 KB |
Output is correct |
54 |
Correct |
96 ms |
16124 KB |
Output is correct |
55 |
Correct |
93 ms |
16068 KB |
Output is correct |
56 |
Correct |
66 ms |
16020 KB |
Output is correct |
57 |
Correct |
75 ms |
16152 KB |
Output is correct |
58 |
Correct |
74 ms |
15972 KB |
Output is correct |
59 |
Correct |
82 ms |
16144 KB |
Output is correct |
60 |
Correct |
86 ms |
16024 KB |
Output is correct |
61 |
Correct |
82 ms |
16168 KB |
Output is correct |
62 |
Correct |
96 ms |
16024 KB |
Output is correct |
63 |
Correct |
94 ms |
16152 KB |
Output is correct |
64 |
Correct |
68 ms |
15892 KB |
Output is correct |
65 |
Correct |
87 ms |
16092 KB |
Output is correct |
66 |
Correct |
88 ms |
16152 KB |
Output is correct |
67 |
Correct |
97 ms |
16152 KB |
Output is correct |
68 |
Correct |
84 ms |
16152 KB |
Output is correct |
69 |
Correct |
84 ms |
16152 KB |
Output is correct |
70 |
Correct |
84 ms |
16072 KB |
Output is correct |
71 |
Correct |
521 ms |
40476 KB |
Output is correct |
72 |
Correct |
540 ms |
40536 KB |
Output is correct |
73 |
Correct |
679 ms |
39128 KB |
Output is correct |
74 |
Correct |
817 ms |
39400 KB |
Output is correct |
75 |
Correct |
527 ms |
41416 KB |
Output is correct |
76 |
Correct |
925 ms |
39772 KB |
Output is correct |
77 |
Correct |
822 ms |
39760 KB |
Output is correct |
78 |
Correct |
375 ms |
43416 KB |
Output is correct |
79 |
Correct |
459 ms |
37464 KB |
Output is correct |
80 |
Correct |
573 ms |
40704 KB |
Output is correct |
81 |
Correct |
757 ms |
40392 KB |
Output is correct |
82 |
Correct |
880 ms |
39440 KB |
Output is correct |
83 |
Correct |
478 ms |
38552 KB |
Output is correct |
84 |
Correct |
847 ms |
39320 KB |
Output is correct |
85 |
Correct |
831 ms |
39624 KB |
Output is correct |
86 |
Correct |
327 ms |
37400 KB |
Output is correct |
87 |
Correct |
547 ms |
40472 KB |
Output is correct |
88 |
Correct |
526 ms |
37400 KB |
Output is correct |
89 |
Correct |
716 ms |
39112 KB |
Output is correct |
90 |
Correct |
628 ms |
39316 KB |
Output is correct |
91 |
Correct |
543 ms |
38560 KB |
Output is correct |
92 |
Correct |
976 ms |
39760 KB |
Output is correct |
93 |
Correct |
840 ms |
39632 KB |
Output is correct |
94 |
Correct |
334 ms |
37400 KB |
Output is correct |
95 |
Correct |
749 ms |
39448 KB |
Output is correct |
96 |
Correct |
795 ms |
39620 KB |
Output is correct |
97 |
Correct |
897 ms |
39576 KB |
Output is correct |
98 |
Correct |
821 ms |
39416 KB |
Output is correct |
99 |
Correct |
703 ms |
39528 KB |
Output is correct |
100 |
Correct |
726 ms |
39580 KB |
Output is correct |