Submission #252194

# Submission time Handle Problem Language Result Execution time Memory
252194 2020-07-24T16:00:08 Z Nightlight Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 18256 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() {
  scanf("%d %d", &L, &Q);
  for(int i = 0; i < (1 << L); i++) {
    scanf(" %c", &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--) {
      scanf(" %c", &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);
        tmp = mask1 & (tmp - 1);
      }
      printf("%d\n", ans * (__builtin_popcount(mask1) & 1 ? 1 : -1) + dp1[maskq + mask1]);
    }
  };
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:11:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &L, &Q);
   ~~~~~^~~~~~~~~~~~~~~~~
snake_escaping.cpp:13:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf(" %c", &ch);
     ~~~~~^~~~~~~~~~~~
snake_escaping.cpp:32:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf(" %c", &ch);
       ~~~~~^~~~~~~~~~~~
# 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 1 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 1 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 758 ms 7668 KB Output is correct
12 Correct 764 ms 13688 KB Output is correct
13 Correct 812 ms 12832 KB Output is correct
14 Correct 789 ms 13048 KB Output is correct
15 Correct 792 ms 14088 KB Output is correct
16 Correct 811 ms 13176 KB Output is correct
17 Correct 794 ms 13176 KB Output is correct
18 Correct 752 ms 15056 KB Output is correct
19 Correct 782 ms 12156 KB Output is correct
20 Correct 800 ms 13648 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 1 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 758 ms 7668 KB Output is correct
12 Correct 764 ms 13688 KB Output is correct
13 Correct 812 ms 12832 KB Output is correct
14 Correct 789 ms 13048 KB Output is correct
15 Correct 792 ms 14088 KB Output is correct
16 Correct 811 ms 13176 KB Output is correct
17 Correct 794 ms 13176 KB Output is correct
18 Correct 752 ms 15056 KB Output is correct
19 Correct 782 ms 12156 KB Output is correct
20 Correct 800 ms 13648 KB Output is correct
21 Correct 975 ms 14056 KB Output is correct
22 Correct 1018 ms 14300 KB Output is correct
23 Correct 1039 ms 13432 KB Output is correct
24 Correct 1008 ms 13132 KB Output is correct
25 Correct 988 ms 15096 KB Output is correct
26 Correct 1006 ms 13560 KB Output is correct
27 Correct 1010 ms 13688 KB Output is correct
28 Correct 947 ms 16120 KB Output is correct
29 Correct 970 ms 12280 KB Output is correct
30 Correct 987 ms 14328 KB Output is correct
31 Correct 1025 ms 14428 KB Output is correct
32 Correct 1073 ms 14456 KB Output is correct
33 Correct 984 ms 13304 KB Output is correct
34 Correct 1029 ms 13316 KB Output is correct
35 Correct 1002 ms 13892 KB Output is correct
36 Correct 939 ms 12536 KB Output is correct
37 Correct 960 ms 14712 KB Output is correct
38 Correct 966 ms 12548 KB Output is correct
39 Correct 1000 ms 13948 KB Output is correct
40 Correct 1004 ms 13816 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 1 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 165 ms 13564 KB Output is correct
12 Correct 166 ms 13688 KB Output is correct
13 Correct 169 ms 13560 KB Output is correct
14 Correct 198 ms 13688 KB Output is correct
15 Correct 170 ms 13688 KB Output is correct
16 Correct 194 ms 13560 KB Output is correct
17 Correct 190 ms 13688 KB Output is correct
18 Correct 161 ms 13816 KB Output is correct
19 Correct 162 ms 13432 KB Output is correct
20 Correct 164 ms 13688 KB Output is correct
21 Correct 174 ms 13688 KB Output is correct
22 Correct 188 ms 13688 KB Output is correct
23 Correct 164 ms 13508 KB Output is correct
24 Correct 184 ms 13560 KB Output is correct
25 Correct 187 ms 13688 KB Output is correct
26 Correct 159 ms 13432 KB Output is correct
27 Correct 164 ms 13688 KB Output is correct
28 Correct 172 ms 13460 KB Output is correct
29 Correct 177 ms 13560 KB Output is correct
30 Correct 192 ms 13560 KB Output is correct
31 Correct 173 ms 13688 KB Output is correct
32 Correct 220 ms 13564 KB Output is correct
33 Correct 204 ms 13560 KB Output is correct
34 Correct 163 ms 13432 KB Output is correct
35 Correct 189 ms 13560 KB Output is correct
36 Correct 188 ms 13560 KB Output is correct
37 Correct 190 ms 13560 KB Output is correct
38 Correct 187 ms 13560 KB Output is correct
39 Correct 183 ms 13628 KB Output is correct
40 Correct 177 ms 13568 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 1 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 758 ms 7668 KB Output is correct
12 Correct 764 ms 13688 KB Output is correct
13 Correct 812 ms 12832 KB Output is correct
14 Correct 789 ms 13048 KB Output is correct
15 Correct 792 ms 14088 KB Output is correct
16 Correct 811 ms 13176 KB Output is correct
17 Correct 794 ms 13176 KB Output is correct
18 Correct 752 ms 15056 KB Output is correct
19 Correct 782 ms 12156 KB Output is correct
20 Correct 800 ms 13648 KB Output is correct
21 Correct 975 ms 14056 KB Output is correct
22 Correct 1018 ms 14300 KB Output is correct
23 Correct 1039 ms 13432 KB Output is correct
24 Correct 1008 ms 13132 KB Output is correct
25 Correct 988 ms 15096 KB Output is correct
26 Correct 1006 ms 13560 KB Output is correct
27 Correct 1010 ms 13688 KB Output is correct
28 Correct 947 ms 16120 KB Output is correct
29 Correct 970 ms 12280 KB Output is correct
30 Correct 987 ms 14328 KB Output is correct
31 Correct 1025 ms 14428 KB Output is correct
32 Correct 1073 ms 14456 KB Output is correct
33 Correct 984 ms 13304 KB Output is correct
34 Correct 1029 ms 13316 KB Output is correct
35 Correct 1002 ms 13892 KB Output is correct
36 Correct 939 ms 12536 KB Output is correct
37 Correct 960 ms 14712 KB Output is correct
38 Correct 966 ms 12548 KB Output is correct
39 Correct 1000 ms 13948 KB Output is correct
40 Correct 1004 ms 13816 KB Output is correct
41 Correct 165 ms 13564 KB Output is correct
42 Correct 166 ms 13688 KB Output is correct
43 Correct 169 ms 13560 KB Output is correct
44 Correct 198 ms 13688 KB Output is correct
45 Correct 170 ms 13688 KB Output is correct
46 Correct 194 ms 13560 KB Output is correct
47 Correct 190 ms 13688 KB Output is correct
48 Correct 161 ms 13816 KB Output is correct
49 Correct 162 ms 13432 KB Output is correct
50 Correct 164 ms 13688 KB Output is correct
51 Correct 174 ms 13688 KB Output is correct
52 Correct 188 ms 13688 KB Output is correct
53 Correct 164 ms 13508 KB Output is correct
54 Correct 184 ms 13560 KB Output is correct
55 Correct 187 ms 13688 KB Output is correct
56 Correct 159 ms 13432 KB Output is correct
57 Correct 164 ms 13688 KB Output is correct
58 Correct 172 ms 13460 KB Output is correct
59 Correct 177 ms 13560 KB Output is correct
60 Correct 192 ms 13560 KB Output is correct
61 Correct 173 ms 13688 KB Output is correct
62 Correct 220 ms 13564 KB Output is correct
63 Correct 204 ms 13560 KB Output is correct
64 Correct 163 ms 13432 KB Output is correct
65 Correct 189 ms 13560 KB Output is correct
66 Correct 188 ms 13560 KB Output is correct
67 Correct 190 ms 13560 KB Output is correct
68 Correct 187 ms 13560 KB Output is correct
69 Correct 183 ms 13628 KB Output is correct
70 Correct 177 ms 13568 KB Output is correct
71 Correct 1608 ms 18116 KB Output is correct
72 Correct 1606 ms 18256 KB Output is correct
73 Correct 1726 ms 16632 KB Output is correct
74 Execution timed out 2021 ms 17060 KB Time limit exceeded
75 Halted 0 ms 0 KB -