#include <bits/stdc++.h>
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define pb push_back
#define sc second
#define fr first
//#define int long long
#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);}int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}//mesanu
int n, q;
string s;
int answer_C(string t, int qm)
{
int sum = 0;
for(int i = 0; i < 1<<qm; i++)
{
int now = 0;
int idx = 0;
for(int j = 0; j < n; j++)
{
if(t[j] == '1')
{
now+=1<<j;
}
if(t[j] == '?')
{
if(i & (1<<idx))
{
now+=1<<j;
}
idx++;
}
}
sum+=(s[now]-'0');
}
return sum;
}
int sub[(1<<20)+5];
int super[(1<<20)+5];
int answer_B(string t)
{
vector<int> pos;
int base = 0;
for(int i = 0; i < n; i++)
{
if(t[i] == '1')
{
pos.pb(i);
}
if(t[i] == '?')
{
base+=1<<i;
}
}
int sum = 0;
int sz = pos.size();
for(int mask = (1<<sz)-1; mask >= 0; mask--)
{
int now = base;
for(int i = 0; i < pos.size(); i++)
{
if(mask & (1<<i))
{
now+=1<<pos[i];
}
}
sum+=sub[now]*power(-1, sz-__builtin_popcount(mask), MOD);
}
return sum;
}
int answer_A(string t)
{
vector<int> pos;
int base = 0;
for(int i = 0; i < n; i++)
{
if(t[i] == '0')
{
pos.pb(i);
}
if(t[i] == '1')
{
base+=1<<i;
}
}
int sum = 0;
int sz = pos.size();
for(int mask = 0; mask < 1<<sz; mask++)
{
int now = base;
for(int i = 0; i < pos.size(); i++)
{
if(mask & (1<<i))
{
now+=1<<pos[i];
}
}
sum+=super[now]*power(-1, __builtin_popcount(mask), MOD);
}
return sum;
}
void solve()
{
cin >> n >> q;
cin >> s;
for(int i = 0; i < 1<<n; i++)
{
sub[i] = (s[i]-'0');
super[i] = (s[i]-'0');
}
for(int i = 0; i < n; i++)
{
for(int mask = 0; mask < 1<< n; mask++)
{
if(mask & 1<<i)
{
sub[mask]+=sub[mask^(1<<i)];
}
}
}
for(int i = n-1; i >= 0; i--)
{
for(int mask = (1<<n)-1; mask >= 0; mask--)
{
if(!(mask&(1<<i)))
{
super[mask]+=super[mask^(1<<i)];
}
}
}
while(q--)
{
string t;
cin >> t;
reverse(all(t));
int qm = 0, ones = 0, zeros = 0;
for(int i = 0; i < n; i++)
{
if(t[i] == '?'){qm++;}
if(t[i] == '1'){ones++;}
if(t[i] == '0'){zeros++;}
}
//cout << answer_A(t) << endl;
if(ones == min({qm, ones, zeros}))
{
cout << answer_B(t) << endl;
}
else if(qm == min({qm, ones, zeros}))
{
cout << answer_C(t, qm) << endl;
}
else
{
cout << answer_A(t) << endl;
}
}
}
int32_t main(){
startt
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}
/*
3 5
12345678
000
0??
1?0
?11
???
*/
Compilation message
snake_escaping.cpp: In function 'int answer_B(std::string)':
snake_escaping.cpp:75:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i = 0; i < pos.size(); i++)
| ~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'int answer_A(std::string)':
snake_escaping.cpp:107:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i = 0; i < pos.size(); i++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
340 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
340 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
231 ms |
4272 KB |
Output is correct |
12 |
Correct |
274 ms |
3980 KB |
Output is correct |
13 |
Correct |
422 ms |
3276 KB |
Output is correct |
14 |
Correct |
523 ms |
3272 KB |
Output is correct |
15 |
Correct |
370 ms |
4316 KB |
Output is correct |
16 |
Correct |
584 ms |
3532 KB |
Output is correct |
17 |
Correct |
483 ms |
3388 KB |
Output is correct |
18 |
Correct |
205 ms |
5240 KB |
Output is correct |
19 |
Correct |
222 ms |
2340 KB |
Output is correct |
20 |
Correct |
267 ms |
4036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
340 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
231 ms |
4272 KB |
Output is correct |
12 |
Correct |
274 ms |
3980 KB |
Output is correct |
13 |
Correct |
422 ms |
3276 KB |
Output is correct |
14 |
Correct |
523 ms |
3272 KB |
Output is correct |
15 |
Correct |
370 ms |
4316 KB |
Output is correct |
16 |
Correct |
584 ms |
3532 KB |
Output is correct |
17 |
Correct |
483 ms |
3388 KB |
Output is correct |
18 |
Correct |
205 ms |
5240 KB |
Output is correct |
19 |
Correct |
222 ms |
2340 KB |
Output is correct |
20 |
Correct |
267 ms |
4036 KB |
Output is correct |
21 |
Correct |
301 ms |
4304 KB |
Output is correct |
22 |
Correct |
345 ms |
4588 KB |
Output is correct |
23 |
Correct |
657 ms |
3668 KB |
Output is correct |
24 |
Correct |
697 ms |
3400 KB |
Output is correct |
25 |
Correct |
453 ms |
5376 KB |
Output is correct |
26 |
Correct |
985 ms |
3836 KB |
Output is correct |
27 |
Correct |
708 ms |
3764 KB |
Output is correct |
28 |
Correct |
220 ms |
6488 KB |
Output is correct |
29 |
Correct |
264 ms |
2408 KB |
Output is correct |
30 |
Correct |
333 ms |
4668 KB |
Output is correct |
31 |
Correct |
554 ms |
18124 KB |
Output is correct |
32 |
Correct |
717 ms |
18132 KB |
Output is correct |
33 |
Correct |
452 ms |
16936 KB |
Output is correct |
34 |
Correct |
882 ms |
17060 KB |
Output is correct |
35 |
Correct |
720 ms |
17556 KB |
Output is correct |
36 |
Correct |
199 ms |
16124 KB |
Output is correct |
37 |
Correct |
297 ms |
18124 KB |
Output is correct |
38 |
Correct |
310 ms |
16092 KB |
Output is correct |
39 |
Correct |
668 ms |
17352 KB |
Output is correct |
40 |
Correct |
1616 ms |
17188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
340 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
76 ms |
9840 KB |
Output is correct |
12 |
Correct |
72 ms |
9836 KB |
Output is correct |
13 |
Correct |
112 ms |
9800 KB |
Output is correct |
14 |
Correct |
157 ms |
9868 KB |
Output is correct |
15 |
Correct |
79 ms |
9872 KB |
Output is correct |
16 |
Correct |
206 ms |
9816 KB |
Output is correct |
17 |
Correct |
163 ms |
9896 KB |
Output is correct |
18 |
Correct |
60 ms |
10068 KB |
Output is correct |
19 |
Correct |
65 ms |
9672 KB |
Output is correct |
20 |
Correct |
71 ms |
9936 KB |
Output is correct |
21 |
Correct |
103 ms |
9808 KB |
Output is correct |
22 |
Correct |
180 ms |
9856 KB |
Output is correct |
23 |
Correct |
85 ms |
9708 KB |
Output is correct |
24 |
Correct |
217 ms |
10044 KB |
Output is correct |
25 |
Correct |
168 ms |
9900 KB |
Output is correct |
26 |
Correct |
58 ms |
9712 KB |
Output is correct |
27 |
Correct |
74 ms |
9800 KB |
Output is correct |
28 |
Correct |
84 ms |
9672 KB |
Output is correct |
29 |
Correct |
147 ms |
9812 KB |
Output is correct |
30 |
Correct |
327 ms |
9804 KB |
Output is correct |
31 |
Correct |
84 ms |
9708 KB |
Output is correct |
32 |
Correct |
177 ms |
9844 KB |
Output is correct |
33 |
Correct |
160 ms |
9808 KB |
Output is correct |
34 |
Correct |
64 ms |
9672 KB |
Output is correct |
35 |
Correct |
138 ms |
9812 KB |
Output is correct |
36 |
Correct |
138 ms |
9772 KB |
Output is correct |
37 |
Correct |
141 ms |
9836 KB |
Output is correct |
38 |
Correct |
133 ms |
9816 KB |
Output is correct |
39 |
Correct |
136 ms |
9800 KB |
Output is correct |
40 |
Correct |
138 ms |
9832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
340 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
231 ms |
4272 KB |
Output is correct |
12 |
Correct |
274 ms |
3980 KB |
Output is correct |
13 |
Correct |
422 ms |
3276 KB |
Output is correct |
14 |
Correct |
523 ms |
3272 KB |
Output is correct |
15 |
Correct |
370 ms |
4316 KB |
Output is correct |
16 |
Correct |
584 ms |
3532 KB |
Output is correct |
17 |
Correct |
483 ms |
3388 KB |
Output is correct |
18 |
Correct |
205 ms |
5240 KB |
Output is correct |
19 |
Correct |
222 ms |
2340 KB |
Output is correct |
20 |
Correct |
267 ms |
4036 KB |
Output is correct |
21 |
Correct |
301 ms |
4304 KB |
Output is correct |
22 |
Correct |
345 ms |
4588 KB |
Output is correct |
23 |
Correct |
657 ms |
3668 KB |
Output is correct |
24 |
Correct |
697 ms |
3400 KB |
Output is correct |
25 |
Correct |
453 ms |
5376 KB |
Output is correct |
26 |
Correct |
985 ms |
3836 KB |
Output is correct |
27 |
Correct |
708 ms |
3764 KB |
Output is correct |
28 |
Correct |
220 ms |
6488 KB |
Output is correct |
29 |
Correct |
264 ms |
2408 KB |
Output is correct |
30 |
Correct |
333 ms |
4668 KB |
Output is correct |
31 |
Correct |
554 ms |
18124 KB |
Output is correct |
32 |
Correct |
717 ms |
18132 KB |
Output is correct |
33 |
Correct |
452 ms |
16936 KB |
Output is correct |
34 |
Correct |
882 ms |
17060 KB |
Output is correct |
35 |
Correct |
720 ms |
17556 KB |
Output is correct |
36 |
Correct |
199 ms |
16124 KB |
Output is correct |
37 |
Correct |
297 ms |
18124 KB |
Output is correct |
38 |
Correct |
310 ms |
16092 KB |
Output is correct |
39 |
Correct |
668 ms |
17352 KB |
Output is correct |
40 |
Correct |
1616 ms |
17188 KB |
Output is correct |
41 |
Correct |
76 ms |
9840 KB |
Output is correct |
42 |
Correct |
72 ms |
9836 KB |
Output is correct |
43 |
Correct |
112 ms |
9800 KB |
Output is correct |
44 |
Correct |
157 ms |
9868 KB |
Output is correct |
45 |
Correct |
79 ms |
9872 KB |
Output is correct |
46 |
Correct |
206 ms |
9816 KB |
Output is correct |
47 |
Correct |
163 ms |
9896 KB |
Output is correct |
48 |
Correct |
60 ms |
10068 KB |
Output is correct |
49 |
Correct |
65 ms |
9672 KB |
Output is correct |
50 |
Correct |
71 ms |
9936 KB |
Output is correct |
51 |
Correct |
103 ms |
9808 KB |
Output is correct |
52 |
Correct |
180 ms |
9856 KB |
Output is correct |
53 |
Correct |
85 ms |
9708 KB |
Output is correct |
54 |
Correct |
217 ms |
10044 KB |
Output is correct |
55 |
Correct |
168 ms |
9900 KB |
Output is correct |
56 |
Correct |
58 ms |
9712 KB |
Output is correct |
57 |
Correct |
74 ms |
9800 KB |
Output is correct |
58 |
Correct |
84 ms |
9672 KB |
Output is correct |
59 |
Correct |
147 ms |
9812 KB |
Output is correct |
60 |
Correct |
327 ms |
9804 KB |
Output is correct |
61 |
Correct |
84 ms |
9708 KB |
Output is correct |
62 |
Correct |
177 ms |
9844 KB |
Output is correct |
63 |
Correct |
160 ms |
9808 KB |
Output is correct |
64 |
Correct |
64 ms |
9672 KB |
Output is correct |
65 |
Correct |
138 ms |
9812 KB |
Output is correct |
66 |
Correct |
138 ms |
9772 KB |
Output is correct |
67 |
Correct |
141 ms |
9836 KB |
Output is correct |
68 |
Correct |
133 ms |
9816 KB |
Output is correct |
69 |
Correct |
136 ms |
9800 KB |
Output is correct |
70 |
Correct |
138 ms |
9832 KB |
Output is correct |
71 |
Correct |
596 ms |
36356 KB |
Output is correct |
72 |
Correct |
632 ms |
36356 KB |
Output is correct |
73 |
Correct |
1508 ms |
34860 KB |
Output is correct |
74 |
Execution timed out |
2049 ms |
31472 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |