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;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
template<class T>
void readi(T &x)
{
x = 0;
bool negative = false;
char c = ' ';
while (c < '-')
{
c = getchar();
}
if (c == '-')
{
negative = true;
c = getchar();
}
while (c >= '0')
{
x = x * 10 + (c - '0');
c = getchar();
}
if (negative)
{
x = -x;
}
}
template<class T>
void printi(T output)
{
if (output == 0)
{
putchar('0');
return;
}
if (output < 0)
{
putchar('-');
output = -output;
}
int buf[20], n = 0;
while(output)
{
buf[n] = ((output % 10));
output /= 10;
n++;
}
for (n--; n >= 0; n--)
{
putchar(buf[n] + '0');
}
return;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 1100013
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, Q;
int arr[MAXN];
int sos[MAXN], sob[MAXN]; //b is for big [superset]
vi freq[3];
int ans;
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
readi(N); readi(Q);
FOR(i, 0, (1 << N))
{
char c = getchar();
arr[i] = c - '0';
sos[i] = arr[i];
sob[i] = arr[i];
}
//compute answer if there were only 1s and ????s
FOR(i, 0, N)
{
FOR(j, 0, (1 << N))
{
if (j & (1 << i))
{
sos[j] += sos[j - (1 << i)];
}
else
{
sob[j] += sob[j + (1 << i)];
}
}
}
while(Q--)
{
ans = 0;
int tot = 0;
FOR(i, 0, 3) freq[i].clear();
char c = getchar();
FOR(i, 0, N)
{
c = getchar();
int want = c - '0';
if (c == '?') want = 2;
freq[want].PB(N - 1 - i);
}
int mn = 0;
FOR(i, 0, 3) if (SZ(freq[i]) < SZ(freq[mn])) mn = i;
//now solve it in O(2^mn)
if (mn == 2)
{
int idx = 0;
for (int x : freq[1]) idx += (1 << x);
FOR(i, 0, (1 << SZ(freq[2])))
{
int cidx = idx;
FOR(j, 0, SZ(freq[2]))
{
if ((i & (1 << j)))
{
cidx += (1 << freq[2][j]);
}
}
ans += arr[cidx];
}
}
if (mn == 1)
{
int idx = 0;
for (int x : freq[1]) idx += (1 << x);
for (int x : freq[2]) idx += (1 << x);
FOR(i, 0, (1 << SZ(freq[1])))
{
int cidx = idx;
FOR(j, 0, SZ(freq[1]))
{
if ((i & (1 << j))) cidx -= (1 << freq[1][j]);
}
ans += ((__builtin_popcount(i) & 1) ? -1 : 1) * sos[cidx];
}
}
if (mn == 0)
{
int idx = 0;
for (int x : freq[1]) idx += (1 << x);
FOR(i, 0, (1 << SZ(freq[0])))
{
int cidx = idx;
FOR(j, 0, SZ(freq[0]))
{
if (i & (1 << j)) cidx += (1 << freq[0][j]);
}
ans += ((__builtin_popcount(i) & 1) ? -1 : 1) * sob[cidx];
}
}
printi(ans); putchar('\n');
}
return 0;
}
Compilation message (stderr)
snake_escaping.cpp: In function 'int32_t main()':
snake_escaping.cpp:126:7: warning: unused variable 'tot' [-Wunused-variable]
int tot = 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... |