Submission #332064

# Submission time Handle Problem Language Result Execution time Memory
332064 2020-12-01T10:50:44 Z AmShZ Snake Escaping (JOI18_snake_escaping) C++11
100 / 100
1462 ms 26768 KB
//khodaya khodet komak kon
# include <bits/stdc++.h>
 
/*
// ordered_set 
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
*/
 
using namespace std;
 
typedef long long                                        ll;
typedef long double                                      ld;
typedef pair <int, int>                                  pii;
typedef pair <pii, int>                                  ppi;
typedef pair <int, pii>                                  pip;
typedef pair <pii, pii>                                  ppp;
typedef pair <ll, ll>                                    pll;
 
# define A                                               first
# define B                                               second
# define endl                                            '\n'
# define sep                                             ' '
# define lc                                              id << 1
# define rc                                              lc | 1
# define all(x)                                          x.begin(), x.end()
# define kill(x)                                         return cout << x << endl, 0
# define SZ(x)                                           int(x.size())
# define InTheNameOfGod                                  ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
 
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
 
const int xn = 21;
const int xm = - 20 + 10;
const int sq = 320;
const ll inf = 1e18 + 10;
const ll INF = 1e18 + 10;
const int mod = 998244353;
const int base = 257;

int n, q, a[1 << xn], cnt[3], ted[1 << xn];
pii dp[1 << xn];
string s;

void solve3(){
	int mask = 0, tot = 0, ans = 0;
	for (int i = 0; i < n; ++ i){
		if (s[i] == '?')
			mask += (1 << i);
		else
			tot += (s[i] - '0') * (1 << i);
	}
	for (int sub = mask; sub > 0; sub = ((sub - 1) & mask))
		ans += a[tot + sub];
	ans += a[tot];
	cout << ans << endl;
}
void solve2(){
	int tot = 0, ans = 0, mask = 0;
	for (int i = 0; i < n; ++ i){
		if (s[i] != '0')
			tot += (1 << i);
		if (s[i] == '1')
			mask += (1 << i);
	}
	ans = dp[tot].A;
	for (int sub = mask; sub > 0; sub = ((sub - 1) & mask)){
		if (ted[sub] % 2)
			ans -= dp[tot - sub].A;
		else
			ans += dp[tot - sub].A;
	}
	cout << ans << endl;
}
void solve1(){
	int tot = 0, ans = 0, mask = 0;
	for (int i = 0; i < n; ++ i){
		if (s[i] == '1')
			tot += (1 << i);
		else if (s[i] == '0')
			mask += (1 << i);
	}
	ans = dp[tot].B;
	for (int sub = mask; sub > 0; sub = ((sub - 1) & mask)){
		if (ted[sub] % 2)
			ans -= dp[tot + sub].B;
		else
			ans += dp[tot + sub].B;
	}
	cout << ans << endl;
}

