#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#define ll long long //int
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (20)
#define int long long
int L, Q, pow3[MAXN];
string S,A;
int ans[1<<MAXN];
int mid=7;
int val[1<<(MAXN-7)];
int dp[1594323 + 2], lastqnm[1594323 + 2];
pi q[1<<MAXN];
string gug(ll x, ll place) {
string res="";
while(place --) {
res+=to_string(x%3);
x /= 3;
}
reverse(all(res));
return res;
}
int32_t main()
{
FAST
cin>>L>>Q; mid=min(mid, L);
cin>>S;
pow3[0]=1;FOR(i,1,MAXN-1)pow3[i]=pow3[i-1]*3ll;
FOR(ii,0,Q-1) { cin>>A; reverse(all(A));
FOR(i,0,min(L,mid)-1) {
if(A[i] == '0' || A[i] == '?') q[ii].f |= (1<<(2*i));
if(A[i] == '1' || A[i] == '?') q[ii].f |= (1<<(2*i+1));
}
FOR(i,min(L,mid),L-1) {
q[ii].s += pow3[i-min(L,mid)] * (A[i] == '?' ? 2 : (A[i] - '0'));
}
}
mmst(lastqnm, -1);
FOR(i,0,pow3[max(0ll,L-mid)]-1) {
int n=i;
FOR(j,0,L-mid-1) {
if(n % 3 == 2) lastqnm[i] = j;
n /= 3;
}
}
FOR(i,0,(1<<mid)-1) {
mmst(val, 0); FOR(j,0,pow3[max(0ll,L-mid)]-1) dp[j]=0;
if(L >= mid) FOR(j,0,(1<<(L-mid))-1) {
int c=0;
FOR(k,0,L-mid-1) {
if(j & (1<<k)) c += pow3[k];
}
dp[c] = S[i + (j << mid)] - '0';
}
if(L < mid) dp[0] = S[i]-'0';
if(L >= mid) FOR(j,0,pow3[L-mid]-1) if(~lastqnm[j]) {
dp[j] = dp[j-pow3[lastqnm[j]]] + dp[j-2*pow3[lastqnm[j]]]; // a base dp val will have lastqnm == -1
}
ll tomeet=0;
FOR(j,0,min(L, mid)-1) {
if( (1<<j) & i) tomeet |= (1<<(j*2+1));
else tomeet |= (1<<(j*2));
}
FOR(ii,0,Q-1) {
if( (q[ii].f & tomeet) == tomeet) ans[ii] += dp[q[ii].s];
}
}
FOR(ii,0,Q-1) cout<<ans[ii]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12920 KB |
Output is correct |
2 |
Correct |
14 ms |
12920 KB |
Output is correct |
3 |
Correct |
13 ms |
12920 KB |
Output is correct |
4 |
Correct |
14 ms |
12920 KB |
Output is correct |
5 |
Correct |
14 ms |
12920 KB |
Output is correct |
6 |
Correct |
14 ms |
12896 KB |
Output is correct |
7 |
Correct |
14 ms |
12920 KB |
Output is correct |
8 |
Correct |
14 ms |
12984 KB |
Output is correct |
9 |
Correct |
13 ms |
12920 KB |
Output is correct |
10 |
Correct |
14 ms |
12920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12920 KB |
Output is correct |
2 |
Correct |
14 ms |
12920 KB |
Output is correct |
3 |
Correct |
13 ms |
12920 KB |
Output is correct |
4 |
Correct |
14 ms |
12920 KB |
Output is correct |
5 |
Correct |
14 ms |
12920 KB |
Output is correct |
6 |
Correct |
14 ms |
12896 KB |
Output is correct |
7 |
Correct |
14 ms |
12920 KB |
Output is correct |
8 |
Correct |
14 ms |
12984 KB |
Output is correct |
9 |
Correct |
13 ms |
12920 KB |
Output is correct |
10 |
Correct |
14 ms |
12920 KB |
Output is correct |
11 |
Correct |
702 ms |
51224 KB |
Output is correct |
12 |
Correct |
783 ms |
50844 KB |
Output is correct |
13 |
Correct |
692 ms |
50160 KB |
Output is correct |
14 |
Correct |
700 ms |
50140 KB |
Output is correct |
15 |
Correct |
810 ms |
51160 KB |
Output is correct |
16 |
Correct |
709 ms |
50296 KB |
Output is correct |
17 |
Correct |
722 ms |
50380 KB |
Output is correct |
18 |
Correct |
521 ms |
52216 KB |
Output is correct |
19 |
Correct |
660 ms |
49204 KB |
Output is correct |
20 |
Correct |
770 ms |
50760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12920 KB |
Output is correct |
2 |
Correct |
14 ms |
12920 KB |
Output is correct |
3 |
Correct |
13 ms |
12920 KB |
Output is correct |
4 |
Correct |
14 ms |
12920 KB |
Output is correct |
5 |
Correct |
14 ms |
12920 KB |
Output is correct |
6 |
Correct |
14 ms |
12896 KB |
Output is correct |
7 |
Correct |
14 ms |
12920 KB |
Output is correct |
8 |
Correct |
14 ms |
12984 KB |
Output is correct |
9 |
Correct |
13 ms |
12920 KB |
Output is correct |
10 |
Correct |
14 ms |
12920 KB |
Output is correct |
11 |
Correct |
702 ms |
51224 KB |
Output is correct |
12 |
Correct |
783 ms |
50844 KB |
Output is correct |
13 |
Correct |
692 ms |
50160 KB |
Output is correct |
14 |
Correct |
700 ms |
50140 KB |
Output is correct |
15 |
Correct |
810 ms |
51160 KB |
Output is correct |
16 |
Correct |
709 ms |
50296 KB |
Output is correct |
17 |
Correct |
722 ms |
50380 KB |
Output is correct |
18 |
Correct |
521 ms |
52216 KB |
Output is correct |
19 |
Correct |
660 ms |
49204 KB |
Output is correct |
20 |
Correct |
770 ms |
50760 KB |
Output is correct |
21 |
Correct |
781 ms |
54100 KB |
Output is correct |
22 |
Correct |
791 ms |
54264 KB |
Output is correct |
23 |
Correct |
690 ms |
53372 KB |
Output is correct |
24 |
Correct |
745 ms |
53112 KB |
Output is correct |
25 |
Correct |
843 ms |
55256 KB |
Output is correct |
26 |
Correct |
769 ms |
53628 KB |
Output is correct |
27 |
Correct |
728 ms |
53880 KB |
Output is correct |
28 |
Correct |
544 ms |
56136 KB |
Output is correct |
29 |
Correct |
552 ms |
52104 KB |
Output is correct |
30 |
Correct |
804 ms |
54396 KB |
Output is correct |
31 |
Correct |
729 ms |
54132 KB |
Output is correct |
32 |
Correct |
718 ms |
54136 KB |
Output is correct |
33 |
Correct |
604 ms |
53112 KB |
Output is correct |
34 |
Correct |
712 ms |
53112 KB |
Output is correct |
35 |
Correct |
706 ms |
53880 KB |
Output is correct |
36 |
Correct |
567 ms |
52236 KB |
Output is correct |
37 |
Correct |
675 ms |
54112 KB |
Output is correct |
38 |
Correct |
567 ms |
52128 KB |
Output is correct |
39 |
Correct |
702 ms |
53364 KB |
Output is correct |
40 |
Correct |
724 ms |
53100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12920 KB |
Output is correct |
2 |
Correct |
14 ms |
12920 KB |
Output is correct |
3 |
Correct |
13 ms |
12920 KB |
Output is correct |
4 |
Correct |
14 ms |
12920 KB |
Output is correct |
5 |
Correct |
14 ms |
12920 KB |
Output is correct |
6 |
Correct |
14 ms |
12896 KB |
Output is correct |
7 |
Correct |
14 ms |
12920 KB |
Output is correct |
8 |
Correct |
14 ms |
12984 KB |
Output is correct |
9 |
Correct |
13 ms |
12920 KB |
Output is correct |
10 |
Correct |
14 ms |
12920 KB |
Output is correct |
11 |
Correct |
848 ms |
29956 KB |
Output is correct |
12 |
Correct |
858 ms |
29988 KB |
Output is correct |
13 |
Correct |
867 ms |
29948 KB |
Output is correct |
14 |
Correct |
861 ms |
29976 KB |
Output is correct |
15 |
Correct |
865 ms |
30116 KB |
Output is correct |
16 |
Correct |
843 ms |
29988 KB |
Output is correct |
17 |
Correct |
854 ms |
30100 KB |
Output is correct |
18 |
Correct |
818 ms |
30216 KB |
Output is correct |
19 |
Correct |
864 ms |
29976 KB |
Output is correct |
20 |
Correct |
836 ms |
30236 KB |
Output is correct |
21 |
Correct |
879 ms |
30060 KB |
Output is correct |
22 |
Correct |
884 ms |
29988 KB |
Output is correct |
23 |
Correct |
808 ms |
30076 KB |
Output is correct |
24 |
Correct |
826 ms |
30112 KB |
Output is correct |
25 |
Correct |
846 ms |
29976 KB |
Output is correct |
26 |
Correct |
823 ms |
29848 KB |
Output is correct |
27 |
Correct |
825 ms |
29976 KB |
Output is correct |
28 |
Correct |
821 ms |
29932 KB |
Output is correct |
29 |
Correct |
829 ms |
29960 KB |
Output is correct |
30 |
Correct |
844 ms |
29992 KB |
Output is correct |
31 |
Correct |
829 ms |
29952 KB |
Output is correct |
32 |
Correct |
934 ms |
30032 KB |
Output is correct |
33 |
Correct |
799 ms |
30104 KB |
Output is correct |
34 |
Correct |
888 ms |
29976 KB |
Output is correct |
35 |
Correct |
808 ms |
30024 KB |
Output is correct |
36 |
Correct |
871 ms |
29976 KB |
Output is correct |
37 |
Correct |
845 ms |
29960 KB |
Output is correct |
38 |
Correct |
797 ms |
30044 KB |
Output is correct |
39 |
Correct |
855 ms |
30116 KB |
Output is correct |
40 |
Correct |
855 ms |
29960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12920 KB |
Output is correct |
2 |
Correct |
14 ms |
12920 KB |
Output is correct |
3 |
Correct |
13 ms |
12920 KB |
Output is correct |
4 |
Correct |
14 ms |
12920 KB |
Output is correct |
5 |
Correct |
14 ms |
12920 KB |
Output is correct |
6 |
Correct |
14 ms |
12896 KB |
Output is correct |
7 |
Correct |
14 ms |
12920 KB |
Output is correct |
8 |
Correct |
14 ms |
12984 KB |
Output is correct |
9 |
Correct |
13 ms |
12920 KB |
Output is correct |
10 |
Correct |
14 ms |
12920 KB |
Output is correct |
11 |
Correct |
702 ms |
51224 KB |
Output is correct |
12 |
Correct |
783 ms |
50844 KB |
Output is correct |
13 |
Correct |
692 ms |
50160 KB |
Output is correct |
14 |
Correct |
700 ms |
50140 KB |
Output is correct |
15 |
Correct |
810 ms |
51160 KB |
Output is correct |
16 |
Correct |
709 ms |
50296 KB |
Output is correct |
17 |
Correct |
722 ms |
50380 KB |
Output is correct |
18 |
Correct |
521 ms |
52216 KB |
Output is correct |
19 |
Correct |
660 ms |
49204 KB |
Output is correct |
20 |
Correct |
770 ms |
50760 KB |
Output is correct |
21 |
Correct |
781 ms |
54100 KB |
Output is correct |
22 |
Correct |
791 ms |
54264 KB |
Output is correct |
23 |
Correct |
690 ms |
53372 KB |
Output is correct |
24 |
Correct |
745 ms |
53112 KB |
Output is correct |
25 |
Correct |
843 ms |
55256 KB |
Output is correct |
26 |
Correct |
769 ms |
53628 KB |
Output is correct |
27 |
Correct |
728 ms |
53880 KB |
Output is correct |
28 |
Correct |
544 ms |
56136 KB |
Output is correct |
29 |
Correct |
552 ms |
52104 KB |
Output is correct |
30 |
Correct |
804 ms |
54396 KB |
Output is correct |
31 |
Correct |
729 ms |
54132 KB |
Output is correct |
32 |
Correct |
718 ms |
54136 KB |
Output is correct |
33 |
Correct |
604 ms |
53112 KB |
Output is correct |
34 |
Correct |
712 ms |
53112 KB |
Output is correct |
35 |
Correct |
706 ms |
53880 KB |
Output is correct |
36 |
Correct |
567 ms |
52236 KB |
Output is correct |
37 |
Correct |
675 ms |
54112 KB |
Output is correct |
38 |
Correct |
567 ms |
52128 KB |
Output is correct |
39 |
Correct |
702 ms |
53364 KB |
Output is correct |
40 |
Correct |
724 ms |
53100 KB |
Output is correct |
41 |
Correct |
848 ms |
29956 KB |
Output is correct |
42 |
Correct |
858 ms |
29988 KB |
Output is correct |
43 |
Correct |
867 ms |
29948 KB |
Output is correct |
44 |
Correct |
861 ms |
29976 KB |
Output is correct |
45 |
Correct |
865 ms |
30116 KB |
Output is correct |
46 |
Correct |
843 ms |
29988 KB |
Output is correct |
47 |
Correct |
854 ms |
30100 KB |
Output is correct |
48 |
Correct |
818 ms |
30216 KB |
Output is correct |
49 |
Correct |
864 ms |
29976 KB |
Output is correct |
50 |
Correct |
836 ms |
30236 KB |
Output is correct |
51 |
Correct |
879 ms |
30060 KB |
Output is correct |
52 |
Correct |
884 ms |
29988 KB |
Output is correct |
53 |
Correct |
808 ms |
30076 KB |
Output is correct |
54 |
Correct |
826 ms |
30112 KB |
Output is correct |
55 |
Correct |
846 ms |
29976 KB |
Output is correct |
56 |
Correct |
823 ms |
29848 KB |
Output is correct |
57 |
Correct |
825 ms |
29976 KB |
Output is correct |
58 |
Correct |
821 ms |
29932 KB |
Output is correct |
59 |
Correct |
829 ms |
29960 KB |
Output is correct |
60 |
Correct |
844 ms |
29992 KB |
Output is correct |
61 |
Correct |
829 ms |
29952 KB |
Output is correct |
62 |
Correct |
934 ms |
30032 KB |
Output is correct |
63 |
Correct |
799 ms |
30104 KB |
Output is correct |
64 |
Correct |
888 ms |
29976 KB |
Output is correct |
65 |
Correct |
808 ms |
30024 KB |
Output is correct |
66 |
Correct |
871 ms |
29976 KB |
Output is correct |
67 |
Correct |
845 ms |
29960 KB |
Output is correct |
68 |
Correct |
797 ms |
30044 KB |
Output is correct |
69 |
Correct |
855 ms |
30116 KB |
Output is correct |
70 |
Correct |
855 ms |
29960 KB |
Output is correct |
71 |
Correct |
1859 ms |
65536 KB |
Output is correct |
72 |
Correct |
1937 ms |
65536 KB |
Output is correct |
73 |
Correct |
1728 ms |
65536 KB |
Output is correct |
74 |
Correct |
1961 ms |
65536 KB |
Output is correct |
75 |
Execution timed out |
2050 ms |
65536 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |