#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
int dp[1 << 20];
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, l, q;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> l >> q;
string str;
cin >> str;
for(int conf = 0; conf < (1 << l); conf++) {
dp[conf] = str[conf] - '0';
}
for(int bit = 0; bit < l; bit++) {
for(int conf = 0; conf < (1 << l); conf++) {
if(conf & (1 << bit)) {
dp[conf] += dp[conf ^ (1 << bit)];
}
}
}
unordered_map <int, int> mp;
while(q > 0) {
q--;
string cur;
cin >> cur;
reverse(cur.begin(), cur.end());
int num = 0, val = 0;
for(i = 0; i < l; i++) {
val *= 3;
if(cur[i] == '?') {
num++;
val += 2;
}
else {
val += cur[i] - '0';
}
}
if(mp[val] > 0) {
cout << mp[val] - 1 << "\n";
continue;
}
vector <int> bit;
if(num <= 10) {
int ans = 0, cnf = 0;
for(i = 0; i < l; i++) {
if(cur[i] == '?') {
bit.push_back(i);
}
if(cur[i] == '1') {
cnf ^= (1 << i);
}
}
int sz = bit.size();
for(int conf = 0; conf < (1 << sz); conf++) {
int aux = cnf;
for(i = 0; i < sz; i++) {
if(conf & (1 << i)) {
aux += (1 << bit[i]);
}
}
ans += str[aux] - '0';
}
mp[val] = ans + 1;
cout << ans << "\n";
}
else {
ll ans = 0;
int cnf = 0;
for(i = 0; i < l; i++) {
if(cur[i] == '1') {
bit.push_back(i);
}
if(cur[i] != '0') {
cnf ^= (1 << i);
}
}
int sz = bit.size();
for(int conf = 0; conf < (1 << sz); conf++) {
int aux = cnf, cnt = 0;
for(i = 0; i < sz; i++) {
if(conf & (1 << i)) {
aux ^= (1 << bit[i]);
cnt++;
}
}
if(cnt & 1) {
ans -= dp[aux];
}
else {
ans += dp[aux];
}
}
mp[val] = ans + 1;
cout << ans << "\n";
}
}
//cin.close();
//cout.close();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
356 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
356 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
227 ms |
4972 KB |
Output is correct |
12 |
Correct |
319 ms |
4732 KB |
Output is correct |
13 |
Correct |
300 ms |
4360 KB |
Output is correct |
14 |
Correct |
267 ms |
4216 KB |
Output is correct |
15 |
Correct |
317 ms |
5240 KB |
Output is correct |
16 |
Correct |
460 ms |
4984 KB |
Output is correct |
17 |
Correct |
513 ms |
7040 KB |
Output is correct |
18 |
Correct |
224 ms |
5856 KB |
Output is correct |
19 |
Correct |
269 ms |
3000 KB |
Output is correct |
20 |
Correct |
289 ms |
4628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
356 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
227 ms |
4972 KB |
Output is correct |
12 |
Correct |
319 ms |
4732 KB |
Output is correct |
13 |
Correct |
300 ms |
4360 KB |
Output is correct |
14 |
Correct |
267 ms |
4216 KB |
Output is correct |
15 |
Correct |
317 ms |
5240 KB |
Output is correct |
16 |
Correct |
460 ms |
4984 KB |
Output is correct |
17 |
Correct |
513 ms |
7040 KB |
Output is correct |
18 |
Correct |
224 ms |
5856 KB |
Output is correct |
19 |
Correct |
269 ms |
3000 KB |
Output is correct |
20 |
Correct |
289 ms |
4628 KB |
Output is correct |
21 |
Correct |
276 ms |
4864 KB |
Output is correct |
22 |
Correct |
443 ms |
5360 KB |
Output is correct |
23 |
Correct |
883 ms |
25916 KB |
Output is correct |
24 |
Correct |
663 ms |
20840 KB |
Output is correct |
25 |
Correct |
840 ms |
20860 KB |
Output is correct |
26 |
Correct |
1214 ms |
31920 KB |
Output is correct |
27 |
Correct |
1971 ms |
47980 KB |
Output is correct |
28 |
Correct |
246 ms |
20152 KB |
Output is correct |
29 |
Correct |
236 ms |
16204 KB |
Output is correct |
30 |
Correct |
492 ms |
18808 KB |
Output is correct |
31 |
Correct |
973 ms |
26852 KB |
Output is correct |
32 |
Correct |
638 ms |
21664 KB |
Output is correct |
33 |
Correct |
591 ms |
18972 KB |
Output is correct |
34 |
Correct |
978 ms |
31520 KB |
Output is correct |
35 |
Correct |
1873 ms |
48108 KB |
Output is correct |
36 |
Correct |
210 ms |
16120 KB |
Output is correct |
37 |
Correct |
249 ms |
18040 KB |
Output is correct |
38 |
Correct |
303 ms |
16504 KB |
Output is correct |
39 |
Correct |
790 ms |
25784 KB |
Output is correct |
40 |
Correct |
575 ms |
20740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
356 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Execution timed out |
2041 ms |
9244 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
356 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
227 ms |
4972 KB |
Output is correct |
12 |
Correct |
319 ms |
4732 KB |
Output is correct |
13 |
Correct |
300 ms |
4360 KB |
Output is correct |
14 |
Correct |
267 ms |
4216 KB |
Output is correct |
15 |
Correct |
317 ms |
5240 KB |
Output is correct |
16 |
Correct |
460 ms |
4984 KB |
Output is correct |
17 |
Correct |
513 ms |
7040 KB |
Output is correct |
18 |
Correct |
224 ms |
5856 KB |
Output is correct |
19 |
Correct |
269 ms |
3000 KB |
Output is correct |
20 |
Correct |
289 ms |
4628 KB |
Output is correct |
21 |
Correct |
276 ms |
4864 KB |
Output is correct |
22 |
Correct |
443 ms |
5360 KB |
Output is correct |
23 |
Correct |
883 ms |
25916 KB |
Output is correct |
24 |
Correct |
663 ms |
20840 KB |
Output is correct |
25 |
Correct |
840 ms |
20860 KB |
Output is correct |
26 |
Correct |
1214 ms |
31920 KB |
Output is correct |
27 |
Correct |
1971 ms |
47980 KB |
Output is correct |
28 |
Correct |
246 ms |
20152 KB |
Output is correct |
29 |
Correct |
236 ms |
16204 KB |
Output is correct |
30 |
Correct |
492 ms |
18808 KB |
Output is correct |
31 |
Correct |
973 ms |
26852 KB |
Output is correct |
32 |
Correct |
638 ms |
21664 KB |
Output is correct |
33 |
Correct |
591 ms |
18972 KB |
Output is correct |
34 |
Correct |
978 ms |
31520 KB |
Output is correct |
35 |
Correct |
1873 ms |
48108 KB |
Output is correct |
36 |
Correct |
210 ms |
16120 KB |
Output is correct |
37 |
Correct |
249 ms |
18040 KB |
Output is correct |
38 |
Correct |
303 ms |
16504 KB |
Output is correct |
39 |
Correct |
790 ms |
25784 KB |
Output is correct |
40 |
Correct |
575 ms |
20740 KB |
Output is correct |
41 |
Execution timed out |
2041 ms |
9244 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |