Submission #1144275

#TimeUsernameProblemLanguageResultExecution timeMemory
1144275monkey133Snake Escaping (JOI18_snake_escaping)C++20
22 / 100
1201 ms42148 KiB
#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i) 
#define RFOR(i, x, n) for (ll i =x; i>=n; --i) 
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> piii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


ll n, q;
string toxic, toxic2;
int mask[5000005], three[25], dp[5000005], hh[5000005], ans[5000005];

int main()
{
	three[0] = 1;
	for (ll i=1; i<=20; ++i) three[i] = three[i-1]*3;
	cin >> n >> q >> toxic2;
	toxic = toxic2;
	for (ll i=0; i<(1LL << n); ++i)
	{
		ll newmask = 0;
		for (ll j=0; j<n; ++j) if (i & (1LL << j)) newmask += (1LL << (n-j-1));
		toxic[i] = toxic2[newmask];
		//show2(i, newmask);
	}
	ll m = min(5LL, n-1);
	for (ll i=0; i<(1LL << (n-m));  ++i)
	{
		for (ll j=0; j<n-m; ++j) 
		{
			if ((1LL << j)&i) hh[i] += 2*three[j];
			else hh[i] += three[j];
		}
	}
	for (ll i=0; i<q; ++i)
	{
		string ss;
		cin >> ss;
		for (ll j=0; j<n; ++j)
		{
			if (ss[j]=='0') mask[i] += three[j];
			else if (ss[j]=='1') mask[i] += three[j]*2;
		}
	}
	for (ll pref=0; pref<(1LL << m); ++pref)
	{
		//show(pref);
		for (ll i=0; i<three[n-m] ; ++i) dp[i] = 0;
		for (ll j=0; j<(1LL << (n-m)); ++j)
		{
			ll newmask = hh[j], newmask2 = pref + j * (1LL << m);
			dp[newmask] = ll(toxic[newmask2]-'0');
			//show(ll(toxic[newmask2]-'0'));
		}
		for (ll j=three[n-m]; j>=0; --j)
		{
			ll last = n-m - 1;
			for (ll i=n-m-1; i>=0; --i) if ((j/three[i])%3==0) last = i - 1;
			while (last >= 0)
			{
				ll newmask = j;
				if ((j/three[last])%3==1) newmask -= three[last];
				else newmask -= three[last]*2;
				dp[newmask] += dp[j];
				last--;
			}
		}
		for (ll j=0; j<q; ++j)
		{
			bool fail = 0;
			for (ll i=0; i<m; ++i) 
			{
				if ((mask[j]/three[i])%3 == 1 && (pref&(1LL << i))) {fail = 1; break;}
				if ((mask[j]/three[i])%3 == 2 && (pref&(1LL << i))==0) {fail = 1; break;}	
			}
			if (fail) continue;
			ll newmask = mask[j]/three[m];
			ans[j] += dp[newmask];
			//show2(j, dp[newmask]);
		}
	}
	for (ll i=0; i<q; ++i) cout << ans[i] << endl;
	
	return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...