#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
// #ifndef ONLINE_JUDGE
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
// #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 5e5 + 5, lg = 20, inf = ll(1e18) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int sml[1<<lg], lrg[1<<lg];
int main(){ IOS();
int n, q; cin >> n >> q;
string s; cin >> s;
rep(i,0,sz(s)) sml[i] = lrg[i] = s[i] - '0';
rep(i,0,n){
rep(j,0,1<<n){
if(j>>i&1){
sml[j] += sml[j^(1<<i)];
}else{
lrg[j] += lrg[j^(1<<i)];
}
}
}
while(q--){
string t; cin >> t;
vector<int> a[3];
rep(i,0,n){
if(t[n-1-i] == '?') a[2].pb(i);
else a[t[n-1-i]-'0'].pb(i);
}
int ans = 0;
if(sz(a[0]) <= n/3){
int vl = 0;
for(char c: t){
vl <<= 1;
if(c == '1') vl |= 1;
}
rep(i,0,1<<sz(a[0])){
int bt = vl;
int ptr = 0;
bool f = false;
for(int p: a[0]){
if(i>>ptr&1) bt ^= 1<<p, f ^= 1;
ptr++;
}
ans += (f?-1:1)*lrg[bt];
}
}else if(sz(a[1]) <= n/3){
int vl = 0;
for(char c: t){
vl <<= 1;
if(c != '0') vl |= 1;
}
rep(i,0,1<<sz(a[1])){
int bt = vl;
int ptr = 0;
bool f = false;
for(int p: a[1]){
if(i>>ptr&1) bt ^= 1<<p, f ^= 1;
ptr++;
}
ans += (f?-1:1)*sml[bt];
}
}else{
int vl = 0;
for(char c: t){
vl <<= 1;
if(c == '1') vl |= 1;
}
rep(i,0,1<<sz(a[2])){
int bt = vl;
int ptr = 0;
for(int p: a[2]){
if(i>>ptr&1) bt ^= 1<<p;
ptr++;
}
ans += s[bt]-'0';
}
}
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
11 |
Correct |
492 ms |
15076 KB |
Output is correct |
12 |
Correct |
469 ms |
14776 KB |
Output is correct |
13 |
Correct |
519 ms |
14028 KB |
Output is correct |
14 |
Correct |
547 ms |
14004 KB |
Output is correct |
15 |
Correct |
478 ms |
15064 KB |
Output is correct |
16 |
Correct |
550 ms |
14368 KB |
Output is correct |
17 |
Correct |
521 ms |
14180 KB |
Output is correct |
18 |
Correct |
306 ms |
16076 KB |
Output is correct |
19 |
Correct |
435 ms |
12988 KB |
Output is correct |
20 |
Correct |
462 ms |
14872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
11 |
Correct |
492 ms |
15076 KB |
Output is correct |
12 |
Correct |
469 ms |
14776 KB |
Output is correct |
13 |
Correct |
519 ms |
14028 KB |
Output is correct |
14 |
Correct |
547 ms |
14004 KB |
Output is correct |
15 |
Correct |
478 ms |
15064 KB |
Output is correct |
16 |
Correct |
550 ms |
14368 KB |
Output is correct |
17 |
Correct |
521 ms |
14180 KB |
Output is correct |
18 |
Correct |
306 ms |
16076 KB |
Output is correct |
19 |
Correct |
435 ms |
12988 KB |
Output is correct |
20 |
Correct |
462 ms |
14872 KB |
Output is correct |
21 |
Correct |
506 ms |
18176 KB |
Output is correct |
22 |
Correct |
533 ms |
18388 KB |
Output is correct |
23 |
Correct |
631 ms |
17252 KB |
Output is correct |
24 |
Correct |
684 ms |
17056 KB |
Output is correct |
25 |
Correct |
604 ms |
19036 KB |
Output is correct |
26 |
Correct |
667 ms |
17652 KB |
Output is correct |
27 |
Correct |
672 ms |
17644 KB |
Output is correct |
28 |
Correct |
340 ms |
20180 KB |
Output is correct |
29 |
Correct |
495 ms |
16292 KB |
Output is correct |
30 |
Correct |
527 ms |
18340 KB |
Output is correct |
31 |
Correct |
635 ms |
18168 KB |
Output is correct |
32 |
Correct |
668 ms |
18172 KB |
Output is correct |
33 |
Correct |
587 ms |
17060 KB |
Output is correct |
34 |
Correct |
649 ms |
17120 KB |
Output is correct |
35 |
Correct |
643 ms |
17488 KB |
Output is correct |
36 |
Correct |
350 ms |
16020 KB |
Output is correct |
37 |
Correct |
513 ms |
18124 KB |
Output is correct |
38 |
Correct |
516 ms |
16128 KB |
Output is correct |
39 |
Correct |
642 ms |
17348 KB |
Output is correct |
40 |
Correct |
663 ms |
17312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
11 |
Correct |
66 ms |
11988 KB |
Output is correct |
12 |
Correct |
69 ms |
11968 KB |
Output is correct |
13 |
Correct |
83 ms |
11840 KB |
Output is correct |
14 |
Correct |
122 ms |
11868 KB |
Output is correct |
15 |
Correct |
71 ms |
11968 KB |
Output is correct |
16 |
Correct |
102 ms |
11908 KB |
Output is correct |
17 |
Correct |
101 ms |
11884 KB |
Output is correct |
18 |
Correct |
57 ms |
12012 KB |
Output is correct |
19 |
Correct |
69 ms |
11756 KB |
Output is correct |
20 |
Correct |
67 ms |
11884 KB |
Output is correct |
21 |
Correct |
91 ms |
11972 KB |
Output is correct |
22 |
Correct |
101 ms |
11852 KB |
Output is correct |
23 |
Correct |
75 ms |
11872 KB |
Output is correct |
24 |
Correct |
110 ms |
11816 KB |
Output is correct |
25 |
Correct |
100 ms |
11840 KB |
Output is correct |
26 |
Correct |
52 ms |
11756 KB |
Output is correct |
27 |
Correct |
65 ms |
11964 KB |
Output is correct |
28 |
Correct |
76 ms |
11840 KB |
Output is correct |
29 |
Correct |
86 ms |
11824 KB |
Output is correct |
30 |
Correct |
102 ms |
11824 KB |
Output is correct |
31 |
Correct |
72 ms |
11824 KB |
Output is correct |
32 |
Correct |
111 ms |
11992 KB |
Output is correct |
33 |
Correct |
106 ms |
11848 KB |
Output is correct |
34 |
Correct |
51 ms |
11808 KB |
Output is correct |
35 |
Correct |
86 ms |
11928 KB |
Output is correct |
36 |
Correct |
95 ms |
11852 KB |
Output is correct |
37 |
Correct |
105 ms |
11940 KB |
Output is correct |
38 |
Correct |
89 ms |
11892 KB |
Output is correct |
39 |
Correct |
92 ms |
11820 KB |
Output is correct |
40 |
Correct |
95 ms |
11868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
11 |
Correct |
492 ms |
15076 KB |
Output is correct |
12 |
Correct |
469 ms |
14776 KB |
Output is correct |
13 |
Correct |
519 ms |
14028 KB |
Output is correct |
14 |
Correct |
547 ms |
14004 KB |
Output is correct |
15 |
Correct |
478 ms |
15064 KB |
Output is correct |
16 |
Correct |
550 ms |
14368 KB |
Output is correct |
17 |
Correct |
521 ms |
14180 KB |
Output is correct |
18 |
Correct |
306 ms |
16076 KB |
Output is correct |
19 |
Correct |
435 ms |
12988 KB |
Output is correct |
20 |
Correct |
462 ms |
14872 KB |
Output is correct |
21 |
Correct |
506 ms |
18176 KB |
Output is correct |
22 |
Correct |
533 ms |
18388 KB |
Output is correct |
23 |
Correct |
631 ms |
17252 KB |
Output is correct |
24 |
Correct |
684 ms |
17056 KB |
Output is correct |
25 |
Correct |
604 ms |
19036 KB |
Output is correct |
26 |
Correct |
667 ms |
17652 KB |
Output is correct |
27 |
Correct |
672 ms |
17644 KB |
Output is correct |
28 |
Correct |
340 ms |
20180 KB |
Output is correct |
29 |
Correct |
495 ms |
16292 KB |
Output is correct |
30 |
Correct |
527 ms |
18340 KB |
Output is correct |
31 |
Correct |
635 ms |
18168 KB |
Output is correct |
32 |
Correct |
668 ms |
18172 KB |
Output is correct |
33 |
Correct |
587 ms |
17060 KB |
Output is correct |
34 |
Correct |
649 ms |
17120 KB |
Output is correct |
35 |
Correct |
643 ms |
17488 KB |
Output is correct |
36 |
Correct |
350 ms |
16020 KB |
Output is correct |
37 |
Correct |
513 ms |
18124 KB |
Output is correct |
38 |
Correct |
516 ms |
16128 KB |
Output is correct |
39 |
Correct |
642 ms |
17348 KB |
Output is correct |
40 |
Correct |
663 ms |
17312 KB |
Output is correct |
41 |
Correct |
66 ms |
11988 KB |
Output is correct |
42 |
Correct |
69 ms |
11968 KB |
Output is correct |
43 |
Correct |
83 ms |
11840 KB |
Output is correct |
44 |
Correct |
122 ms |
11868 KB |
Output is correct |
45 |
Correct |
71 ms |
11968 KB |
Output is correct |
46 |
Correct |
102 ms |
11908 KB |
Output is correct |
47 |
Correct |
101 ms |
11884 KB |
Output is correct |
48 |
Correct |
57 ms |
12012 KB |
Output is correct |
49 |
Correct |
69 ms |
11756 KB |
Output is correct |
50 |
Correct |
67 ms |
11884 KB |
Output is correct |
51 |
Correct |
91 ms |
11972 KB |
Output is correct |
52 |
Correct |
101 ms |
11852 KB |
Output is correct |
53 |
Correct |
75 ms |
11872 KB |
Output is correct |
54 |
Correct |
110 ms |
11816 KB |
Output is correct |
55 |
Correct |
100 ms |
11840 KB |
Output is correct |
56 |
Correct |
52 ms |
11756 KB |
Output is correct |
57 |
Correct |
65 ms |
11964 KB |
Output is correct |
58 |
Correct |
76 ms |
11840 KB |
Output is correct |
59 |
Correct |
86 ms |
11824 KB |
Output is correct |
60 |
Correct |
102 ms |
11824 KB |
Output is correct |
61 |
Correct |
72 ms |
11824 KB |
Output is correct |
62 |
Correct |
111 ms |
11992 KB |
Output is correct |
63 |
Correct |
106 ms |
11848 KB |
Output is correct |
64 |
Correct |
51 ms |
11808 KB |
Output is correct |
65 |
Correct |
86 ms |
11928 KB |
Output is correct |
66 |
Correct |
95 ms |
11852 KB |
Output is correct |
67 |
Correct |
105 ms |
11940 KB |
Output is correct |
68 |
Correct |
89 ms |
11892 KB |
Output is correct |
69 |
Correct |
92 ms |
11820 KB |
Output is correct |
70 |
Correct |
95 ms |
11868 KB |
Output is correct |
71 |
Correct |
771 ms |
36264 KB |
Output is correct |
72 |
Correct |
806 ms |
36344 KB |
Output is correct |
73 |
Correct |
1157 ms |
34988 KB |
Output is correct |
74 |
Correct |
1570 ms |
35308 KB |
Output is correct |
75 |
Correct |
884 ms |
37224 KB |
Output is correct |
76 |
Correct |
1553 ms |
35588 KB |
Output is correct |
77 |
Correct |
1528 ms |
35532 KB |
Output is correct |
78 |
Correct |
436 ms |
39196 KB |
Output is correct |
79 |
Correct |
677 ms |
33228 KB |
Output is correct |
80 |
Correct |
787 ms |
36336 KB |
Output is correct |
81 |
Correct |
1106 ms |
36392 KB |
Output is correct |
82 |
Correct |
1470 ms |
35248 KB |
Output is correct |
83 |
Correct |
936 ms |
34524 KB |
Output is correct |
84 |
Correct |
1597 ms |
35260 KB |
Output is correct |
85 |
Correct |
1481 ms |
35704 KB |
Output is correct |
86 |
Correct |
524 ms |
33316 KB |
Output is correct |
87 |
Correct |
739 ms |
36348 KB |
Output is correct |
88 |
Correct |
749 ms |
33284 KB |
Output is correct |
89 |
Correct |
1093 ms |
35032 KB |
Output is correct |
90 |
Correct |
1380 ms |
35260 KB |
Output is correct |
91 |
Correct |
920 ms |
34464 KB |
Output is correct |
92 |
Correct |
1728 ms |
35556 KB |
Output is correct |
93 |
Correct |
1482 ms |
35396 KB |
Output is correct |
94 |
Correct |
536 ms |
33236 KB |
Output is correct |
95 |
Correct |
1336 ms |
35468 KB |
Output is correct |
96 |
Correct |
1249 ms |
35408 KB |
Output is correct |
97 |
Correct |
1269 ms |
35476 KB |
Output is correct |
98 |
Correct |
1240 ms |
35392 KB |
Output is correct |
99 |
Correct |
1261 ms |
35408 KB |
Output is correct |
100 |
Correct |
1260 ms |
35404 KB |
Output is correct |