#include<bits/stdc++.h>
#define int long long
#define TASKNAME "snake"
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
#define fi first
#define fast ios_base::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL)
#define se second
#define pb push_back
#define MASK(i) (1LL << i) //2^i
#define BIT(x, i) (((x) >> (i))&1) //lay bit thu i cua i
using namespace std;
typedef pair<int, int> ii;
/**
Cho n = 2^L - 1 va q truy van:
Voi truy van thu i. Ta duoc 1 chuoi str gom ?, 0, 1:
1 mask goi la khop neu khi ta so tung vi tri thi str[i] bang '?' hoac mask[i] == str[i].
subtask 1: L <= 10, n <= 1000.
subtask 2: L <= 13.
subtask 3: L <= 15
subtask 4: Q <= 50000
subtask 5: L <= 20, Q <= 1000000
Gọi X là số dấu ?
Nếu X <= 10 thì ta bruteforce.
Nếu X > 10 thì ta sẽ có tối đa 9 số. Thì ta sẽ dùng bao hàm bù trừ kết hợp dp SOS để tính nhanh.
Rõ ràng là với những dấu ? thì chúng khá là vô dụng tức là những thằng nào
Giả sử ta biết vị trí của những số 1 hay ? thì ta co cach nao lam tot hon khong.
**/
const int MAX = MASK(20);
int dp1[MAX], dp2[MAX], val[MAX];
char query[21];
int lengthBit;
int numQuery;
namespace subtask5{
bool check(){
return true;
}
vector<int> hidden;
vector<int> number1, number0;
int ans = 0, mask1 = 0, mask2 = 0, mask3 = 0;
void calc1(int pos){
if (pos == hidden.size()){
ans += val[mask1];
return;
}
calc1(pos + 1);
mask1 ^= (1LL << hidden[pos]);
calc1(pos + 1);
mask1 ^= (1LL << hidden[pos]);
}
void calc2(int pos, int val){
if (pos == number1.size()){
// printf("%lld %lld %lld\n", dp1[mask2], mask2, val);
ans += dp1[mask2] * ((val&1) ? (-1) : (1));
return;
}
calc2(pos + 1, val + 1);
mask2 ^= (1LL << number1[pos]);
calc2(pos + 1, val);
mask2 ^= (1LL << number1[pos]);
}
void calc3(int pos, int val){
if (pos == number0.size()){
ans += dp2[mask3] * ((val&1) ? (-1) : (1));
return;
}
calc3(pos + 1, val);
mask3 ^= (1LL << number0[pos]);
calc3(pos + 1, val + 1);
mask3 ^= (1LL << number0[pos]);
}
void solve(){
FOR(i, 0, numQuery - 1){
mask1 = 0, mask2 = 0, mask3 = 0, ans = 0;
FORD(j, lengthBit - 1, 0){
cin >> query[j];
if (query[j] == '?') hidden.pb(j), mask2 ^= (1LL << j);
else if (query[j] == '1') number1.pb(j), mask1 ^= (1 << j), mask3 ^= (1 << j);
else number0.pb(j);
}
if (hidden.size() <= lengthBit / 3){
calc1(0);
printf("%lld\n", ans);
}
else if (number1.size() <= lengthBit / 3) {
calc2(0, 0);
printf("%lld\n", ans);
}
else if (number0.size() <= lengthBit / 3){
calc3(0, 0);
printf("%lld\n", ans);
}
number1.clear();
number0.clear();
hidden.clear();
}
}
}
main(){
fast;
if (fopen(TASKNAME".inp","r")){
freopen(TASKNAME".inp","r",stdin);
freopen(TASKNAME".out","w",stdout);
}
cin >> lengthBit >> numQuery;
FOR(i, 0, MASK(lengthBit) - 1){
char c;
cin >> c;
val[i] = (c - '0');
dp1[i] += (c - '0');
dp2[i] += (c - '0');
}
FOR(j, 0, lengthBit - 1){
FOR(i, 0, MASK(lengthBit) - 1){
if (i & (1 << j)) dp1[i] += dp1[i ^ (1 << j)];
}
}
FOR(j, 0, lengthBit - 1){
FORD(i, MASK(lengthBit) - 1, 0){
if (!BIT(i, j)) dp2[i] += dp2[i ^ (1 << j)];
}
}
// FOR(i, 0, MASK(lengthBit) - 1) printf("%lld\n", dp[i]);
if (subtask5::check()) return subtask5::solve(), 0;
}
Compilation message
snake_escaping.cpp: In function 'void subtask5::calc1(long long int)':
snake_escaping.cpp:48:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | if (pos == hidden.size()){
| ~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp: In function 'void subtask5::calc2(long long int, long long int)':
snake_escaping.cpp:61:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if (pos == number1.size()){
| ~~~~^~~~~~~~~~~~~~~~~
snake_escaping.cpp: In function 'void subtask5::calc3(long long int, long long int)':
snake_escaping.cpp:74:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | if (pos == number0.size()){
| ~~~~^~~~~~~~~~~~~~~~~
snake_escaping.cpp: In function 'void subtask5::solve()':
snake_escaping.cpp:92:31: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
92 | if (hidden.size() <= lengthBit / 3){
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp:96:37: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
96 | else if (number1.size() <= lengthBit / 3) {
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp:100:37: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
100 | else if (number0.size() <= lengthBit / 3){
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
snake_escaping.cpp: At global scope:
snake_escaping.cpp:110:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
110 | main(){
| ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
11 |
Correct |
214 ms |
19252 KB |
Output is correct |
12 |
Correct |
241 ms |
18740 KB |
Output is correct |
13 |
Correct |
259 ms |
18004 KB |
Output is correct |
14 |
Correct |
265 ms |
18256 KB |
Output is correct |
15 |
Correct |
248 ms |
19072 KB |
Output is correct |
16 |
Correct |
272 ms |
18408 KB |
Output is correct |
17 |
Correct |
280 ms |
18260 KB |
Output is correct |
18 |
Correct |
147 ms |
20308 KB |
Output is correct |
19 |
Correct |
202 ms |
17148 KB |
Output is correct |
20 |
Correct |
260 ms |
18768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
11 |
Correct |
214 ms |
19252 KB |
Output is correct |
12 |
Correct |
241 ms |
18740 KB |
Output is correct |
13 |
Correct |
259 ms |
18004 KB |
Output is correct |
14 |
Correct |
265 ms |
18256 KB |
Output is correct |
15 |
Correct |
248 ms |
19072 KB |
Output is correct |
16 |
Correct |
272 ms |
18408 KB |
Output is correct |
17 |
Correct |
280 ms |
18260 KB |
Output is correct |
18 |
Correct |
147 ms |
20308 KB |
Output is correct |
19 |
Correct |
202 ms |
17148 KB |
Output is correct |
20 |
Correct |
260 ms |
18768 KB |
Output is correct |
21 |
Correct |
274 ms |
22096 KB |
Output is correct |
22 |
Correct |
311 ms |
22352 KB |
Output is correct |
23 |
Correct |
352 ms |
21456 KB |
Output is correct |
24 |
Correct |
368 ms |
21140 KB |
Output is correct |
25 |
Correct |
330 ms |
23172 KB |
Output is correct |
26 |
Correct |
367 ms |
21592 KB |
Output is correct |
27 |
Correct |
354 ms |
21584 KB |
Output is correct |
28 |
Correct |
169 ms |
24108 KB |
Output is correct |
29 |
Correct |
258 ms |
20308 KB |
Output is correct |
30 |
Correct |
301 ms |
22456 KB |
Output is correct |
31 |
Correct |
350 ms |
22100 KB |
Output is correct |
32 |
Correct |
384 ms |
22100 KB |
Output is correct |
33 |
Correct |
292 ms |
21120 KB |
Output is correct |
34 |
Correct |
390 ms |
21076 KB |
Output is correct |
35 |
Correct |
361 ms |
21588 KB |
Output is correct |
36 |
Correct |
188 ms |
20048 KB |
Output is correct |
37 |
Correct |
262 ms |
22288 KB |
Output is correct |
38 |
Correct |
295 ms |
20496 KB |
Output is correct |
39 |
Correct |
323 ms |
21332 KB |
Output is correct |
40 |
Correct |
354 ms |
21072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
11 |
Correct |
56 ms |
27356 KB |
Output is correct |
12 |
Correct |
57 ms |
27272 KB |
Output is correct |
13 |
Correct |
70 ms |
27216 KB |
Output is correct |
14 |
Correct |
83 ms |
27216 KB |
Output is correct |
15 |
Correct |
61 ms |
27360 KB |
Output is correct |
16 |
Correct |
77 ms |
27216 KB |
Output is correct |
17 |
Correct |
73 ms |
27148 KB |
Output is correct |
18 |
Correct |
51 ms |
27472 KB |
Output is correct |
19 |
Correct |
58 ms |
26964 KB |
Output is correct |
20 |
Correct |
56 ms |
27216 KB |
Output is correct |
21 |
Correct |
65 ms |
27360 KB |
Output is correct |
22 |
Correct |
83 ms |
27088 KB |
Output is correct |
23 |
Correct |
65 ms |
27216 KB |
Output is correct |
24 |
Correct |
84 ms |
27204 KB |
Output is correct |
25 |
Correct |
84 ms |
27224 KB |
Output is correct |
26 |
Correct |
47 ms |
26964 KB |
Output is correct |
27 |
Correct |
53 ms |
27220 KB |
Output is correct |
28 |
Correct |
54 ms |
26960 KB |
Output is correct |
29 |
Correct |
65 ms |
27220 KB |
Output is correct |
30 |
Correct |
83 ms |
27220 KB |
Output is correct |
31 |
Correct |
58 ms |
27220 KB |
Output is correct |
32 |
Correct |
77 ms |
27264 KB |
Output is correct |
33 |
Correct |
71 ms |
27220 KB |
Output is correct |
34 |
Correct |
46 ms |
27180 KB |
Output is correct |
35 |
Correct |
66 ms |
27224 KB |
Output is correct |
36 |
Correct |
77 ms |
27088 KB |
Output is correct |
37 |
Correct |
73 ms |
27216 KB |
Output is correct |
38 |
Correct |
65 ms |
27148 KB |
Output is correct |
39 |
Correct |
65 ms |
27220 KB |
Output is correct |
40 |
Correct |
65 ms |
27216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4572 KB |
Output is correct |
11 |
Correct |
214 ms |
19252 KB |
Output is correct |
12 |
Correct |
241 ms |
18740 KB |
Output is correct |
13 |
Correct |
259 ms |
18004 KB |
Output is correct |
14 |
Correct |
265 ms |
18256 KB |
Output is correct |
15 |
Correct |
248 ms |
19072 KB |
Output is correct |
16 |
Correct |
272 ms |
18408 KB |
Output is correct |
17 |
Correct |
280 ms |
18260 KB |
Output is correct |
18 |
Correct |
147 ms |
20308 KB |
Output is correct |
19 |
Correct |
202 ms |
17148 KB |
Output is correct |
20 |
Correct |
260 ms |
18768 KB |
Output is correct |
21 |
Correct |
274 ms |
22096 KB |
Output is correct |
22 |
Correct |
311 ms |
22352 KB |
Output is correct |
23 |
Correct |
352 ms |
21456 KB |
Output is correct |
24 |
Correct |
368 ms |
21140 KB |
Output is correct |
25 |
Correct |
330 ms |
23172 KB |
Output is correct |
26 |
Correct |
367 ms |
21592 KB |
Output is correct |
27 |
Correct |
354 ms |
21584 KB |
Output is correct |
28 |
Correct |
169 ms |
24108 KB |
Output is correct |
29 |
Correct |
258 ms |
20308 KB |
Output is correct |
30 |
Correct |
301 ms |
22456 KB |
Output is correct |
31 |
Correct |
350 ms |
22100 KB |
Output is correct |
32 |
Correct |
384 ms |
22100 KB |
Output is correct |
33 |
Correct |
292 ms |
21120 KB |
Output is correct |
34 |
Correct |
390 ms |
21076 KB |
Output is correct |
35 |
Correct |
361 ms |
21588 KB |
Output is correct |
36 |
Correct |
188 ms |
20048 KB |
Output is correct |
37 |
Correct |
262 ms |
22288 KB |
Output is correct |
38 |
Correct |
295 ms |
20496 KB |
Output is correct |
39 |
Correct |
323 ms |
21332 KB |
Output is correct |
40 |
Correct |
354 ms |
21072 KB |
Output is correct |
41 |
Correct |
56 ms |
27356 KB |
Output is correct |
42 |
Correct |
57 ms |
27272 KB |
Output is correct |
43 |
Correct |
70 ms |
27216 KB |
Output is correct |
44 |
Correct |
83 ms |
27216 KB |
Output is correct |
45 |
Correct |
61 ms |
27360 KB |
Output is correct |
46 |
Correct |
77 ms |
27216 KB |
Output is correct |
47 |
Correct |
73 ms |
27148 KB |
Output is correct |
48 |
Correct |
51 ms |
27472 KB |
Output is correct |
49 |
Correct |
58 ms |
26964 KB |
Output is correct |
50 |
Correct |
56 ms |
27216 KB |
Output is correct |
51 |
Correct |
65 ms |
27360 KB |
Output is correct |
52 |
Correct |
83 ms |
27088 KB |
Output is correct |
53 |
Correct |
65 ms |
27216 KB |
Output is correct |
54 |
Correct |
84 ms |
27204 KB |
Output is correct |
55 |
Correct |
84 ms |
27224 KB |
Output is correct |
56 |
Correct |
47 ms |
26964 KB |
Output is correct |
57 |
Correct |
53 ms |
27220 KB |
Output is correct |
58 |
Correct |
54 ms |
26960 KB |
Output is correct |
59 |
Correct |
65 ms |
27220 KB |
Output is correct |
60 |
Correct |
83 ms |
27220 KB |
Output is correct |
61 |
Correct |
58 ms |
27220 KB |
Output is correct |
62 |
Correct |
77 ms |
27264 KB |
Output is correct |
63 |
Correct |
71 ms |
27220 KB |
Output is correct |
64 |
Correct |
46 ms |
27180 KB |
Output is correct |
65 |
Correct |
66 ms |
27224 KB |
Output is correct |
66 |
Correct |
77 ms |
27088 KB |
Output is correct |
67 |
Correct |
73 ms |
27216 KB |
Output is correct |
68 |
Correct |
65 ms |
27148 KB |
Output is correct |
69 |
Correct |
65 ms |
27220 KB |
Output is correct |
70 |
Correct |
65 ms |
27216 KB |
Output is correct |
71 |
Correct |
423 ms |
51592 KB |
Output is correct |
72 |
Correct |
493 ms |
51792 KB |
Output is correct |
73 |
Correct |
678 ms |
50260 KB |
Output is correct |
74 |
Correct |
897 ms |
50544 KB |
Output is correct |
75 |
Correct |
533 ms |
52468 KB |
Output is correct |
76 |
Correct |
872 ms |
50772 KB |
Output is correct |
77 |
Correct |
819 ms |
50768 KB |
Output is correct |
78 |
Correct |
286 ms |
54612 KB |
Output is correct |
79 |
Correct |
438 ms |
48468 KB |
Output is correct |
80 |
Correct |
515 ms |
51648 KB |
Output is correct |
81 |
Correct |
684 ms |
51612 KB |
Output is correct |
82 |
Correct |
946 ms |
50564 KB |
Output is correct |
83 |
Correct |
507 ms |
49748 KB |
Output is correct |
84 |
Correct |
1008 ms |
50516 KB |
Output is correct |
85 |
Correct |
826 ms |
50772 KB |
Output is correct |
86 |
Correct |
275 ms |
48464 KB |
Output is correct |
87 |
Correct |
427 ms |
51432 KB |
Output is correct |
88 |
Correct |
437 ms |
48496 KB |
Output is correct |
89 |
Correct |
677 ms |
50080 KB |
Output is correct |
90 |
Correct |
1014 ms |
50516 KB |
Output is correct |
91 |
Correct |
507 ms |
49748 KB |
Output is correct |
92 |
Correct |
941 ms |
51040 KB |
Output is correct |
93 |
Correct |
836 ms |
50768 KB |
Output is correct |
94 |
Correct |
267 ms |
48416 KB |
Output is correct |
95 |
Correct |
716 ms |
50660 KB |
Output is correct |
96 |
Correct |
675 ms |
50648 KB |
Output is correct |
97 |
Correct |
688 ms |
50680 KB |
Output is correct |
98 |
Correct |
697 ms |
50512 KB |
Output is correct |
99 |
Correct |
675 ms |
50648 KB |
Output is correct |
100 |
Correct |
703 ms |
50512 KB |
Output is correct |