# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
45520 | reality | Snake Escaping (JOI18_snake_escaping) | C++17 | 991 ms | 65536 KiB |
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;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
int D0[1 << 20];
int D1[1 << 20];
char str[1 << 20];
int coef[1 << 20];
char s[64];
int main(void)
{
int n,q;
scanf("%d%d%s",&n,&q,str);
const int N = 1 << n;
for (int i = 0;i < N;++i)
str[i] -= '0';
for (int i = 0;i < N;++i)
coef[i] = __builtin_popcount(i) & 1 ? -1 : 1;
for (int i = 0;i < N;++i)
D0[i] = D1[i] = str[i];
for (int i = 0;i < n;++i)
for (int mask = 0;mask < N;++mask)
if ((mask >> i) & 1)
D0[mask ^ (1 << i)] += D0[mask];
else
D1[mask ^ (1 << i)] += D1[mask];
while (q --) {
scanf("%s",s);
reverse(s,s + n);
int a = 0,b = 0,c = 0;
int la = 0,lb = 0,lc = 0;
for (int i = 0;i < n;++i) {
if (s[i] == '0')
a |= 1 << i,++la;
else
if (s[i] == '1')
b |= 1 << i,++lb;
else
c |= 1 << i,++lc;
}
int ans = 0;
if (la <= lb && la <= lc) {
for (int mask = a;mask >= 0;mask = !mask ? -1 : ((mask - 1) & a))
ans += coef[mask] * D0[(N - 1) ^ mask ^ c];
} else
if (lb <= la && lb <= lc) {
for (int mask = b;mask >= 0;mask = !mask ? -1 : ((mask - 1) & b))
ans += coef[mask] * D1[mask ^ c];
} else {
for (int mask = c;mask >= 0;mask = !mask ? -1 : ((mask - 1) & c))
ans += str[mask ^ b];
}
ans = abs(ans);
printf("%d\n",ans);
}
return 0;
}
Compilation message (stderr)
# | 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... |