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[2][1 << 20];
char str[1 << 20];
int bit[20], sz;
int ans;
void bktQ(int p, int cnf) {
if(p == sz) {
ans += str[cnf] - '0';
}
else {
bktQ(p + 1, cnf);
bktQ(p + 1, cnf + (1 << bit[p]));
}
}
bool type;
void bkt(int p, int cnf, int cnt) {
if(p == sz) {
if(cnt & 1) {
ans -= dp[type][cnf];
}
else {
ans += dp[type][cnf];
}
}
else {
bkt(p + 1, cnf, cnt);
bkt(p + 1, cnf ^ (1 << bit[p]), cnt + 1);
}
}
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);
scanf("%d%d%s" ,&l,&q,str);
for(int conf = 0; conf < (1 << l); conf++) {
dp[0][conf ^ ((1 << l) - 1)] = str[conf] - '0';
dp[1][conf] = str[conf] - '0';
}
for(i = 0; i < 2; i++) {
for(int bit = 0; bit < l; bit++) {
for(int conf = 0; conf < (1 << (l - 1)); conf++) {
int cur = (conf & ((1 << bit) - 1)) + (1 << bit) + ((conf >> bit) << (bit + 1));
dp[i][cur] += dp[i][cur ^ (1 << bit)];
}
}
}
while(q > 0) {
q--;
static char cur[20];
scanf("%s" ,cur);
reverse(cur, cur + l);
int numq = 0, num0 = 0, num1 = 0;
for(i = 0; i < l; i++) {
if(cur[i] == '0') {
num0++;
}
else if(cur[i] == '1') {
num1++;
}
else {
numq++;
}
}
int mn = min(numq, min(num0, num1));
int cnf = 0;
ans = sz = 0;
if(numq == mn) {
for(i = 0; i < l; i++) {
if(cur[i] == '?') {
bit[sz++] = i;
}
else {
cnf += (1 << i) * (cur[i] == '1');
}
}
bktQ(0, cnf);
}
else if(num0 == mn) {
for(i = 0; i < l; i++) {
if(cur[i] == '0') {
bit[sz++] = i;
}
if(cur[i] != '1') {
cnf += (1 << i);
}
}
type = 0;
bkt(0, cnf, 0);
}
else if(num1 == mn) {
for(i = 0; i < l; i++) {
if(cur[i] == '1') {
bit[sz++] = i;
}
if(cur[i] != '0') {
cnf += (1 << i);
}
}
type = 1;
bkt(0, cnf, 0);
}
printf("%d\n" ,ans);
}
//cin.close();
//cout.close();
return 0;
}
Compilation message (stderr)
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%s" ,&l,&q,str);
~~~~~^~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s" ,cur);
~~~~~^~~~~~~~~~~
# | 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... |