Submission #1361766

#TimeUsernameProblemLanguageResultExecution timeMemory
1361766blackscreen1Snake Escaping (JOI18_snake_escaping)C++20
100 / 100
365 ms34252 KiB
#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;
#define ll long long
#define iloop(m, h) for (auto i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define lloop(m, h) for (auto l = m; l != h; l += (m < h ? 1 : -1))
#define pll pair<ll, ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
ll n, q, a[1048580], dp[1048580], dp2[1048580], c[3], t, cv[3], cans;
string s, s2;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> q;
	cin >> s;
	iloop(0, 1<<n) {a[i] = s[i] - '0'; dp[i] = a[i], dp2[i^((1<<n) - 1)] = a[i];}
	jloop(0, n) iloop(0, 1<<n) if (i&(1<<j)) dp[i] += dp[i^(1<<j)], dp2[i] += dp2[i^(1<<j)];
	while (q--) {
		cin >> s2;
		cv[0] = cv[1] = cv[2] = 0;
		for (auto it : s2) {
			cv[0] <<= 1, cv[1] <<= 1, cv[2] <<= 1;
			cv[(it == '?' ? 2 : it - '0')]++;
		}
		c[0] = c[1] = c[2] = 0;
		for (auto it : s2) c[(it == '?' ? 2 : it - '0')]++;
		t = min({c[0], c[1], c[2]});
		cans = 0;
		if (c[0] == t) {
			cans = dp2[cv[2]];
			for (t = cv[0]; t > 0; t = (t-1)&cv[0]) {
				if (__builtin_popcount(t)%2 == 0) cans += dp2[t^cv[2]];
				else cans -= dp2[t^cv[2]];
			}
		}
		else if (c[1] == t) {
			cans = dp[cv[2]];
			for (t = cv[1]; t > 0; t = (t-1)&cv[1]) {
				if (__builtin_popcount(t)%2 == 0) cans += dp[t^cv[2]];
				else cans -= dp[t^cv[2]];
			}
		}
		else {
			cans = a[cv[1]];
			for (t = cv[2]; t > 0; t = (t-1)&cv[2]) cans += a[t^cv[1]];
		}
		cout << abs(cans) << "\n";
	}
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...