#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
/*
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
void solve(int test_case)
{
int n,q; cin >> n >> q;
string s; cin >> s;
int B = max(n/2,1);
amin(B,7);
vector<int> pow3(n+5);
pow3[0] = 1;
rep1(i,n) pow3[i] = pow3[i-1]*3;
vector<pii> queries(q);
rep(id,q){
string t; cin >> t;
int first_t = 0, last_t = 0;
rep(i,sz(t)){
ll x = 0;
if(t[i] == '0') x = 0;
if(t[i] == '1') x = 1;
if(t[i] == '?') x = 2;
if(i < B) first_t = first_t*3+x;
else last_t = last_t*3+x;
}
queries[id] = {first_t,last_t};
}
vector<int> maskf(1<<(n-B));
rep(i,1<<(n-B)){
int mask = 0;
rep(bit,n-B){
int b = 0;
if(i&(1<<bit)) b = 1;
mask += pow3[bit]*b;
}
maskf[i] = mask;
}
vector<int> lsqf(pow3[n-B],inf1);
rep(i,pow3[n-B]){
if(i%3 == 2) lsqf[i] = 0;
else lsqf[i] = lsqf[i/3]+1;
}
bool ok[1<<B][pow3[B]];
memset(ok,0,sizeof ok);
ok[0][0] = 1;
rep(mask1,1<<B){
rep(mask2,pow3[B]){
if(mask1 == 0 and mask2 == 0) conts;
int b1 = mask1&1;
int b2 = mask2%3;
if(b2 == 2 or b1 == b2){
ok[mask1][mask2] = ok[mask1>>1][mask2/3];
}
else{
ok[mask1][mask2] = 0;
}
}
}
vector<int> ans(q);
vector<int> dp(pow3[n-B]);
rep(mask1,1<<B){
fill(all(dp),0);
rep(i,1<<n){
int firstB = i>>(n-B);
int lastB = i^(firstB<<(n-B));
if(firstB != mask1) conts;
dp[maskf[lastB]] += s[i]-'0';
}
rep(mask2,pow3[n-B]){
int lsq = lsqf[mask2];
if(lsq >= inf1) conts;
rep(x,2){
int mask3 = mask2-pow3[lsq]*2+pow3[lsq]*x;
dp[mask2] += dp[mask3];
}
}
rep(id,q){
ans[id] += ok[mask1][queries[id].ff]*dp[queries[id].ss];
}
}
rep(i,q) cout << ans[i] << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
159 ms |
16212 KB |
Output is correct |
12 |
Correct |
129 ms |
15712 KB |
Output is correct |
13 |
Correct |
151 ms |
14932 KB |
Output is correct |
14 |
Correct |
150 ms |
15188 KB |
Output is correct |
15 |
Correct |
150 ms |
16208 KB |
Output is correct |
16 |
Correct |
187 ms |
15184 KB |
Output is correct |
17 |
Correct |
164 ms |
15184 KB |
Output is correct |
18 |
Correct |
126 ms |
16976 KB |
Output is correct |
19 |
Correct |
153 ms |
14176 KB |
Output is correct |
20 |
Correct |
166 ms |
15704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
159 ms |
16212 KB |
Output is correct |
12 |
Correct |
129 ms |
15712 KB |
Output is correct |
13 |
Correct |
151 ms |
14932 KB |
Output is correct |
14 |
Correct |
150 ms |
15188 KB |
Output is correct |
15 |
Correct |
150 ms |
16208 KB |
Output is correct |
16 |
Correct |
187 ms |
15184 KB |
Output is correct |
17 |
Correct |
164 ms |
15184 KB |
Output is correct |
18 |
Correct |
126 ms |
16976 KB |
Output is correct |
19 |
Correct |
153 ms |
14176 KB |
Output is correct |
20 |
Correct |
166 ms |
15704 KB |
Output is correct |
21 |
Correct |
196 ms |
16212 KB |
Output is correct |
22 |
Correct |
161 ms |
16228 KB |
Output is correct |
23 |
Correct |
195 ms |
15468 KB |
Output is correct |
24 |
Correct |
198 ms |
15220 KB |
Output is correct |
25 |
Correct |
183 ms |
17252 KB |
Output is correct |
26 |
Correct |
212 ms |
15696 KB |
Output is correct |
27 |
Correct |
199 ms |
15716 KB |
Output is correct |
28 |
Correct |
201 ms |
18260 KB |
Output is correct |
29 |
Correct |
190 ms |
14160 KB |
Output is correct |
30 |
Correct |
217 ms |
16236 KB |
Output is correct |
31 |
Correct |
185 ms |
16236 KB |
Output is correct |
32 |
Correct |
181 ms |
16252 KB |
Output is correct |
33 |
Correct |
198 ms |
15184 KB |
Output is correct |
34 |
Correct |
195 ms |
15208 KB |
Output is correct |
35 |
Correct |
196 ms |
15716 KB |
Output is correct |
36 |
Correct |
148 ms |
14160 KB |
Output is correct |
37 |
Correct |
158 ms |
16208 KB |
Output is correct |
38 |
Correct |
211 ms |
14420 KB |
Output is correct |
39 |
Correct |
209 ms |
15224 KB |
Output is correct |
40 |
Correct |
194 ms |
15076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
457 ms |
14940 KB |
Output is correct |
12 |
Correct |
432 ms |
15104 KB |
Output is correct |
13 |
Correct |
458 ms |
14852 KB |
Output is correct |
14 |
Correct |
433 ms |
14844 KB |
Output is correct |
15 |
Correct |
452 ms |
15104 KB |
Output is correct |
16 |
Correct |
490 ms |
14848 KB |
Output is correct |
17 |
Correct |
445 ms |
14848 KB |
Output is correct |
18 |
Correct |
434 ms |
15088 KB |
Output is correct |
19 |
Correct |
434 ms |
14848 KB |
Output is correct |
20 |
Correct |
452 ms |
15104 KB |
Output is correct |
21 |
Correct |
437 ms |
15092 KB |
Output is correct |
22 |
Correct |
437 ms |
14848 KB |
Output is correct |
23 |
Correct |
450 ms |
14848 KB |
Output is correct |
24 |
Correct |
441 ms |
14848 KB |
Output is correct |
25 |
Correct |
480 ms |
14844 KB |
Output is correct |
26 |
Correct |
429 ms |
14844 KB |
Output is correct |
27 |
Correct |
429 ms |
14852 KB |
Output is correct |
28 |
Correct |
463 ms |
15092 KB |
Output is correct |
29 |
Correct |
438 ms |
14844 KB |
Output is correct |
30 |
Correct |
463 ms |
15096 KB |
Output is correct |
31 |
Correct |
436 ms |
14848 KB |
Output is correct |
32 |
Correct |
476 ms |
14844 KB |
Output is correct |
33 |
Correct |
446 ms |
14844 KB |
Output is correct |
34 |
Correct |
427 ms |
14868 KB |
Output is correct |
35 |
Correct |
467 ms |
14848 KB |
Output is correct |
36 |
Correct |
444 ms |
14848 KB |
Output is correct |
37 |
Correct |
450 ms |
15096 KB |
Output is correct |
38 |
Correct |
447 ms |
14844 KB |
Output is correct |
39 |
Correct |
442 ms |
14852 KB |
Output is correct |
40 |
Correct |
450 ms |
15088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
159 ms |
16212 KB |
Output is correct |
12 |
Correct |
129 ms |
15712 KB |
Output is correct |
13 |
Correct |
151 ms |
14932 KB |
Output is correct |
14 |
Correct |
150 ms |
15188 KB |
Output is correct |
15 |
Correct |
150 ms |
16208 KB |
Output is correct |
16 |
Correct |
187 ms |
15184 KB |
Output is correct |
17 |
Correct |
164 ms |
15184 KB |
Output is correct |
18 |
Correct |
126 ms |
16976 KB |
Output is correct |
19 |
Correct |
153 ms |
14176 KB |
Output is correct |
20 |
Correct |
166 ms |
15704 KB |
Output is correct |
21 |
Correct |
196 ms |
16212 KB |
Output is correct |
22 |
Correct |
161 ms |
16228 KB |
Output is correct |
23 |
Correct |
195 ms |
15468 KB |
Output is correct |
24 |
Correct |
198 ms |
15220 KB |
Output is correct |
25 |
Correct |
183 ms |
17252 KB |
Output is correct |
26 |
Correct |
212 ms |
15696 KB |
Output is correct |
27 |
Correct |
199 ms |
15716 KB |
Output is correct |
28 |
Correct |
201 ms |
18260 KB |
Output is correct |
29 |
Correct |
190 ms |
14160 KB |
Output is correct |
30 |
Correct |
217 ms |
16236 KB |
Output is correct |
31 |
Correct |
185 ms |
16236 KB |
Output is correct |
32 |
Correct |
181 ms |
16252 KB |
Output is correct |
33 |
Correct |
198 ms |
15184 KB |
Output is correct |
34 |
Correct |
195 ms |
15208 KB |
Output is correct |
35 |
Correct |
196 ms |
15716 KB |
Output is correct |
36 |
Correct |
148 ms |
14160 KB |
Output is correct |
37 |
Correct |
158 ms |
16208 KB |
Output is correct |
38 |
Correct |
211 ms |
14420 KB |
Output is correct |
39 |
Correct |
209 ms |
15224 KB |
Output is correct |
40 |
Correct |
194 ms |
15076 KB |
Output is correct |
41 |
Correct |
457 ms |
14940 KB |
Output is correct |
42 |
Correct |
432 ms |
15104 KB |
Output is correct |
43 |
Correct |
458 ms |
14852 KB |
Output is correct |
44 |
Correct |
433 ms |
14844 KB |
Output is correct |
45 |
Correct |
452 ms |
15104 KB |
Output is correct |
46 |
Correct |
490 ms |
14848 KB |
Output is correct |
47 |
Correct |
445 ms |
14848 KB |
Output is correct |
48 |
Correct |
434 ms |
15088 KB |
Output is correct |
49 |
Correct |
434 ms |
14848 KB |
Output is correct |
50 |
Correct |
452 ms |
15104 KB |
Output is correct |
51 |
Correct |
437 ms |
15092 KB |
Output is correct |
52 |
Correct |
437 ms |
14848 KB |
Output is correct |
53 |
Correct |
450 ms |
14848 KB |
Output is correct |
54 |
Correct |
441 ms |
14848 KB |
Output is correct |
55 |
Correct |
480 ms |
14844 KB |
Output is correct |
56 |
Correct |
429 ms |
14844 KB |
Output is correct |
57 |
Correct |
429 ms |
14852 KB |
Output is correct |
58 |
Correct |
463 ms |
15092 KB |
Output is correct |
59 |
Correct |
438 ms |
14844 KB |
Output is correct |
60 |
Correct |
463 ms |
15096 KB |
Output is correct |
61 |
Correct |
436 ms |
14848 KB |
Output is correct |
62 |
Correct |
476 ms |
14844 KB |
Output is correct |
63 |
Correct |
446 ms |
14844 KB |
Output is correct |
64 |
Correct |
427 ms |
14868 KB |
Output is correct |
65 |
Correct |
467 ms |
14848 KB |
Output is correct |
66 |
Correct |
444 ms |
14848 KB |
Output is correct |
67 |
Correct |
450 ms |
15096 KB |
Output is correct |
68 |
Correct |
447 ms |
14844 KB |
Output is correct |
69 |
Correct |
442 ms |
14852 KB |
Output is correct |
70 |
Correct |
450 ms |
15088 KB |
Output is correct |
71 |
Correct |
776 ms |
30960 KB |
Output is correct |
72 |
Correct |
717 ms |
31216 KB |
Output is correct |
73 |
Correct |
907 ms |
29644 KB |
Output is correct |
74 |
Correct |
846 ms |
29936 KB |
Output is correct |
75 |
Correct |
808 ms |
31984 KB |
Output is correct |
76 |
Correct |
864 ms |
30192 KB |
Output is correct |
77 |
Correct |
837 ms |
30192 KB |
Output is correct |
78 |
Correct |
701 ms |
34020 KB |
Output is correct |
79 |
Correct |
754 ms |
27892 KB |
Output is correct |
80 |
Correct |
736 ms |
31216 KB |
Output is correct |
81 |
Correct |
811 ms |
30960 KB |
Output is correct |
82 |
Correct |
874 ms |
29932 KB |
Output is correct |
83 |
Correct |
825 ms |
29136 KB |
Output is correct |
84 |
Correct |
867 ms |
29908 KB |
Output is correct |
85 |
Correct |
867 ms |
30160 KB |
Output is correct |
86 |
Correct |
671 ms |
27880 KB |
Output is correct |
87 |
Correct |
702 ms |
31312 KB |
Output is correct |
88 |
Correct |
772 ms |
27880 KB |
Output is correct |
89 |
Correct |
873 ms |
29680 KB |
Output is correct |
90 |
Correct |
864 ms |
29936 KB |
Output is correct |
91 |
Correct |
831 ms |
29164 KB |
Output is correct |
92 |
Correct |
855 ms |
30196 KB |
Output is correct |
93 |
Correct |
864 ms |
30160 KB |
Output is correct |
94 |
Correct |
679 ms |
27884 KB |
Output is correct |
95 |
Correct |
858 ms |
30160 KB |
Output is correct |
96 |
Correct |
889 ms |
30196 KB |
Output is correct |
97 |
Correct |
860 ms |
30188 KB |
Output is correct |
98 |
Correct |
844 ms |
30168 KB |
Output is correct |
99 |
Correct |
852 ms |
30192 KB |
Output is correct |
100 |
Correct |
951 ms |
30192 KB |
Output is correct |