int main(){
	InTheNameOfGod;
	
	cin >> n >> q >> s;
	for (int i = 0; i < (1 << n); ++ i)
		a[i] = dp[i].A = dp[i].B = s[i] - '0';
	for (int bit = 0; bit < n; ++ bit){
		for (int mask = (1 << n) - 1; mask >= 0; -- mask)
			if ((mask & (1 << bit)))
				dp[mask].A += dp[mask - (1 << bit)].A;
		for (int mask = 0; mask < (1 << n); ++ mask)
			if (!(mask & (1 << bit)))
				dp[mask].B += dp[mask + (1 << bit)].B;
	}
	for (int mask = 1; mask < (1 << n); ++ mask)
		ted[mask] = ted[mask - (mask & - mask)] ^ 1;
	while (q --){
		cin >> s, reverse(all(s));
		cnt[0] = cnt[1] = cnt[2] = 0;
		for (char c : s){
			if (c == '0')
				++ cnt[0];
			else if (c == '1')
				++ cnt[1];
			else
				++ cnt[2];
		}
		if (cnt[0] <= n / 3)
			solve1();
		else if (cnt[1] <= n / 3)
			solve2();
		else
			solve3();
	}
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 284 ms 4844 KB Output is correct
12 Correct 315 ms 4588 KB Output is correct
13 Correct 322 ms 3820 KB Output is correct
14 Correct 305 ms 3692 KB Output is correct
15 Correct 308 ms 4844 KB Output is correct
16 Correct 334 ms 4036 KB Output is correct
17 Correct 367 ms 3820 KB Output is correct
18 Correct 248 ms 5740 KB Output is correct
19 Correct 253 ms 2668 KB Output is correct
20 Correct 317 ms 4716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 284 ms 4844 KB Output is correct
12 Correct 315 ms 4588 KB Output is correct
13 Correct 322 ms 3820 KB Output is correct
14 Correct 305 ms 3692 KB Output is correct
15 Correct 308 ms 4844 KB Output is correct
16 Correct 334 ms 4036 KB Output is correct
17 Correct 367 ms 3820 KB Output is correct
18 Correct 248 ms 5740 KB Output is correct
19 Correct 253 ms 2668 KB Output is correct
20 Correct 317 ms 4716 KB Output is correct
21 Correct 347 ms 5820 KB Output is correct
22 Correct 344 ms 5996 KB Output is correct
23 Correct 398 ms 4964 KB Output is correct
24 Correct 430 ms 4908 KB Output is correct
25 Correct 362 ms 7020 KB Output is correct
26 Correct 468 ms 5292 KB Output is correct
27 Correct 424 ms 5300 KB Output is correct
28 Correct 247 ms 7916 KB Output is correct
29 Correct 284 ms 3764 KB Output is correct
30 Correct 370 ms 5956 KB Output is correct
31 Correct 395 ms 5868 KB Output is correct
32 Correct 424 ms 5812 KB Output is correct
33 Correct 351 ms 4844 KB Output is correct
34 Correct 443 ms 4972 KB Output is correct
35 Correct 429 ms 5256 KB Output is correct
36 Correct 233 ms 3820 KB Output is correct
37 Correct 347 ms 5996 KB Output is correct
38 Correct 319 ms 3820 KB Output is correct
39 Correct 386 ms 5100 KB Output is correct
40 Correct 408 ms 4844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 107 ms 19216 KB Output is correct
12 Correct 98 ms 19088 KB Output is correct
13 Correct 106 ms 19088 KB Output is correct
14 Correct 132 ms 19088 KB Output is correct
15 Correct 92 ms 19216 KB Output is correct
16 Correct 153 ms 19088 KB Output is correct
17 Correct 130 ms 19216 KB Output is correct
18 Correct 76 ms 19344 KB Output is correct
19 Correct 83 ms 18960 KB Output is correct
20 Correct 93 ms 19088 KB Output is correct
21 Correct 107 ms 19164 KB Output is correct
22 Correct 138 ms 19088 KB Output is correct
23 Correct 96 ms 18960 KB Output is correct
24 Correct 125 ms 19088 KB Output is correct
25 Correct 126 ms 19048 KB Output is correct
26 Correct 77 ms 18960 KB Output is correct
27 Correct 87 ms 19088 KB Output is correct
28 Correct 91 ms 18960 KB Output is correct
29 Correct 110 ms 19088 KB Output is correct
30 Correct 116 ms 19088 KB Output is correct
31 Correct 106 ms 18960 KB Output is correct
32 Correct 129 ms 19088 KB Output is correct
33 Correct 116 ms 19088 KB Output is correct
34 Correct 75 ms 18960 KB Output is correct
35 Correct 109 ms 19088 KB Output is correct
36 Correct 110 ms 19088 KB Output is correct
37 Correct 107 ms 19048 KB Output is correct
38 Correct 110 ms 19088 KB Output is correct
39 Correct 108 ms 19088 KB Output is correct
40 Correct 109 ms 19088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 284 ms 4844 KB Output is correct
12 Correct 315 ms 4588 KB Output is correct
13 Correct 322 ms 3820 KB Output is correct
14 Correct 305 ms 3692 KB Output is correct
15 Correct 308 ms 4844 KB Output is correct
16 Correct 334 ms 4036 KB Output is correct
17 Correct 367 ms 3820 KB Output is correct
18 Correct 248 ms 5740 KB Output is correct
19 Correct 253 ms 2668 KB Output is correct
20 Correct 317 ms 4716 KB Output is correct
21 Correct 347 ms 5820 KB Output is correct
22 Correct 344 ms 5996 KB Output is correct
23 Correct 398 ms 4964 KB Output is correct
24 Correct 430 ms 4908 KB Output is correct
25 Correct 362 ms 7020 KB Output is correct
26 Correct 468 ms 5292 KB Output is correct
27 Correct 424 ms 5300 KB Output is correct
28 Correct 247 ms 7916 KB Output is correct
29 Correct 284 ms 3764 KB Output is correct
30 Correct 370 ms 5956 KB Output is correct
31 Correct 395 ms 5868 KB Output is correct
32 Correct 424 ms 5812 KB Output is correct
33 Correct 351 ms 4844 KB Output is correct
34 Correct 443 ms 4972 KB Output is correct
35 Correct 429 ms 5256 KB Output is correct
36 Correct 233 ms 3820 KB Output is correct
37 Correct 347 ms 5996 KB Output is correct
38 Correct 319 ms 3820 KB Output is correct
39 Correct 386 ms 5100 KB Output is correct
40 Correct 408 ms 4844 KB Output is correct
41 Correct 107 ms 19216 KB Output is correct
42 Correct 98 ms 19088 KB Output is correct
43 Correct 106 ms 19088 KB Output is correct
44 Correct 132 ms 19088 KB Output is correct
45 Correct 92 ms 19216 KB Output is correct
46 Correct 153 ms 19088 KB Output is correct
47 Correct 130 ms 19216 KB Output is correct
48 Correct 76 ms 19344 KB Output is correct
49 Correct 83 ms 18960 KB Output is correct
50 Correct 93 ms 19088 KB Output is correct
51 Correct 107 ms 19164 KB Output is correct
52 Correct 138 ms 19088 KB Output is correct
53 Correct 96 ms 18960 KB Output is correct
54 Correct 125 ms 19088 KB Output is correct
55 Correct 126 ms 19048 KB Output is correct
56 Correct 77 ms 18960 KB Output is correct
57 Correct 87 ms 19088 KB Output is correct
58 Correct 91 ms 18960 KB Output is correct
59 Correct 110 ms 19088 KB Output is correct
60 Correct 116 ms 19088 KB Output is correct
61 Correct 106 ms 18960 KB Output is correct
62 Correct 129 ms 19088 KB Output is correct
63 Correct 116 ms 19088 KB Output is correct
64 Correct 75 ms 18960 KB Output is correct
65 Correct 109 ms 19088 KB Output is correct
66 Correct 110 ms 19088 KB Output is correct
67 Correct 107 ms 19048 KB Output is correct
68 Correct 110 ms 19088 KB Output is correct
69 Correct 108 ms 19088 KB Output is correct
70 Correct 109 ms 19088 KB Output is correct
71 Correct 601 ms 24520 KB Output is correct
72 Correct 645 ms 24592 KB Output is correct
73 Correct 981 ms 23056 KB Output is correct
74 Correct 1411 ms 23432 KB Output is correct
75 Correct 582 ms 24872 KB Output is correct
76 Correct 1462 ms 23184 KB Output is correct
77 Correct 1156 ms 23184 KB Output is correct
78 Correct 344 ms 26768 KB Output is correct
79 Correct 475 ms 20752 KB Output is correct
80 Correct 633 ms 24080 KB Output is correct
81 Correct 856 ms 23952 KB Output is correct
82 Correct 1440 ms 23124 KB Output is correct
83 Correct 634 ms 22032 KB Output is correct
84 Correct 1199 ms 23032 KB Output is correct
85 Correct 1074 ms 23056 KB Output is correct
86 Correct 361 ms 20800 KB Output is correct
87 Correct 634 ms 23780 KB Output is correct
88 Correct 566 ms 20852 KB Output is correct
89 Correct 926 ms 22712 KB Output is correct
90 Correct 822 ms 22672 KB Output is correct
91 Correct 660 ms 22032 KB Output is correct
92 Correct 1281 ms 23056 KB Output is correct
93 Correct 1084 ms 22948 KB Output is correct
94 Correct 299 ms 20368 KB Output is correct
95 Correct 919 ms 22544 KB Output is correct
96 Correct 911 ms 22672 KB Output is correct
97 Correct 930 ms 22416 KB Output is correct
98 Correct 910 ms 22416 KB Output is correct
99 Correct 951 ms 22416 KB Output is correct
100 Correct 895 ms 22672 KB Output is correct