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>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
int dp[1 << 20];
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, l, q;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> l >> q;
string str;
cin >> str;
for(int conf = 0; conf < (1 << l); conf++) {
dp[conf] = str[conf] - '0';
}
for(int bit = 0; bit < l; bit++) {
for(int conf = 0; conf < (1 << l); conf++) {
if(conf & (1 << bit)) {
dp[conf] += dp[conf ^ (1 << bit)];
}
}
}
unordered_map <int, int> mp;
while(q > 0) {
q--;
string cur;
cin >> cur;
reverse(cur.begin(), cur.end());
int num = 0, val = 0;
for(i = 0; i < l; i++) {
val *= 3;
if(cur[i] == '?') {
num++;
val += 2;
}
else {
val += cur[i] - '0';
}
}
if(mp[val] > 0) {
cout << mp[val] - 1 << "\n";
continue;
}
vector <int> bit;
if(num <= 10) {
int ans = 0, cnf = 0;
for(i = 0; i < l; i++) {
if(cur[i] == '?') {
bit.push_back(i);
}
if(cur[i] == '1') {
cnf ^= (1 << i);
}
}
int sz = bit.size();
for(int conf = 0; conf < (1 << sz); conf++) {
int aux = cnf;
for(i = 0; i < sz; i++) {
if(conf & (1 << i)) {
aux += (1 << bit[i]);
}
}
ans += str[aux] - '0';
}
mp[val] = ans + 1;
cout << ans << "\n";
}
else {
ll ans = 0;
int cnf = 0;
for(i = 0; i < l; i++) {
if(cur[i] == '1') {
bit.push_back(i);
}
if(cur[i] != '0') {
cnf ^= (1 << i);
}
}
int sz = bit.size();
for(int conf = 0; conf < (1 << sz); conf++) {
int aux = cnf, cnt = 0;
for(i = 0; i < sz; i++) {
if(conf & (1 << i)) {
aux ^= (1 << bit[i]);
cnt++;
}
}
if(cnt & 1) {
ans -= dp[aux];
}
else {
ans += dp[aux];
}
}
mp[val] = ans + 1;
cout << ans << "\n";
}
}
//cin.close();
//cout.close();
return 0;
}
# | 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... |