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 fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
vi aa,bb,cc;
int n,q,a[maxn],sub[maxn],super[maxn];
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
int m = (1 << n) - 1;
fo(i,0,m)
{
char t;
cin >> t;
a[i] = t - '0';
sub[i] = a[i];
super[i] = a[i];
}
fo(i,0,n - 1) fod(mask,m,0) if(!bit(mask,i)) super[mask] += super[offbit(mask,i)];
fo(i,0,n - 1) fo(mask,0,m) if(bit(mask,i)) sub[mask] += sub[offbit(mask,i)];
while(q--)
{
string s;
cin >> s;
reverse(all(s));
aa.clear();
bb.clear();
cc.clear();
fo(i,0,ss(s) - 1)
{
if(s[i] == '0') aa.pb(i);
else if(s[i] == '1') bb.pb(i);
else if(s[i] == '?') cc.pb(i);
}
int ans = 0;
if(ss(cc) <= 6)
{
int vv = 0;
for(int it : bb) vv = onbit(vv,it);
fo(mask,0,(1 << ss(cc)) - 1)
{
int v1 = vv;
fo(i,0,ss(cc) - 1) if(bit(mask,i))
{
v1 = onbit(v1,cc[i]);
}
ans += a[v1];
}
}
else if(ss(aa) <= 6)
{
int vv = 0;
for(int it : bb) vv = onbit(vv,it);
fo(mask,0,(1 << ss(aa)) - 1)
{
int v1 = vv;
fo(i,0,ss(aa) - 1) if(bit(mask,i))
{
v1 = onbit(v1,aa[i]);
}
if(bitcount(mask) & 1) ans -= super[v1];
else ans += super[v1];
}
}
else if(ss(bb) <= 6)
{
int vv = 0;
for(int it : cc) vv = onbit(vv,it);
fo(mask,0,(1 << ss(bb)) - 1)
{
int v1 = vv;
fo(i,0,ss(bb) - 1) if(bit(mask,i))
{
v1 = onbit(v1,bb[i]);
}
if(bitcount(mask) % 2 == ss(bb) % 2) ans += sub[v1];
else ans -= sub[v1];
}
}
cout << ans;en;
}
}
Compilation message (stderr)
snake_escaping.cpp:59:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
59 | main()
| ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |