Submission #252193

# Submission time Handle Problem Language Result Execution time Memory
252193 2020-07-24T15:57:23 Z Nightlight Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1654 ms 42312 KB
#include <bits/stdc++.h>
using namespace std;

int L, Q;
char ch;
int A[1 << 20];
int dp0[1 << 20];//1 jadi 1, semua 0 jadi ?
int dp1[1 << 20];//0 jadi 0, semua 1 jadi ?

int main() {
  ios_base::sync_with_stdio(0);
  cin >> L >> Q;
  for(int i = 0; i < (1 << L); i++) {
    cin >> ch;
    A[i] = ch - '0';
    dp0[i] = dp1[i] = A[i];
  }
  for(int i = 0; i < L; i++) {
    for(int mask = 0; mask < (1 << L); mask++) {
      if(mask & (1 << i)) {
        dp1[mask] += dp1[mask ^ (1 << i)];
      }else dp0[mask] += dp0[mask ^ (1 << i)];
    }
  }
//  cout << "-> " << dp0[0] << "\n";
//  cout << "-> " << dp0[(1 << L) - 1] << "\n";
  int mask1, mask0, maskq;
  int cnt1, cnt0, cntq;
  int ans, tmp, bit;
  while(Q--) {
    ans = mask1 = mask0 = maskq = cnt1 = cnt0 = cntq = 0;
    for(int i = L - 1; i >= 0; i--) {
      cin >> ch;
      if(ch == '0') mask0 |= (1 << i), cnt0++;
      if(ch == '1') mask1 |= (1 << i), cnt1++;
      if(ch == '?') maskq |= (1 << i), cntq++;
    }
    if(cntq <= cnt0 && cntq < cnt1) {
      tmp = maskq;
      while(tmp) {
//      	cout << Q << ": " << A[mask1 | tmp] << " " << A[mask1] << "\n";
        ans += A[mask1 | tmp];
        tmp = maskq & (tmp - 1);
      }
      printf("%d\n", ans + A[mask1]);
    }else if(cnt0 < cnt1) {
      tmp = mask0;
      while(tmp) {
        ans += dp0[mask1 | tmp] * (__builtin_popcount(tmp) & 1 ? -1 : 1);
        tmp = mask0 & (tmp - 1);
      }
      printf("%d\n", ans + dp0[mask1]);
    }else {
      tmp = mask1;
      while(tmp) {
        bit = ((((1 << L) - 1) ^ tmp) & (((1 << L) - 1) & mask1));
//        cout << tmp << " " << bit << " " << dp1[maskq | bit] << "\n";
        ans += dp1[maskq | bit] * (__builtin_popcount(bit) & 1 ? 1 : -1) * (__builtin_popcount(mask1) & 1 ? 1 : -1);;
        tmp = mask1 & (tmp - 1);
      }
      printf("%d\n", ans + dp1[maskq + mask1]);
    }
  };
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 377 ms 7260 KB Output is correct
12 Correct 401 ms 14840 KB Output is correct
13 Correct 425 ms 14200 KB Output is correct
14 Correct 451 ms 14200 KB Output is correct
15 Correct 390 ms 15296 KB Output is correct
16 Correct 439 ms 14328 KB Output is correct
17 Correct 438 ms 14328 KB Output is correct
18 Correct 308 ms 16120 KB Output is correct
19 Correct 388 ms 13180 KB Output is correct
20 Correct 407 ms 14968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 377 ms 7260 KB Output is correct
12 Correct 401 ms 14840 KB Output is correct
13 Correct 425 ms 14200 KB Output is correct
14 Correct 451 ms 14200 KB Output is correct
15 Correct 390 ms 15296 KB Output is correct
16 Correct 439 ms 14328 KB Output is correct
17 Correct 438 ms 14328 KB Output is correct
18 Correct 308 ms 16120 KB Output is correct
19 Correct 388 ms 13180 KB Output is correct
20 Correct 407 ms 14968 KB Output is correct
21 Correct 461 ms 18172 KB Output is correct
22 Correct 469 ms 18424 KB Output is correct
23 Correct 538 ms 17604 KB Output is correct
24 Correct 595 ms 17268 KB Output is correct
25 Correct 457 ms 19372 KB Output is correct
26 Correct 539 ms 17784 KB Output is correct
27 Correct 545 ms 17784 KB Output is correct
28 Correct 365 ms 20196 KB Output is correct
29 Correct 456 ms 16312 KB Output is correct
30 Correct 466 ms 18624 KB Output is correct
31 Correct 523 ms 18348 KB Output is correct
32 Correct 588 ms 18424 KB Output is correct
33 Correct 455 ms 17096 KB Output is correct
34 Correct 554 ms 17284 KB Output is correct
35 Correct 542 ms 17784 KB Output is correct
36 Correct 344 ms 16148 KB Output is correct
37 Correct 462 ms 18168 KB Output is correct
38 Correct 451 ms 16120 KB Output is correct
39 Correct 501 ms 17400 KB Output is correct
40 Correct 536 ms 17216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 95 ms 12920 KB Output is correct
12 Correct 84 ms 12920 KB Output is correct
13 Correct 100 ms 12968 KB Output is correct
14 Correct 113 ms 12792 KB Output is correct
15 Correct 87 ms 12920 KB Output is correct
16 Correct 114 ms 12792 KB Output is correct
17 Correct 106 ms 13080 KB Output is correct
18 Correct 73 ms 13048 KB Output is correct
19 Correct 82 ms 12792 KB Output is correct
20 Correct 85 ms 12920 KB Output is correct
21 Correct 93 ms 12920 KB Output is correct
22 Correct 102 ms 12896 KB Output is correct
23 Correct 83 ms 12792 KB Output is correct
24 Correct 107 ms 12792 KB Output is correct
25 Correct 103 ms 12792 KB Output is correct
26 Correct 73 ms 12792 KB Output is correct
27 Correct 81 ms 12908 KB Output is correct
28 Correct 82 ms 12792 KB Output is correct
29 Correct 91 ms 12920 KB Output is correct
30 Correct 91 ms 12792 KB Output is correct
31 Correct 93 ms 12792 KB Output is correct
32 Correct 109 ms 12792 KB Output is correct
33 Correct 105 ms 12968 KB Output is correct
34 Correct 72 ms 12792 KB Output is correct
35 Correct 106 ms 12800 KB Output is correct
36 Correct 94 ms 12820 KB Output is correct
37 Correct 96 ms 12796 KB Output is correct
38 Correct 100 ms 12792 KB Output is correct
39 Correct 98 ms 12920 KB Output is correct
40 Correct 94 ms 12792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 377 ms 7260 KB Output is correct
12 Correct 401 ms 14840 KB Output is correct
13 Correct 425 ms 14200 KB Output is correct
14 Correct 451 ms 14200 KB Output is correct
15 Correct 390 ms 15296 KB Output is correct
16 Correct 439 ms 14328 KB Output is correct
17 Correct 438 ms 14328 KB Output is correct
18 Correct 308 ms 16120 KB Output is correct
19 Correct 388 ms 13180 KB Output is correct
20 Correct 407 ms 14968 KB Output is correct
21 Correct 461 ms 18172 KB Output is correct
22 Correct 469 ms 18424 KB Output is correct
23 Correct 538 ms 17604 KB Output is correct
24 Correct 595 ms 17268 KB Output is correct
25 Correct 457 ms 19372 KB Output is correct
26 Correct 539 ms 17784 KB Output is correct
27 Correct 545 ms 17784 KB Output is correct
28 Correct 365 ms 20196 KB Output is correct
29 Correct 456 ms 16312 KB Output is correct
30 Correct 466 ms 18624 KB Output is correct
31 Correct 523 ms 18348 KB Output is correct
32 Correct 588 ms 18424 KB Output is correct
33 Correct 455 ms 17096 KB Output is correct
34 Correct 554 ms 17284 KB Output is correct
35 Correct 542 ms 17784 KB Output is correct
36 Correct 344 ms 16148 KB Output is correct
37 Correct 462 ms 18168 KB Output is correct
38 Correct 451 ms 16120 KB Output is correct
39 Correct 501 ms 17400 KB Output is correct
40 Correct 536 ms 17216 KB Output is correct
41 Correct 95 ms 12920 KB Output is correct
42 Correct 84 ms 12920 KB Output is correct
43 Correct 100 ms 12968 KB Output is correct
44 Correct 113 ms 12792 KB Output is correct
45 Correct 87 ms 12920 KB Output is correct
46 Correct 114 ms 12792 KB Output is correct
47 Correct 106 ms 13080 KB Output is correct
48 Correct 73 ms 13048 KB Output is correct
49 Correct 82 ms 12792 KB Output is correct
50 Correct 85 ms 12920 KB Output is correct
51 Correct 93 ms 12920 KB Output is correct
52 Correct 102 ms 12896 KB Output is correct
53 Correct 83 ms 12792 KB Output is correct
54 Correct 107 ms 12792 KB Output is correct
55 Correct 103 ms 12792 KB Output is correct
56 Correct 73 ms 12792 KB Output is correct
57 Correct 81 ms 12908 KB Output is correct
58 Correct 82 ms 12792 KB Output is correct
59 Correct 91 ms 12920 KB Output is correct
60 Correct 91 ms 12792 KB Output is correct
61 Correct 93 ms 12792 KB Output is correct
62 Correct 109 ms 12792 KB Output is correct
63 Correct 105 ms 12968 KB Output is correct
64 Correct 72 ms 12792 KB Output is correct
65 Correct 106 ms 12800 KB Output is correct
66 Correct 94 ms 12820 KB Output is correct
67 Correct 96 ms 12796 KB Output is correct
68 Correct 100 ms 12792 KB Output is correct
69 Correct 98 ms 12920 KB Output is correct
70 Correct 94 ms 12792 KB Output is correct
71 Correct 792 ms 39336 KB Output is correct
72 Correct 717 ms 39404 KB Output is correct
73 Correct 986 ms 38264 KB Output is correct
74 Correct 1169 ms 38500 KB Output is correct
75 Correct 714 ms 40284 KB Output is correct
76 Correct 1654 ms 38632 KB Output is correct
77 Correct 1242 ms 38548 KB Output is correct
78 Correct 539 ms 42312 KB Output is correct
79 Correct 764 ms 36232 KB Output is correct
80 Correct 752 ms 39504 KB Output is correct
81 Correct 1053 ms 39356 KB Output is correct
82 Correct 1154 ms 38288 KB Output is correct
83 Correct 743 ms 37496 KB Output is correct
84 Correct 1414 ms 38304 KB Output is correct
85 Correct 1254 ms 38512 KB Output is correct
86 Correct 519 ms 36408 KB Output is correct
87 Correct 707 ms 39284 KB Output is correct
88 Correct 724 ms 36304 KB Output is correct
89 Correct 838 ms 38020 KB Output is correct
90 Correct 935 ms 38408 KB Output is correct
91 Correct 780 ms 37540 KB Output is correct
92 Correct 1306 ms 38636 KB Output is correct
93 Correct 1162 ms 38532 KB Output is correct
94 Correct 522 ms 36584 KB Output is correct
95 Correct 1078 ms 38548 KB Output is correct
96 Correct 1077 ms 38380 KB Output is correct
97 Correct 1063 ms 38492 KB Output is correct
98 Correct 1178 ms 38580 KB Output is correct
99 Correct 1073 ms 38480 KB Output is correct
100 Correct 1052 ms 38548 KB Output is correct