Submission #675737

# Submission time Handle Problem Language Result Execution time Memory
675737 2022-12-27T19:25:21 Z MilosMilutinovic Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 36272 KB
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
#else
	#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

clock_t startTime;
double getCurrentTime() {
	return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

const int L = 22;
const int N = (1 << 22);
int n, q;
char s[N];
char t[N];
int sub[N];
int sup[N];

int main()
{
	startTime = clock();
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	scanf("%d%d", &n, &q);
	scanf("%s", s);
	for (int i = 0; i < (1 << n); i++) {
		sub[i] = (int)(s[i] - '0');
		sup[i] = (int)(s[i] - '0');
	}
	for (int i = 0; i < n; i++)
		for (int mask = 0; mask < (1 << n); mask++)
			if (mask >> i & 1)
				sub[mask] += sub[mask ^ (1 << i)];
	for (int i = 0; i < n; i++)
		for (int mask = (1 << n) - 1; mask >= 0; mask--)
			if (!(mask >> i & 1))
				sup[mask] += sup[mask ^ (1 << i)];
	while(q--) {
		scanf("%s", t);
		reverse(t, t + n);
		vector<int> zero, one, qmark;
		for (int i = 0; i < n; i++) {
			if (t[i] == '0') zero.push_back(i);
			if (t[i] == '1') one.push_back(i);
			if (t[i] == '?') qmark.push_back(i);
		}
		int tmask = 0;
		for (int i = 0; i < n; i++)
			if (t[i] == '1')
				tmask += (1 << i);
		int ans = 0;
		if (qmark.size() <= 6) {
			int sz = qmark.size();
			for (int mask = 0; mask < (1 << sz); mask++) {
				int nmask = tmask;
				for (int i = 0; i < sz; i++)
					if (mask >> i & 1)
						nmask += (1 << qmark[i]);
				ans += (int)(s[nmask] - '0');
			}
		} else if (zero.size() <= 6) {
			int sz = zero.size();
			for (int mask = 0; mask < (1 << sz); mask++) {
				int nmask = tmask;
				for (int i = 0; i < sz; i++)
					if (mask >> i & 1)
						nmask += (1 << zero[i]);
				ans += sup[nmask] * (__builtin_popcount(mask) % 2 == 0 ? 1 : -1);
			}
		} else {
			assert(one.size() <= 6);
			int sz = one.size();
			tmask = 0;
			for (int i = 0; i < n; i++)
				if (t[i] == '?')
					tmask += (1 << i);
			for (int mask = 0; mask < (1 << sz); mask++) {
				int nmask = tmask;
				for (int i = 0; i < sz; i++)
					if (mask >> i & 1)
						nmask += (1 << one[i]);
				ans += sub[nmask] * ((sz - __builtin_popcount(mask)) % 2 == 0 ? 1 : -1);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |  scanf("%d%d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |  scanf("%s", s);
      |  ~~~~~^~~~~~~~~
snake_escaping.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |   scanf("%s", t);
      |   ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 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 2 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 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 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 2 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 340 KB Output is correct
11 Correct 831 ms 4292 KB Output is correct
12 Correct 744 ms 3896 KB Output is correct
13 Correct 643 ms 3148 KB Output is correct
14 Correct 658 ms 3320 KB Output is correct
15 Correct 1212 ms 4256 KB Output is correct
16 Correct 733 ms 3480 KB Output is correct
17 Correct 735 ms 3580 KB Output is correct
18 Correct 361 ms 5276 KB Output is correct
19 Correct 543 ms 2300 KB Output is correct
20 Correct 787 ms 4056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 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 2 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 340 KB Output is correct
11 Correct 831 ms 4292 KB Output is correct
12 Correct 744 ms 3896 KB Output is correct
13 Correct 643 ms 3148 KB Output is correct
14 Correct 658 ms 3320 KB Output is correct
15 Correct 1212 ms 4256 KB Output is correct
16 Correct 733 ms 3480 KB Output is correct
17 Correct 735 ms 3580 KB Output is correct
18 Correct 361 ms 5276 KB Output is correct
19 Correct 543 ms 2300 KB Output is correct
20 Correct 787 ms 4056 KB Output is correct
21 Correct 624 ms 4340 KB Output is correct
22 Correct 961 ms 4464 KB Output is correct
23 Correct 852 ms 3480 KB Output is correct
24 Correct 777 ms 3536 KB Output is correct
25 Correct 654 ms 5328 KB Output is correct
26 Correct 931 ms 3984 KB Output is correct
27 Correct 949 ms 3892 KB Output is correct
28 Correct 366 ms 6408 KB Output is correct
29 Correct 587 ms 2296 KB Output is correct
30 Correct 844 ms 4456 KB Output is correct
31 Correct 1276 ms 4408 KB Output is correct
32 Correct 918 ms 4288 KB Output is correct
33 Correct 685 ms 3404 KB Output is correct
34 Correct 772 ms 3392 KB Output is correct
35 Correct 915 ms 3788 KB Output is correct
36 Correct 351 ms 2440 KB Output is correct
37 Correct 1131 ms 4452 KB Output is correct
38 Correct 610 ms 2268 KB Output is correct
39 Correct 825 ms 3476 KB Output is correct
40 Correct 772 ms 3316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 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 2 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 340 KB Output is correct
11 Correct 99 ms 10568 KB Output is correct
12 Correct 104 ms 11828 KB Output is correct
13 Correct 114 ms 11704 KB Output is correct
14 Correct 146 ms 11700 KB Output is correct
15 Correct 101 ms 11888 KB Output is correct
16 Correct 134 ms 11824 KB Output is correct
17 Correct 127 ms 11724 KB Output is correct
18 Correct 72 ms 11876 KB Output is correct
19 Correct 100 ms 11696 KB Output is correct
20 Correct 96 ms 11776 KB Output is correct
21 Correct 114 ms 11860 KB Output is correct
22 Correct 127 ms 11716 KB Output is correct
23 Correct 98 ms 11696 KB Output is correct
24 Correct 149 ms 11712 KB Output is correct
25 Correct 139 ms 11756 KB Output is correct
26 Correct 72 ms 11596 KB Output is correct
27 Correct 115 ms 11784 KB Output is correct
28 Correct 89 ms 11668 KB Output is correct
29 Correct 116 ms 11724 KB Output is correct
30 Correct 122 ms 11700 KB Output is correct
31 Correct 99 ms 11700 KB Output is correct
32 Correct 146 ms 11744 KB Output is correct
33 Correct 137 ms 11808 KB Output is correct
34 Correct 87 ms 11696 KB Output is correct
35 Correct 120 ms 11744 KB Output is correct
36 Correct 113 ms 11700 KB Output is correct
37 Correct 120 ms 11780 KB Output is correct
38 Correct 111 ms 11724 KB Output is correct
39 Correct 143 ms 11784 KB Output is correct
40 Correct 118 ms 11836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 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 2 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 340 KB Output is correct
11 Correct 831 ms 4292 KB Output is correct
12 Correct 744 ms 3896 KB Output is correct
13 Correct 643 ms 3148 KB Output is correct
14 Correct 658 ms 3320 KB Output is correct
15 Correct 1212 ms 4256 KB Output is correct
16 Correct 733 ms 3480 KB Output is correct
17 Correct 735 ms 3580 KB Output is correct
18 Correct 361 ms 5276 KB Output is correct
19 Correct 543 ms 2300 KB Output is correct
20 Correct 787 ms 4056 KB Output is correct
21 Correct 624 ms 4340 KB Output is correct
22 Correct 961 ms 4464 KB Output is correct
23 Correct 852 ms 3480 KB Output is correct
24 Correct 777 ms 3536 KB Output is correct
25 Correct 654 ms 5328 KB Output is correct
26 Correct 931 ms 3984 KB Output is correct
27 Correct 949 ms 3892 KB Output is correct
28 Correct 366 ms 6408 KB Output is correct
29 Correct 587 ms 2296 KB Output is correct
30 Correct 844 ms 4456 KB Output is correct
31 Correct 1276 ms 4408 KB Output is correct
32 Correct 918 ms 4288 KB Output is correct
33 Correct 685 ms 3404 KB Output is correct
34 Correct 772 ms 3392 KB Output is correct
35 Correct 915 ms 3788 KB Output is correct
36 Correct 351 ms 2440 KB Output is correct
37 Correct 1131 ms 4452 KB Output is correct
38 Correct 610 ms 2268 KB Output is correct
39 Correct 825 ms 3476 KB Output is correct
40 Correct 772 ms 3316 KB Output is correct
41 Correct 99 ms 10568 KB Output is correct
42 Correct 104 ms 11828 KB Output is correct
43 Correct 114 ms 11704 KB Output is correct
44 Correct 146 ms 11700 KB Output is correct
45 Correct 101 ms 11888 KB Output is correct
46 Correct 134 ms 11824 KB Output is correct
47 Correct 127 ms 11724 KB Output is correct
48 Correct 72 ms 11876 KB Output is correct
49 Correct 100 ms 11696 KB Output is correct
50 Correct 96 ms 11776 KB Output is correct
51 Correct 114 ms 11860 KB Output is correct
52 Correct 127 ms 11716 KB Output is correct
53 Correct 98 ms 11696 KB Output is correct
54 Correct 149 ms 11712 KB Output is correct
55 Correct 139 ms 11756 KB Output is correct
56 Correct 72 ms 11596 KB Output is correct
57 Correct 115 ms 11784 KB Output is correct
58 Correct 89 ms 11668 KB Output is correct
59 Correct 116 ms 11724 KB Output is correct
60 Correct 122 ms 11700 KB Output is correct
61 Correct 99 ms 11700 KB Output is correct
62 Correct 146 ms 11744 KB Output is correct
63 Correct 137 ms 11808 KB Output is correct
64 Correct 87 ms 11696 KB Output is correct
65 Correct 120 ms 11744 KB Output is correct
66 Correct 113 ms 11700 KB Output is correct
67 Correct 120 ms 11780 KB Output is correct
68 Correct 111 ms 11724 KB Output is correct
69 Correct 143 ms 11784 KB Output is correct
70 Correct 118 ms 11836 KB Output is correct
71 Correct 975 ms 36132 KB Output is correct
72 Correct 1020 ms 36272 KB Output is correct
73 Correct 1317 ms 34768 KB Output is correct
74 Execution timed out 2008 ms 33324 KB Time limit exceeded
75 Halted 0 ms 0 KB -