This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int N, L, Q, T;
int arr[1<<20];
int dp[60000][1<<10];
vector<int> ternary[60000];
int pows[20];
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> L >> Q;
N = 1<<L;
for(int m = 0; m<N; m++){
char c;
cin >> c;
arr[m] = c-'0';
}
int H = L/2;
T = 1;
pows[0] = 1;
for(int k = 0; k<H; k++){
T *= 3;
pows[k+1] = pows[k] * 3;
}
for(int m = 0; m<T; m++){
int temp = m;
for(int k = 0; k<H; k++){
ternary[m].emplace_back(temp%3);
temp /= 3;
}
}
for(int m = 0; m<1<<(L-H); m++){
for(int t = 0; t<T; t++){
bool has2 = 0;
int temp = 0;
for(int h = 0; h<H; h++){
if(ternary[t][h] == 2){
has2 = 1;
dp[t][m] = dp[t-pows[h]][m] + dp[t-pows[h]*2][m];
break;
}
temp += (1<<h)*ternary[t][h];
}
if(!has2){
dp[t][m] = arr[(temp<<(L-H)) + m];
}
}
}
while(Q--){
string s;
cin >> s;
int t = 0;
vector<int> qu;
for(int i = 0; i<L; i++){
if(s[i] == '?'){
qu.emplace_back(2);
}
else{
qu.emplace_back(s[i]-'0');
}
}
for(int k = 0; k<H; k++){
t *= 3;
t += qu[k];
}
reverse(qu.begin(), qu.end());
long long out = 0;
for(int m = 0; m<1<<(L-H); m++){
bool good = 1;
for(int b = 0; b<L-H; b++){
if(qu[b] == 2 || qu[b] == ((m>>b)&1)){
}
else{
good = 0;
break;
}
}
if(good){
out += dp[t][m];
}
}
cout << out << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |