제출 #874342

#제출 시각아이디문제언어결과실행 시간메모리
874342tsumondaiSnake Escaping (JOI18_snake_escaping)C++14
100 / 100
881 ms59892 KiB
/*
Siêu tóm tắt: tổng của tất cả các mask theo yêu cầu
*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME  (1.0 * clock() / CLOCKS_PER_SEC)

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;

const int N = 1e6 + 5, L = 21, LMX = (1 << 21) + 5;

const int oo = 1e18, mod = 1e9 + 7;

int n, m, k, l, q, a[LMX], f[LMX], g[LMX];
string s;
vector<int> arr;

void process() {
	cin >> l >> q >> s;
	m = (1 << l);
	for (int i = 0; i < m; ++i) {
		a[i] = s[i] - '0';
	}
	for (int i = 0; i < m; ++i) {
		f[i] = a[i];
		g[i] = a[i];
	}
	for (int j = 0; j < l; ++j) {
		for (int i = 0; i < m; ++i) {
			if ((i >> j) & 1) {
				f[i] += f[i ^ (1 << j)];
				g[i ^ (1 << j)] += g[i];
			}
		}
	}
	while (q--) {
		vector<int> t(3, 0);
		string tt;
		cin >> tt;
		for (int i = l - 1; i >= 0; --i) {
			t[tt[i] == '?' ? 2 : tt[i] - '0'] |= (1 << (l - i - 1));
		}
		if (__builtin_popcount(t[1]) <= 6) {
			int ans = 0;
			int ppc = __builtin_popcount(t[1]);
			for (int z = t[1]; ; z = (z - 1) & t[1]) {
				ans += (((ppc ^ __builtin_popcount(z)) & 1) ? -1 : 1) * f[t[2] | z];
				if (z == 0) {
					break;
				}
			}
			cout << ans << "\n";
		} else if (__builtin_popcount(t[0]) <= 6) {
			int ans = 0;
			int ppc = __builtin_popcount(t[0]);
			for (int z = t[0]; ; z = (z - 1) & t[0]) {
				ans += (((ppc ^ __builtin_popcount(z)) & 1) ? -1 : 1) * g[t[1] ^ t[0] ^ z];
				if (z == 0) {
					break;
				}
			}
			cout << ans << "\n";
		} else {
			assert(__builtin_popcount(t[2]) <= 6);
			int ans = 0;
			for (int z = t[2]; ; z = (z - 1) & t[2]) {
				ans += a[t[1] | z];
				if (z == 0) {
					break;
				}
			}
			cout << ans << "\n";
		}
	}
	return;
}

signed main() {
	cin.tie(0)->sync_with_stdio(false);
	//freopen(".inp", "r", stdin);
	//freopen(".out", "w", stdout);
	process();
	cerr << "Time elapsed: " << __TIME << " s.\n";
	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...