Submission #699503

# Submission time Handle Problem Language Result Execution time Memory
699503 2023-02-17T07:59:36 Z badont Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1208 ms 34268 KB
#include<bits/stdc++.h>
using namespace std;

void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }

#ifdef LOCAL
#define debug(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define debug(...) "zzz"
#endif

using ll = long long;
using ld = long double;
using pii = pair<ll,ll>;

#define FOR(i, n) for(int i = 1; i<=n; i++)
#define F0R(i, n) for(int i = 0; i<n; i++)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

template<typename T, typename = void> struct is_iterable : false_type {};
template<typename T> struct is_iterable<T, void_t<decltype(begin(declval<T>())),decltype(end(declval<T>()))>> : true_type {};
template<typename T> typename enable_if<is_iterable<T>::value&&!is_same<T, string>::value,ostream&>::type operator<<(ostream &cout, T const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename T> typename enable_if<is_iterable<T>::value&&!is_same<T, string>::value,ostream&>::type operator<<(ostream &cout, T const &v) {
	cout << "["; 
	for (auto it = v.begin(); it != v.end();) {
		cout << *it;
		if (++it != v.end()) cout << ", ";
	}
	return cout << "]";
}

//var 
ll T;

void solve() {
	ll n, q;
	string a;

	cin >> n >> q >> a;

	debug(n, q, a);

	vector<ll> sub(1 << n), super(1 << n);

	ll big = (1LL << n) - 1;
	F0R (i, 1 << n) {
		sub[i] = a[i] - '0';
		super[i ^ big] = a[i] - '0';
	}

	F0R (i, n) F0R (msk, 1 << n) if ((msk >> i) & 1) {
		sub[msk] += sub[msk ^ (1 << i)];
		super[msk] += super[msk ^ (1 << i)];
	}

	vector new_super = super;
	F0R (msk, 1 << n) {
		new_super[big ^ msk] = super[msk];
	}
	swap(new_super, super);
	new_super.clear();

	//debug(super);
	//debug(super[1]);;

	//debug(super);
	//debug(sub);

	while (q--) {
		string t; cin >> t;
		reverse(all(t));

		int zc = 0, oc = 0, qc = 0;
		for (auto u : t) {
			if (u == '0') {
				zc++;
			} else if (u == '1') {
				oc++;
			} else {
				qc++;
			}
		}

		ll qmsk = 0, omsk = 0, zmsk = 0;
		F0R (i, n) if (t[i] == '?') {
			qmsk |= 1LL << i;
		}

		F0R (i, n) if (t[i] == '1') {
			omsk |= 1LL << i;
		}

		F0R (i, n) if (t[i] == '0') {
			zmsk |= 1LL << i;
		}

		if (qc <= 6) {
			ll ans = 0;
			for (int i = qmsk; i >= 0; i = (i - 1) & qmsk) {
				ans += a[i | omsk] - '0';
				if (i == 0) break;
			}
			cout << ans << '\n';
		} else if (oc <= 6) {
			//zeros fixed (sub)
			ll ans = 0;
			for (int i = omsk; i >= 0; i = (i - 1) & omsk) {
				ll pie = (__builtin_popcount(i) % 2 == __builtin_popcount(omsk) % 2 ? 1 : -1);
				ans += pie * sub[(i | qmsk)];
				if (i == 0) break;
			} 
			cout << ans << '\n';
		} else if (zc <= 6) {
			//ones fixed (super)
			ll ans = 0;
 			for (int i = zmsk; i >= 0; i = (i - 1) & zmsk) {
 				ll pie = (__builtin_popcount(i) % 2 == 0 ? 1 : -1);
 				ans += pie * super[i | omsk];
 				//debug(ans, i | omsk);
 				if (i == 0) break;
			}
			cout << ans << '\n';
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0); 
	cin.tie(0);

	T = 1;
	//cin >> T;
	FOR(t, T)
		solve();

	cout.flush();
	return 0;
}


Compilation message

snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:10:20: warning: statement has no effect [-Wunused-value]
   10 | #define debug(...) "zzz"
      |                    ^~~~~
snake_escaping.cpp:47:2: note: in expansion of macro 'debug'
   47 |  debug(n, q, a);
      |  ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 296 ms 4836 KB Output is correct
12 Correct 315 ms 4364 KB Output is correct
13 Correct 337 ms 3676 KB Output is correct
14 Correct 331 ms 3844 KB Output is correct
15 Correct 367 ms 4684 KB Output is correct
16 Correct 402 ms 3788 KB Output is correct
17 Correct 378 ms 3920 KB Output is correct
18 Correct 186 ms 5628 KB Output is correct
19 Correct 238 ms 2732 KB Output is correct
20 Correct 371 ms 4316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 296 ms 4836 KB Output is correct
12 Correct 315 ms 4364 KB Output is correct
13 Correct 337 ms 3676 KB Output is correct
14 Correct 331 ms 3844 KB Output is correct
15 Correct 367 ms 4684 KB Output is correct
16 Correct 402 ms 3788 KB Output is correct
17 Correct 378 ms 3920 KB Output is correct
18 Correct 186 ms 5628 KB Output is correct
19 Correct 238 ms 2732 KB Output is correct
20 Correct 371 ms 4316 KB Output is correct
21 Correct 566 ms 4840 KB Output is correct
22 Correct 393 ms 4976 KB Output is correct
23 Correct 433 ms 3976 KB Output is correct
24 Correct 424 ms 3824 KB Output is correct
25 Correct 401 ms 5896 KB Output is correct
26 Correct 455 ms 4328 KB Output is correct
27 Correct 460 ms 4288 KB Output is correct
28 Correct 231 ms 6836 KB Output is correct
29 Correct 328 ms 2976 KB Output is correct
30 Correct 479 ms 5060 KB Output is correct
31 Correct 463 ms 4852 KB Output is correct
32 Correct 432 ms 4972 KB Output is correct
33 Correct 374 ms 3692 KB Output is correct
34 Correct 464 ms 3848 KB Output is correct
35 Correct 451 ms 4336 KB Output is correct
36 Correct 220 ms 2800 KB Output is correct
37 Correct 436 ms 4912 KB Output is correct
38 Correct 359 ms 2884 KB Output is correct
39 Correct 426 ms 4056 KB Output is correct
40 Correct 413 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 84 ms 26584 KB Output is correct
12 Correct 86 ms 26592 KB Output is correct
13 Correct 87 ms 26472 KB Output is correct
14 Correct 131 ms 28284 KB Output is correct
15 Correct 86 ms 28440 KB Output is correct
16 Correct 106 ms 28356 KB Output is correct
17 Correct 101 ms 28248 KB Output is correct
18 Correct 61 ms 28500 KB Output is correct
19 Correct 78 ms 28244 KB Output is correct
20 Correct 79 ms 28364 KB Output is correct
21 Correct 87 ms 28368 KB Output is correct
22 Correct 114 ms 28240 KB Output is correct
23 Correct 77 ms 28268 KB Output is correct
24 Correct 85 ms 28240 KB Output is correct
25 Correct 94 ms 28316 KB Output is correct
26 Correct 64 ms 28136 KB Output is correct
27 Correct 73 ms 28340 KB Output is correct
28 Correct 77 ms 28252 KB Output is correct
29 Correct 80 ms 28380 KB Output is correct
30 Correct 83 ms 28264 KB Output is correct
31 Correct 72 ms 28228 KB Output is correct
32 Correct 109 ms 28268 KB Output is correct
33 Correct 99 ms 28268 KB Output is correct
34 Correct 62 ms 28196 KB Output is correct
35 Correct 89 ms 28524 KB Output is correct
36 Correct 85 ms 28316 KB Output is correct
37 Correct 85 ms 28236 KB Output is correct
38 Correct 87 ms 28276 KB Output is correct
39 Correct 88 ms 28240 KB Output is correct
40 Correct 89 ms 28236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 296 ms 4836 KB Output is correct
12 Correct 315 ms 4364 KB Output is correct
13 Correct 337 ms 3676 KB Output is correct
14 Correct 331 ms 3844 KB Output is correct
15 Correct 367 ms 4684 KB Output is correct
16 Correct 402 ms 3788 KB Output is correct
17 Correct 378 ms 3920 KB Output is correct
18 Correct 186 ms 5628 KB Output is correct
19 Correct 238 ms 2732 KB Output is correct
20 Correct 371 ms 4316 KB Output is correct
21 Correct 566 ms 4840 KB Output is correct
22 Correct 393 ms 4976 KB Output is correct
23 Correct 433 ms 3976 KB Output is correct
24 Correct 424 ms 3824 KB Output is correct
25 Correct 401 ms 5896 KB Output is correct
26 Correct 455 ms 4328 KB Output is correct
27 Correct 460 ms 4288 KB Output is correct
28 Correct 231 ms 6836 KB Output is correct
29 Correct 328 ms 2976 KB Output is correct
30 Correct 479 ms 5060 KB Output is correct
31 Correct 463 ms 4852 KB Output is correct
32 Correct 432 ms 4972 KB Output is correct
33 Correct 374 ms 3692 KB Output is correct
34 Correct 464 ms 3848 KB Output is correct
35 Correct 451 ms 4336 KB Output is correct
36 Correct 220 ms 2800 KB Output is correct
37 Correct 436 ms 4912 KB Output is correct
38 Correct 359 ms 2884 KB Output is correct
39 Correct 426 ms 4056 KB Output is correct
40 Correct 413 ms 3832 KB Output is correct
41 Correct 84 ms 26584 KB Output is correct
42 Correct 86 ms 26592 KB Output is correct
43 Correct 87 ms 26472 KB Output is correct
44 Correct 131 ms 28284 KB Output is correct
45 Correct 86 ms 28440 KB Output is correct
46 Correct 106 ms 28356 KB Output is correct
47 Correct 101 ms 28248 KB Output is correct
48 Correct 61 ms 28500 KB Output is correct
49 Correct 78 ms 28244 KB Output is correct
50 Correct 79 ms 28364 KB Output is correct
51 Correct 87 ms 28368 KB Output is correct
52 Correct 114 ms 28240 KB Output is correct
53 Correct 77 ms 28268 KB Output is correct
54 Correct 85 ms 28240 KB Output is correct
55 Correct 94 ms 28316 KB Output is correct
56 Correct 64 ms 28136 KB Output is correct
57 Correct 73 ms 28340 KB Output is correct
58 Correct 77 ms 28252 KB Output is correct
59 Correct 80 ms 28380 KB Output is correct
60 Correct 83 ms 28264 KB Output is correct
61 Correct 72 ms 28228 KB Output is correct
62 Correct 109 ms 28268 KB Output is correct
63 Correct 99 ms 28268 KB Output is correct
64 Correct 62 ms 28196 KB Output is correct
65 Correct 89 ms 28524 KB Output is correct
66 Correct 85 ms 28316 KB Output is correct
67 Correct 85 ms 28236 KB Output is correct
68 Correct 87 ms 28276 KB Output is correct
69 Correct 88 ms 28240 KB Output is correct
70 Correct 89 ms 28236 KB Output is correct
71 Correct 600 ms 31376 KB Output is correct
72 Correct 623 ms 31480 KB Output is correct
73 Correct 691 ms 29960 KB Output is correct
74 Correct 1208 ms 30436 KB Output is correct
75 Correct 613 ms 32364 KB Output is correct
76 Correct 1156 ms 30684 KB Output is correct
77 Correct 955 ms 30548 KB Output is correct
78 Correct 321 ms 34268 KB Output is correct
79 Correct 554 ms 28308 KB Output is correct
80 Correct 641 ms 31596 KB Output is correct
81 Correct 856 ms 31552 KB Output is correct
82 Correct 1168 ms 30460 KB Output is correct
83 Correct 533 ms 29444 KB Output is correct
84 Correct 719 ms 30444 KB Output is correct
85 Correct 984 ms 30552 KB Output is correct
86 Correct 331 ms 28260 KB Output is correct
87 Correct 579 ms 31420 KB Output is correct
88 Correct 558 ms 28468 KB Output is correct
89 Correct 643 ms 30064 KB Output is correct
90 Correct 714 ms 30376 KB Output is correct
91 Correct 569 ms 29440 KB Output is correct
92 Correct 1176 ms 30820 KB Output is correct
93 Correct 955 ms 30664 KB Output is correct
94 Correct 289 ms 28256 KB Output is correct
95 Correct 831 ms 30472 KB Output is correct
96 Correct 822 ms 30524 KB Output is correct
97 Correct 825 ms 30392 KB Output is correct
98 Correct 819 ms 30436 KB Output is correct
99 Correct 827 ms 30376 KB Output is correct
100 Correct 871 ms 30504 KB Output is correct