#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = (1 << 20)+20,mod = 1e9+7,inf = 1e9+10,maxm = 1e6;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k >>= 1;
}
return z;
}
int sos[N][2][2];
map<ll,int> mp;
ll pw[40];
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
int l,q;
cin >> l >> q;
pw[0] = 1;
rep(i,1,l) pw[i] = 3*pw[i-1];
string s;
cin >> s;
int n = s.size();
rep(i,0,n)
sos[i][(l&1)][1] = sos[i][(l&1)][0] = (s[i]-'0');
repr(j,l-1,0){
bool d = (j&1);
rep(i,0,n){
sos[i][d][0] = sos[i][1-d][0];
sos[i][d][1] = sos[i][1-d][1];
if (i&(1 << j))
sos[i][d][1] += sos[i-(1 << j)][!d][1];
else
sos[i][d][0] += sos[i+(1 << j)][!d][0];
}
}
while (q--){
string t;
cin >> t;
ll jam = 0;
reverse(t.begin(),t.end());
int t0 = 0,t2 = 0,t1 = 0,mask = 0;
vector<int> ve;
rep(i,0,l){
if (t[i] == '1'){
t1++;
jam += pw[i];
mask += (1 << i);
}
else if (t[i] == '0') t0++;
else{
t2++;
jam += 2*pw[i];
}
}
if (mp.find(jam) != mp.end()){
cout << mp[jam] << endl;
continue;
}
if (t2 < 7){
rep(i,0,l) if (t[i] == '?') ve.pb(i);
int y = (1 << t2);
int ans = 0;
rep(j,0,y){
int sum = mask;
rep(k,0,t2)
if (j&(1 << k))
sum += (1 << ve[k]);
ans += (s[sum]-'0');
}
cout << ans << endl;
mp[jam] = ans;
continue;
}
if (t1 <= t0){
rep(i,0,l){
if (t[i] == '1') ve.pb(i);
if (t[i] == '?') mask += (1 << i);
}
int y = (1 << t1),ans = 0;
rep(i,0,y){
int cnt = 0,sum = mask;
rep(j,0,t1){
if ((1 << j)&i){
sum -= (1 << ve[j]);
cnt ^= 1;
}
}
if (cnt) ans -= sos[sum][0][1];
else ans += sos[sum][0][1];
}
cout << ans << endl;
mp[jam] = ans;
continue;
}
rep(i,0,l)
if (t[i] == '0')
ve.pb(i);
int y = (1 << t0),ans = 0;
rep(i,0,y){
int cnt = 0,sum = mask;
rep(j,0,t0)
if ((1 << j)&i){
sum += (1 << ve[j]);
cnt ^= 1;
}
if (cnt) ans -= sos[sum][0][0];
else ans += sos[sum][0][0];
}
cout << ans << endl;
mp[jam] = ans;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
372 KB |
Output is correct |
11 |
Correct |
232 ms |
4848 KB |
Output is correct |
12 |
Correct |
335 ms |
4768 KB |
Output is correct |
13 |
Correct |
429 ms |
4516 KB |
Output is correct |
14 |
Correct |
369 ms |
4192 KB |
Output is correct |
15 |
Correct |
347 ms |
5156 KB |
Output is correct |
16 |
Correct |
464 ms |
5244 KB |
Output is correct |
17 |
Correct |
676 ms |
7704 KB |
Output is correct |
18 |
Correct |
163 ms |
5912 KB |
Output is correct |
19 |
Correct |
231 ms |
2952 KB |
Output is correct |
20 |
Correct |
284 ms |
4644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
372 KB |
Output is correct |
11 |
Correct |
232 ms |
4848 KB |
Output is correct |
12 |
Correct |
335 ms |
4768 KB |
Output is correct |
13 |
Correct |
429 ms |
4516 KB |
Output is correct |
14 |
Correct |
369 ms |
4192 KB |
Output is correct |
15 |
Correct |
347 ms |
5156 KB |
Output is correct |
16 |
Correct |
464 ms |
5244 KB |
Output is correct |
17 |
Correct |
676 ms |
7704 KB |
Output is correct |
18 |
Correct |
163 ms |
5912 KB |
Output is correct |
19 |
Correct |
231 ms |
2952 KB |
Output is correct |
20 |
Correct |
284 ms |
4644 KB |
Output is correct |
21 |
Correct |
330 ms |
5104 KB |
Output is correct |
22 |
Correct |
457 ms |
5724 KB |
Output is correct |
23 |
Correct |
945 ms |
17884 KB |
Output is correct |
24 |
Correct |
630 ms |
9852 KB |
Output is correct |
25 |
Correct |
499 ms |
8572 KB |
Output is correct |
26 |
Correct |
1166 ms |
25940 KB |
Output is correct |
27 |
Correct |
1652 ms |
51172 KB |
Output is correct |
28 |
Correct |
186 ms |
7036 KB |
Output is correct |
29 |
Correct |
344 ms |
3204 KB |
Output is correct |
30 |
Correct |
438 ms |
5708 KB |
Output is correct |
31 |
Correct |
1079 ms |
18820 KB |
Output is correct |
32 |
Correct |
664 ms |
10652 KB |
Output is correct |
33 |
Correct |
557 ms |
6452 KB |
Output is correct |
34 |
Correct |
1171 ms |
25608 KB |
Output is correct |
35 |
Correct |
1556 ms |
51208 KB |
Output is correct |
36 |
Correct |
221 ms |
2904 KB |
Output is correct |
37 |
Correct |
324 ms |
5096 KB |
Output is correct |
38 |
Correct |
399 ms |
3352 KB |
Output is correct |
39 |
Correct |
1057 ms |
17664 KB |
Output is correct |
40 |
Correct |
641 ms |
9784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
372 KB |
Output is correct |
11 |
Correct |
137 ms |
21092 KB |
Output is correct |
12 |
Correct |
112 ms |
21508 KB |
Output is correct |
13 |
Correct |
117 ms |
21596 KB |
Output is correct |
14 |
Correct |
198 ms |
21512 KB |
Output is correct |
15 |
Correct |
113 ms |
21572 KB |
Output is correct |
16 |
Correct |
179 ms |
21600 KB |
Output is correct |
17 |
Correct |
140 ms |
21476 KB |
Output is correct |
18 |
Correct |
67 ms |
18660 KB |
Output is correct |
19 |
Correct |
101 ms |
21196 KB |
Output is correct |
20 |
Correct |
129 ms |
21492 KB |
Output is correct |
21 |
Correct |
131 ms |
21564 KB |
Output is correct |
22 |
Correct |
237 ms |
21484 KB |
Output is correct |
23 |
Correct |
123 ms |
21484 KB |
Output is correct |
24 |
Correct |
138 ms |
21488 KB |
Output is correct |
25 |
Correct |
148 ms |
21476 KB |
Output is correct |
26 |
Correct |
68 ms |
18464 KB |
Output is correct |
27 |
Correct |
104 ms |
21216 KB |
Output is correct |
28 |
Correct |
112 ms |
21348 KB |
Output is correct |
29 |
Correct |
126 ms |
21564 KB |
Output is correct |
30 |
Correct |
141 ms |
21604 KB |
Output is correct |
31 |
Correct |
107 ms |
21476 KB |
Output is correct |
32 |
Correct |
178 ms |
21500 KB |
Output is correct |
33 |
Correct |
155 ms |
21580 KB |
Output is correct |
34 |
Correct |
66 ms |
18524 KB |
Output is correct |
35 |
Correct |
123 ms |
21180 KB |
Output is correct |
36 |
Correct |
164 ms |
21192 KB |
Output is correct |
37 |
Correct |
138 ms |
21092 KB |
Output is correct |
38 |
Correct |
129 ms |
21128 KB |
Output is correct |
39 |
Correct |
178 ms |
21096 KB |
Output is correct |
40 |
Correct |
132 ms |
21144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
372 KB |
Output is correct |
11 |
Correct |
232 ms |
4848 KB |
Output is correct |
12 |
Correct |
335 ms |
4768 KB |
Output is correct |
13 |
Correct |
429 ms |
4516 KB |
Output is correct |
14 |
Correct |
369 ms |
4192 KB |
Output is correct |
15 |
Correct |
347 ms |
5156 KB |
Output is correct |
16 |
Correct |
464 ms |
5244 KB |
Output is correct |
17 |
Correct |
676 ms |
7704 KB |
Output is correct |
18 |
Correct |
163 ms |
5912 KB |
Output is correct |
19 |
Correct |
231 ms |
2952 KB |
Output is correct |
20 |
Correct |
284 ms |
4644 KB |
Output is correct |
21 |
Correct |
330 ms |
5104 KB |
Output is correct |
22 |
Correct |
457 ms |
5724 KB |
Output is correct |
23 |
Correct |
945 ms |
17884 KB |
Output is correct |
24 |
Correct |
630 ms |
9852 KB |
Output is correct |
25 |
Correct |
499 ms |
8572 KB |
Output is correct |
26 |
Correct |
1166 ms |
25940 KB |
Output is correct |
27 |
Correct |
1652 ms |
51172 KB |
Output is correct |
28 |
Correct |
186 ms |
7036 KB |
Output is correct |
29 |
Correct |
344 ms |
3204 KB |
Output is correct |
30 |
Correct |
438 ms |
5708 KB |
Output is correct |
31 |
Correct |
1079 ms |
18820 KB |
Output is correct |
32 |
Correct |
664 ms |
10652 KB |
Output is correct |
33 |
Correct |
557 ms |
6452 KB |
Output is correct |
34 |
Correct |
1171 ms |
25608 KB |
Output is correct |
35 |
Correct |
1556 ms |
51208 KB |
Output is correct |
36 |
Correct |
221 ms |
2904 KB |
Output is correct |
37 |
Correct |
324 ms |
5096 KB |
Output is correct |
38 |
Correct |
399 ms |
3352 KB |
Output is correct |
39 |
Correct |
1057 ms |
17664 KB |
Output is correct |
40 |
Correct |
641 ms |
9784 KB |
Output is correct |
41 |
Correct |
137 ms |
21092 KB |
Output is correct |
42 |
Correct |
112 ms |
21508 KB |
Output is correct |
43 |
Correct |
117 ms |
21596 KB |
Output is correct |
44 |
Correct |
198 ms |
21512 KB |
Output is correct |
45 |
Correct |
113 ms |
21572 KB |
Output is correct |
46 |
Correct |
179 ms |
21600 KB |
Output is correct |
47 |
Correct |
140 ms |
21476 KB |
Output is correct |
48 |
Correct |
67 ms |
18660 KB |
Output is correct |
49 |
Correct |
101 ms |
21196 KB |
Output is correct |
50 |
Correct |
129 ms |
21492 KB |
Output is correct |
51 |
Correct |
131 ms |
21564 KB |
Output is correct |
52 |
Correct |
237 ms |
21484 KB |
Output is correct |
53 |
Correct |
123 ms |
21484 KB |
Output is correct |
54 |
Correct |
138 ms |
21488 KB |
Output is correct |
55 |
Correct |
148 ms |
21476 KB |
Output is correct |
56 |
Correct |
68 ms |
18464 KB |
Output is correct |
57 |
Correct |
104 ms |
21216 KB |
Output is correct |
58 |
Correct |
112 ms |
21348 KB |
Output is correct |
59 |
Correct |
126 ms |
21564 KB |
Output is correct |
60 |
Correct |
141 ms |
21604 KB |
Output is correct |
61 |
Correct |
107 ms |
21476 KB |
Output is correct |
62 |
Correct |
178 ms |
21500 KB |
Output is correct |
63 |
Correct |
155 ms |
21580 KB |
Output is correct |
64 |
Correct |
66 ms |
18524 KB |
Output is correct |
65 |
Correct |
123 ms |
21180 KB |
Output is correct |
66 |
Correct |
164 ms |
21192 KB |
Output is correct |
67 |
Correct |
138 ms |
21092 KB |
Output is correct |
68 |
Correct |
129 ms |
21128 KB |
Output is correct |
69 |
Correct |
178 ms |
21096 KB |
Output is correct |
70 |
Correct |
132 ms |
21144 KB |
Output is correct |
71 |
Correct |
893 ms |
34248 KB |
Output is correct |
72 |
Correct |
1501 ms |
63424 KB |
Output is correct |
73 |
Runtime error |
1565 ms |
65540 KB |
Execution killed with signal 9 |
74 |
Halted |
0 ms |
0 KB |
- |