#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)
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;
}
int 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(0,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(0,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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6648 KB |
Output is correct |
2 |
Correct |
8 ms |
6648 KB |
Output is correct |
3 |
Correct |
8 ms |
6648 KB |
Output is correct |
4 |
Correct |
8 ms |
6652 KB |
Output is correct |
5 |
Correct |
8 ms |
6648 KB |
Output is correct |
6 |
Correct |
8 ms |
6708 KB |
Output is correct |
7 |
Correct |
8 ms |
6648 KB |
Output is correct |
8 |
Correct |
8 ms |
6648 KB |
Output is correct |
9 |
Correct |
9 ms |
6648 KB |
Output is correct |
10 |
Correct |
10 ms |
6704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6648 KB |
Output is correct |
2 |
Correct |
8 ms |
6648 KB |
Output is correct |
3 |
Correct |
8 ms |
6648 KB |
Output is correct |
4 |
Correct |
8 ms |
6652 KB |
Output is correct |
5 |
Correct |
8 ms |
6648 KB |
Output is correct |
6 |
Correct |
8 ms |
6708 KB |
Output is correct |
7 |
Correct |
8 ms |
6648 KB |
Output is correct |
8 |
Correct |
8 ms |
6648 KB |
Output is correct |
9 |
Correct |
9 ms |
6648 KB |
Output is correct |
10 |
Correct |
10 ms |
6704 KB |
Output is correct |
11 |
Correct |
648 ms |
31000 KB |
Output is correct |
12 |
Correct |
702 ms |
30656 KB |
Output is correct |
13 |
Correct |
577 ms |
29912 KB |
Output is correct |
14 |
Correct |
625 ms |
29988 KB |
Output is correct |
15 |
Correct |
752 ms |
31228 KB |
Output is correct |
16 |
Correct |
673 ms |
30288 KB |
Output is correct |
17 |
Correct |
614 ms |
30220 KB |
Output is correct |
18 |
Correct |
512 ms |
32280 KB |
Output is correct |
19 |
Correct |
513 ms |
29176 KB |
Output is correct |
20 |
Correct |
704 ms |
30840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6648 KB |
Output is correct |
2 |
Correct |
8 ms |
6648 KB |
Output is correct |
3 |
Correct |
8 ms |
6648 KB |
Output is correct |
4 |
Correct |
8 ms |
6652 KB |
Output is correct |
5 |
Correct |
8 ms |
6648 KB |
Output is correct |
6 |
Correct |
8 ms |
6708 KB |
Output is correct |
7 |
Correct |
8 ms |
6648 KB |
Output is correct |
8 |
Correct |
8 ms |
6648 KB |
Output is correct |
9 |
Correct |
9 ms |
6648 KB |
Output is correct |
10 |
Correct |
10 ms |
6704 KB |
Output is correct |
11 |
Correct |
648 ms |
31000 KB |
Output is correct |
12 |
Correct |
702 ms |
30656 KB |
Output is correct |
13 |
Correct |
577 ms |
29912 KB |
Output is correct |
14 |
Correct |
625 ms |
29988 KB |
Output is correct |
15 |
Correct |
752 ms |
31228 KB |
Output is correct |
16 |
Correct |
673 ms |
30288 KB |
Output is correct |
17 |
Correct |
614 ms |
30220 KB |
Output is correct |
18 |
Correct |
512 ms |
32280 KB |
Output is correct |
19 |
Correct |
513 ms |
29176 KB |
Output is correct |
20 |
Correct |
704 ms |
30840 KB |
Output is correct |
21 |
Correct |
707 ms |
31268 KB |
Output is correct |
22 |
Correct |
765 ms |
31396 KB |
Output is correct |
23 |
Correct |
577 ms |
30456 KB |
Output is correct |
24 |
Correct |
632 ms |
30236 KB |
Output is correct |
25 |
Correct |
806 ms |
32272 KB |
Output is correct |
26 |
Correct |
639 ms |
30756 KB |
Output is correct |
27 |
Correct |
634 ms |
30712 KB |
Output is correct |
28 |
Correct |
510 ms |
33216 KB |
Output is correct |
29 |
Correct |
520 ms |
29436 KB |
Output is correct |
30 |
Correct |
714 ms |
31652 KB |
Output is correct |
31 |
Correct |
671 ms |
31440 KB |
Output is correct |
32 |
Correct |
635 ms |
31396 KB |
Output is correct |
33 |
Correct |
540 ms |
30328 KB |
Output is correct |
34 |
Correct |
597 ms |
30420 KB |
Output is correct |
35 |
Correct |
627 ms |
30844 KB |
Output is correct |
36 |
Correct |
402 ms |
29496 KB |
Output is correct |
37 |
Correct |
631 ms |
31480 KB |
Output is correct |
38 |
Correct |
526 ms |
29536 KB |
Output is correct |
39 |
Correct |
704 ms |
30844 KB |
Output is correct |
40 |
Correct |
596 ms |
30584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6648 KB |
Output is correct |
2 |
Correct |
8 ms |
6648 KB |
Output is correct |
3 |
Correct |
8 ms |
6648 KB |
Output is correct |
4 |
Correct |
8 ms |
6652 KB |
Output is correct |
5 |
Correct |
8 ms |
6648 KB |
Output is correct |
6 |
Correct |
8 ms |
6708 KB |
Output is correct |
7 |
Correct |
8 ms |
6648 KB |
Output is correct |
8 |
Correct |
8 ms |
6648 KB |
Output is correct |
9 |
Correct |
9 ms |
6648 KB |
Output is correct |
10 |
Correct |
10 ms |
6704 KB |
Output is correct |
11 |
Correct |
721 ms |
16608 KB |
Output is correct |
12 |
Correct |
648 ms |
16796 KB |
Output is correct |
13 |
Correct |
657 ms |
16692 KB |
Output is correct |
14 |
Correct |
643 ms |
16632 KB |
Output is correct |
15 |
Correct |
650 ms |
16732 KB |
Output is correct |
16 |
Correct |
637 ms |
16664 KB |
Output is correct |
17 |
Correct |
656 ms |
16600 KB |
Output is correct |
18 |
Correct |
624 ms |
17076 KB |
Output is correct |
19 |
Correct |
636 ms |
16664 KB |
Output is correct |
20 |
Correct |
633 ms |
16856 KB |
Output is correct |
21 |
Correct |
640 ms |
16724 KB |
Output is correct |
22 |
Correct |
638 ms |
16792 KB |
Output is correct |
23 |
Correct |
640 ms |
16920 KB |
Output is correct |
24 |
Correct |
636 ms |
16920 KB |
Output is correct |
25 |
Correct |
636 ms |
16912 KB |
Output is correct |
26 |
Correct |
641 ms |
16680 KB |
Output is correct |
27 |
Correct |
631 ms |
16920 KB |
Output is correct |
28 |
Correct |
619 ms |
16692 KB |
Output is correct |
29 |
Correct |
633 ms |
16816 KB |
Output is correct |
30 |
Correct |
635 ms |
16900 KB |
Output is correct |
31 |
Correct |
620 ms |
16892 KB |
Output is correct |
32 |
Correct |
633 ms |
16920 KB |
Output is correct |
33 |
Correct |
634 ms |
16864 KB |
Output is correct |
34 |
Correct |
608 ms |
16732 KB |
Output is correct |
35 |
Correct |
631 ms |
16908 KB |
Output is correct |
36 |
Correct |
634 ms |
16920 KB |
Output is correct |
37 |
Correct |
670 ms |
16992 KB |
Output is correct |
38 |
Correct |
648 ms |
17048 KB |
Output is correct |
39 |
Correct |
660 ms |
17020 KB |
Output is correct |
40 |
Correct |
645 ms |
17024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6648 KB |
Output is correct |
2 |
Correct |
8 ms |
6648 KB |
Output is correct |
3 |
Correct |
8 ms |
6648 KB |
Output is correct |
4 |
Correct |
8 ms |
6652 KB |
Output is correct |
5 |
Correct |
8 ms |
6648 KB |
Output is correct |
6 |
Correct |
8 ms |
6708 KB |
Output is correct |
7 |
Correct |
8 ms |
6648 KB |
Output is correct |
8 |
Correct |
8 ms |
6648 KB |
Output is correct |
9 |
Correct |
9 ms |
6648 KB |
Output is correct |
10 |
Correct |
10 ms |
6704 KB |
Output is correct |
11 |
Correct |
648 ms |
31000 KB |
Output is correct |
12 |
Correct |
702 ms |
30656 KB |
Output is correct |
13 |
Correct |
577 ms |
29912 KB |
Output is correct |
14 |
Correct |
625 ms |
29988 KB |
Output is correct |
15 |
Correct |
752 ms |
31228 KB |
Output is correct |
16 |
Correct |
673 ms |
30288 KB |
Output is correct |
17 |
Correct |
614 ms |
30220 KB |
Output is correct |
18 |
Correct |
512 ms |
32280 KB |
Output is correct |
19 |
Correct |
513 ms |
29176 KB |
Output is correct |
20 |
Correct |
704 ms |
30840 KB |
Output is correct |
21 |
Correct |
707 ms |
31268 KB |
Output is correct |
22 |
Correct |
765 ms |
31396 KB |
Output is correct |
23 |
Correct |
577 ms |
30456 KB |
Output is correct |
24 |
Correct |
632 ms |
30236 KB |
Output is correct |
25 |
Correct |
806 ms |
32272 KB |
Output is correct |
26 |
Correct |
639 ms |
30756 KB |
Output is correct |
27 |
Correct |
634 ms |
30712 KB |
Output is correct |
28 |
Correct |
510 ms |
33216 KB |
Output is correct |
29 |
Correct |
520 ms |
29436 KB |
Output is correct |
30 |
Correct |
714 ms |
31652 KB |
Output is correct |
31 |
Correct |
671 ms |
31440 KB |
Output is correct |
32 |
Correct |
635 ms |
31396 KB |
Output is correct |
33 |
Correct |
540 ms |
30328 KB |
Output is correct |
34 |
Correct |
597 ms |
30420 KB |
Output is correct |
35 |
Correct |
627 ms |
30844 KB |
Output is correct |
36 |
Correct |
402 ms |
29496 KB |
Output is correct |
37 |
Correct |
631 ms |
31480 KB |
Output is correct |
38 |
Correct |
526 ms |
29536 KB |
Output is correct |
39 |
Correct |
704 ms |
30844 KB |
Output is correct |
40 |
Correct |
596 ms |
30584 KB |
Output is correct |
41 |
Correct |
721 ms |
16608 KB |
Output is correct |
42 |
Correct |
648 ms |
16796 KB |
Output is correct |
43 |
Correct |
657 ms |
16692 KB |
Output is correct |
44 |
Correct |
643 ms |
16632 KB |
Output is correct |
45 |
Correct |
650 ms |
16732 KB |
Output is correct |
46 |
Correct |
637 ms |
16664 KB |
Output is correct |
47 |
Correct |
656 ms |
16600 KB |
Output is correct |
48 |
Correct |
624 ms |
17076 KB |
Output is correct |
49 |
Correct |
636 ms |
16664 KB |
Output is correct |
50 |
Correct |
633 ms |
16856 KB |
Output is correct |
51 |
Correct |
640 ms |
16724 KB |
Output is correct |
52 |
Correct |
638 ms |
16792 KB |
Output is correct |
53 |
Correct |
640 ms |
16920 KB |
Output is correct |
54 |
Correct |
636 ms |
16920 KB |
Output is correct |
55 |
Correct |
636 ms |
16912 KB |
Output is correct |
56 |
Correct |
641 ms |
16680 KB |
Output is correct |
57 |
Correct |
631 ms |
16920 KB |
Output is correct |
58 |
Correct |
619 ms |
16692 KB |
Output is correct |
59 |
Correct |
633 ms |
16816 KB |
Output is correct |
60 |
Correct |
635 ms |
16900 KB |
Output is correct |
61 |
Correct |
620 ms |
16892 KB |
Output is correct |
62 |
Correct |
633 ms |
16920 KB |
Output is correct |
63 |
Correct |
634 ms |
16864 KB |
Output is correct |
64 |
Correct |
608 ms |
16732 KB |
Output is correct |
65 |
Correct |
631 ms |
16908 KB |
Output is correct |
66 |
Correct |
634 ms |
16920 KB |
Output is correct |
67 |
Correct |
670 ms |
16992 KB |
Output is correct |
68 |
Correct |
648 ms |
17048 KB |
Output is correct |
69 |
Correct |
660 ms |
17020 KB |
Output is correct |
70 |
Correct |
645 ms |
17024 KB |
Output is correct |
71 |
Correct |
1531 ms |
51548 KB |
Output is correct |
72 |
Correct |
1850 ms |
51916 KB |
Output is correct |
73 |
Correct |
1449 ms |
48952 KB |
Output is correct |
74 |
Correct |
1525 ms |
49696 KB |
Output is correct |
75 |
Correct |
1688 ms |
51864 KB |
Output is correct |
76 |
Correct |
1527 ms |
59496 KB |
Output is correct |
77 |
Correct |
1612 ms |
59496 KB |
Output is correct |
78 |
Correct |
1220 ms |
63204 KB |
Output is correct |
79 |
Correct |
1221 ms |
57148 KB |
Output is correct |
80 |
Correct |
1580 ms |
60316 KB |
Output is correct |
81 |
Correct |
1613 ms |
60168 KB |
Output is correct |
82 |
Correct |
1531 ms |
59292 KB |
Output is correct |
83 |
Correct |
1331 ms |
58392 KB |
Output is correct |
84 |
Correct |
1504 ms |
59232 KB |
Output is correct |
85 |
Correct |
1569 ms |
59396 KB |
Output is correct |
86 |
Correct |
1376 ms |
57148 KB |
Output is correct |
87 |
Correct |
1476 ms |
60172 KB |
Output is correct |
88 |
Correct |
1234 ms |
57336 KB |
Output is correct |
89 |
Correct |
1467 ms |
58880 KB |
Output is correct |
90 |
Correct |
1567 ms |
59140 KB |
Output is correct |
91 |
Correct |
1286 ms |
58268 KB |
Output is correct |
92 |
Correct |
1567 ms |
59396 KB |
Output is correct |
93 |
Correct |
1559 ms |
59372 KB |
Output is correct |
94 |
Correct |
1117 ms |
57108 KB |
Output is correct |
95 |
Correct |
1531 ms |
59440 KB |
Output is correct |
96 |
Correct |
1559 ms |
59272 KB |
Output is correct |
97 |
Correct |
1614 ms |
59184 KB |
Output is correct |
98 |
Correct |
1563 ms |
59440 KB |
Output is correct |
99 |
Correct |
1600 ms |
59404 KB |
Output is correct |
100 |
Correct |
1624 ms |
59272 KB |
Output is correct |