Submission #952730

#TimeUsernameProblemLanguageResultExecution timeMemory
952730jmyszka2007Snake Escaping (JOI18_snake_escaping)C++17
100 / 100
1308 ms39208 KiB
#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
struct custom_hash {static uint64_t splitmix64(uint64_t x) {x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);}
size_t operator()(uint64_t x) const {static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}};
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#endif
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define pi pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
void solve() {
	//ifstream cin("nazwa.in");
	//ofstream cout("nazwa.out");
	int l, t;
	cin >> l >> t;
	string c;
	cin >> c;
	vi dpdol((1 << l), 0), dpgora((1 << l), 0);
	for(int i = 0; i < (1 << l); i++) {
		dpdol[i] = (c[i] - '0');
		dpgora[i] = (c[i] - '0');
	}
	for(int i = 0; i < l; i++) {
		for(int mask = 0; mask < (1 << l); mask++) {
			if(mask & (1 << i)) {
				dpdol[mask] += dpdol[mask ^ (1 << i)];
			}
			else {
				dpgora[mask] += dpgora[mask ^ (1 << i)];
			}
		}
	}
	while(t--) {
		string a;
		cin >> a;
		reverse(all(a));
		int ilezap = 0, ilezer = 0, ilejed = 0;
		for(auto x : a) {
			if(x == '?') {
				ilezap++;
			}
			else if(x == '0') {
				ilezer++;
			}
			else {
				ilejed++;
			}
		}
		if(min({ilezap, ilezer, ilejed}) == ilezer) {
			vi kt;
			for(int i = 0; i < l; i++) {
				if(a[i] == '0') {
					kt.eb(i);
				}
			}
			int aktmask = 0;
			for(int i = 0; i < l; i++) {
				if(a[i] == '1') {
					aktmask += (1 << i);
				}
			}
			int ans = 0;
			for(int mask = 0; mask < (1 << sz(kt)); mask++) {
				int nmask = aktmask;
				for(int j = 0; j < sz(kt); j++) {
					if(mask & (1 << j)) {
						nmask ^= (1 << kt[j]);
					}
				}
				if(__builtin_popcount(mask) & 1) {
					ans -= dpgora[nmask];
				}
				else {
					ans += dpgora[nmask];
				}
			}
			cout << ans << '\n';
		}
		else if(min({ilezap, ilezer, ilejed}) == ilejed) {
			vi kt;
			for(int i = 0; i < l; i++) {
				if(a[i] == '1') {
					kt.eb(i);
				}
			}
			int aktmask = 0;
			for(int i = 0; i < l; i++) {
				if(a[i] == '1' || a[i] == '?') {
					aktmask += (1 << i);
				}
			}
			int ans = 0;
			for(int mask = 0; mask < (1 << sz(kt)); mask++) {
				int nmask = aktmask;
				for(int j = 0; j < sz(kt); j++) {
					if(mask & (1 << j)) {
						nmask ^= (1 << kt[j]);
					}
				}
				if(__builtin_popcount(mask) & 1) {
					ans -= dpdol[nmask];
				}
				else {
					ans += dpdol[nmask];
				}
			}
			cout << ans << '\n';
		}
		else {
			vi kt;
			for(int i = 0; i < l; i++) {
				if(a[i] == '?') {
					kt.eb(i);
				}
			}
			int aktmask = 0;
			for(int i = 0; i < l; i++) {
				if(a[i] == '1') {
					aktmask += (1 << i);
				}
			}
			int ans = 0;
			for(int mask = 0; mask < (1 << sz(kt)); mask++) {
				int nmask = aktmask;
				for(int j = 0; j < sz(kt); j++) {
					if(mask & (1 << j)) {
						nmask ^= (1 << kt[j]);
					}
				}
				ans += (c[nmask] - '0');
			}
			cout << ans << '\n';
		}
	}
}
int main() {
	fastio();
	int t = 1;
	//cin >> t;
	while(t--) {
		solve();
	}
}
#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...