답안 #332060

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332060 2020-12-01T10:28:46 Z AmShZ Snake Escaping (JOI18_snake_escaping) C++11
100 / 100
1918 ms 33400 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];
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)){
		int ted = __builtin_popcount(sub);
		if (ted % 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)){
		int ted = __builtin_popcount(sub);
		if (ted % 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;
	}
	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] < 7)
			solve1();
		else if (cnt[1] < 7)
			solve2();
		else
			solve3();
	}
	
	return 0;
}
# 결과 실행 시간 메모리 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 1 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
# 결과 실행 시간 메모리 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 1 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 283 ms 14444 KB Output is correct
12 Correct 392 ms 13932 KB Output is correct
13 Correct 452 ms 13036 KB Output is correct
14 Correct 419 ms 13164 KB Output is correct
15 Correct 308 ms 14188 KB Output is correct
16 Correct 374 ms 13292 KB Output is correct
17 Correct 428 ms 13644 KB Output is correct
18 Correct 239 ms 15212 KB Output is correct
19 Correct 387 ms 12140 KB Output is correct
20 Correct 305 ms 13804 KB Output is correct
# 결과 실행 시간 메모리 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 1 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 283 ms 14444 KB Output is correct
12 Correct 392 ms 13932 KB Output is correct
13 Correct 452 ms 13036 KB Output is correct
14 Correct 419 ms 13164 KB Output is correct
15 Correct 308 ms 14188 KB Output is correct
16 Correct 374 ms 13292 KB Output is correct
17 Correct 428 ms 13644 KB Output is correct
18 Correct 239 ms 15212 KB Output is correct
19 Correct 387 ms 12140 KB Output is correct
20 Correct 305 ms 13804 KB Output is correct
21 Correct 352 ms 16472 KB Output is correct
22 Correct 432 ms 16620 KB Output is correct
23 Correct 642 ms 15512 KB Output is correct
24 Correct 502 ms 15084 KB Output is correct
25 Correct 379 ms 17260 KB Output is correct
26 Correct 492 ms 16012 KB Output is correct
27 Correct 500 ms 15476 KB Output is correct
28 Correct 262 ms 18028 KB Output is correct
29 Correct 613 ms 14360 KB Output is correct
30 Correct 357 ms 16108 KB Output is correct
31 Correct 438 ms 16176 KB Output is correct
32 Correct 448 ms 15596 KB Output is correct
33 Correct 403 ms 14616 KB Output is correct
34 Correct 509 ms 14880 KB Output is correct
35 Correct 495 ms 15084 KB Output is correct
36 Correct 256 ms 13676 KB Output is correct
37 Correct 335 ms 15480 KB Output is correct
38 Correct 515 ms 13420 KB Output is correct
39 Correct 452 ms 14720 KB Output is correct
40 Correct 472 ms 14328 KB Output is correct
# 결과 실행 시간 메모리 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 1 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 85 ms 16144 KB Output is correct
12 Correct 91 ms 16216 KB Output is correct
13 Correct 134 ms 16144 KB Output is correct
14 Correct 147 ms 16016 KB Output is correct
15 Correct 89 ms 16144 KB Output is correct
16 Correct 159 ms 16092 KB Output is correct
17 Correct 137 ms 16016 KB Output is correct
18 Correct 75 ms 16272 KB Output is correct
19 Correct 83 ms 16016 KB Output is correct
20 Correct 91 ms 16144 KB Output is correct
21 Correct 107 ms 16144 KB Output is correct
22 Correct 151 ms 16016 KB Output is correct
23 Correct 94 ms 16016 KB Output is correct
24 Correct 145 ms 16016 KB Output is correct
25 Correct 128 ms 16016 KB Output is correct
26 Correct 74 ms 16016 KB Output is correct
27 Correct 88 ms 16144 KB Output is correct
28 Correct 87 ms 16016 KB Output is correct
29 Correct 114 ms 16016 KB Output is correct
30 Correct 103 ms 16016 KB Output is correct
31 Correct 95 ms 16144 KB Output is correct
32 Correct 144 ms 16144 KB Output is correct
33 Correct 133 ms 16272 KB Output is correct
34 Correct 73 ms 16016 KB Output is correct
35 Correct 112 ms 16160 KB Output is correct
36 Correct 113 ms 16084 KB Output is correct
37 Correct 114 ms 16144 KB Output is correct
38 Correct 115 ms 16016 KB Output is correct
39 Correct 115 ms 16144 KB Output is correct
40 Correct 115 ms 16144 KB Output is correct
# 결과 실행 시간 메모리 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 1 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 283 ms 14444 KB Output is correct
12 Correct 392 ms 13932 KB Output is correct
13 Correct 452 ms 13036 KB Output is correct
14 Correct 419 ms 13164 KB Output is correct
15 Correct 308 ms 14188 KB Output is correct
16 Correct 374 ms 13292 KB Output is correct
17 Correct 428 ms 13644 KB Output is correct
18 Correct 239 ms 15212 KB Output is correct
19 Correct 387 ms 12140 KB Output is correct
20 Correct 305 ms 13804 KB Output is correct
21 Correct 352 ms 16472 KB Output is correct
22 Correct 432 ms 16620 KB Output is correct
23 Correct 642 ms 15512 KB Output is correct
24 Correct 502 ms 15084 KB Output is correct
25 Correct 379 ms 17260 KB Output is correct
26 Correct 492 ms 16012 KB Output is correct
27 Correct 500 ms 15476 KB Output is correct
28 Correct 262 ms 18028 KB Output is correct
29 Correct 613 ms 14360 KB Output is correct
30 Correct 357 ms 16108 KB Output is correct
31 Correct 438 ms 16176 KB Output is correct
32 Correct 448 ms 15596 KB Output is correct
33 Correct 403 ms 14616 KB Output is correct
34 Correct 509 ms 14880 KB Output is correct
35 Correct 495 ms 15084 KB Output is correct
36 Correct 256 ms 13676 KB Output is correct
37 Correct 335 ms 15480 KB Output is correct
38 Correct 515 ms 13420 KB Output is correct
39 Correct 452 ms 14720 KB Output is correct
40 Correct 472 ms 14328 KB Output is correct
41 Correct 85 ms 16144 KB Output is correct
42 Correct 91 ms 16216 KB Output is correct
43 Correct 134 ms 16144 KB Output is correct
44 Correct 147 ms 16016 KB Output is correct
45 Correct 89 ms 16144 KB Output is correct
46 Correct 159 ms 16092 KB Output is correct
47 Correct 137 ms 16016 KB Output is correct
48 Correct 75 ms 16272 KB Output is correct
49 Correct 83 ms 16016 KB Output is correct
50 Correct 91 ms 16144 KB Output is correct
51 Correct 107 ms 16144 KB Output is correct
52 Correct 151 ms 16016 KB Output is correct
53 Correct 94 ms 16016 KB Output is correct
54 Correct 145 ms 16016 KB Output is correct
55 Correct 128 ms 16016 KB Output is correct
56 Correct 74 ms 16016 KB Output is correct
57 Correct 88 ms 16144 KB Output is correct
58 Correct 87 ms 16016 KB Output is correct
59 Correct 114 ms 16016 KB Output is correct
60 Correct 103 ms 16016 KB Output is correct
61 Correct 95 ms 16144 KB Output is correct
62 Correct 144 ms 16144 KB Output is correct
63 Correct 133 ms 16272 KB Output is correct
64 Correct 73 ms 16016 KB Output is correct
65 Correct 112 ms 16160 KB Output is correct
66 Correct 113 ms 16084 KB Output is correct
67 Correct 114 ms 16144 KB Output is correct
68 Correct 115 ms 16016 KB Output is correct
69 Correct 115 ms 16144 KB Output is correct
70 Correct 115 ms 16144 KB Output is correct
71 Correct 593 ms 32784 KB Output is correct
72 Correct 657 ms 33400 KB Output is correct
73 Correct 1133 ms 32132 KB Output is correct
74 Correct 1869 ms 32832 KB Output is correct
75 Correct 615 ms 20748 KB Output is correct
76 Correct 1918 ms 19084 KB Output is correct
77 Correct 1378 ms 18996 KB Output is correct
78 Correct 347 ms 22672 KB Output is correct
79 Correct 472 ms 16656 KB Output is correct
80 Correct 620 ms 19788 KB Output is correct
81 Correct 991 ms 19808 KB Output is correct
82 Correct 1765 ms 18684 KB Output is correct
83 Correct 661 ms 17936 KB Output is correct
84 Correct 1511 ms 18832 KB Output is correct
85 Correct 1321 ms 18960 KB Output is correct
86 Correct 348 ms 16604 KB Output is correct
87 Correct 591 ms 19728 KB Output is correct
88 Correct 555 ms 16528 KB Output is correct
89 Correct 1034 ms 18704 KB Output is correct
90 Correct 776 ms 18704 KB Output is correct
91 Correct 703 ms 17880 KB Output is correct
92 Correct 1633 ms 18960 KB Output is correct
93 Correct 1363 ms 19044 KB Output is correct
94 Correct 333 ms 16528 KB Output is correct
95 Correct 1114 ms 18620 KB Output is correct
96 Correct 1122 ms 32272 KB Output is correct
97 Correct 1095 ms 18960 KB Output is correct
98 Correct 1088 ms 18832 KB Output is correct
99 Correct 1086 ms 18764 KB Output is correct
100 Correct 1105 ms 18704 KB Output is correct