Submission #1054806

# Submission time Handle Problem Language Result Execution time Memory
1054806 2024-08-12T12:18:26 Z nima_aryan Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 36340 KB
/**
 *    author:  NimaAryan
 *    created: 2024-08-12 13:06:22  
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  
  int L, Q;
  cin >> L >> Q;

  string S;
  cin >> S;

  vector<int> sub(1 << L);
  for (int i = 0; i < S.size(); ++i) {
    sub[i] = (S[i] - '0');
  }
  for (int i = 0; i < L; ++i) {
    for (int u = (1 << L) - 1; u >= 0; --u) {
      if (u >> i & 1) {
        sub[u] += sub[u ^ (1 << i)];
      }
    }
  }

  vector<int> super(1 << L);
  for (int i = 0; i < S.size(); ++i) {
    super[i] = (S[i] - '0');
  }
  for (int i = 0; i < L; ++i) {
    for (int u = 0; u < (1 << L); ++u) {
      if (!(u >> i & 1)) {
        super[u] += super[u ^ (1 << i)];
      }
    }
  }

  while (Q--) {
    string T;
    cin >> T;
    reverse(T.begin(), T.end());
    int cnt0 = count(T.begin(), T.end(), '0');
    int cnt1 = count(T.begin(), T.end(), '1');
    if (cnt0 <= L / 3) {
      int ans = 0;
      for (int u = 0; u < (1 << cnt0); ++u) {
        int v = 0;
        for (int i = 0, j = 0; i < L; ++i) {
          if (T[i] == '0') {
            if (u >> j & 1) {
              v ^= (1 << i);
            } else {

            }
            ++j;
          } else if (T[i] == '1') {
            v ^= (1 << i);
          } else if (T[i] == '?') {

          }
        }
        ans += (__builtin_popcount(u) % 2 == 0 ? +1 : -1) * super[v];
      }
      cout << ans << "\n";
    } else if (cnt1 <= L / 3) {
      int ans = 0;
      for (int u = 0; u < (1 << cnt1); ++u) {
        int v = 0;
        for (int i = 0, j = 0; i < L; ++i) {
          if (T[i] == '1') {
            if (u >> j & 1) {

            } else {
              v ^= (1 << i) * 1;
            }
            ++j;
          } else if (T[i] == '0') {

          } else if (T[i] == '?') {
            v ^= (1 << i);
          }
        }
        ans += (__builtin_popcount(u) % 2 == 0 ? +1 : -1) * sub[v];
      }
      cout << ans << "\n";
    } else {
      int u = 0;
      for (int i = 0; i < L; ++i) {
        if (T[i] == '?') {
          u ^= (1 << i);
        }
      }
      int ans = 0;
      auto add = [&](int s) {
        int v = 0;
        for (int i = 0; i < L; ++i) {
          if (T[i] == '?') {
            if (s >> i & 1) {
              v ^= (1 << i);
            } else {
              
            }
          } else if (T[i] == '0') {
            
          } else {
            v ^= (1 << i);
          }
        }
        ans += S[v] - '0';
      };
      for (int s = u; s > 0; s = (s - 1) & u) {
        add(s);
      }
      add(0);
      cout << ans << "\n";
    }
  }

  return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:26:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |   for (int i = 0; i < S.size(); ++i) {
      |                   ~~^~~~~~~~~~
snake_escaping.cpp:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |   for (int i = 0; i < S.size(); ++i) {
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 460 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 460 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 97 ms 15152 KB Output is correct
12 Correct 119 ms 14648 KB Output is correct
13 Correct 156 ms 13936 KB Output is correct
14 Correct 210 ms 14148 KB Output is correct
15 Correct 147 ms 15136 KB Output is correct
16 Correct 198 ms 14240 KB Output is correct
17 Correct 183 ms 14160 KB Output is correct
18 Correct 99 ms 16208 KB Output is correct
19 Correct 129 ms 13140 KB Output is correct
20 Correct 104 ms 14764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 460 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 97 ms 15152 KB Output is correct
12 Correct 119 ms 14648 KB Output is correct
13 Correct 156 ms 13936 KB Output is correct
14 Correct 210 ms 14148 KB Output is correct
15 Correct 147 ms 15136 KB Output is correct
16 Correct 198 ms 14240 KB Output is correct
17 Correct 183 ms 14160 KB Output is correct
18 Correct 99 ms 16208 KB Output is correct
19 Correct 129 ms 13140 KB Output is correct
20 Correct 104 ms 14764 KB Output is correct
21 Correct 103 ms 18000 KB Output is correct
22 Correct 182 ms 18260 KB Output is correct
23 Correct 424 ms 17232 KB Output is correct
24 Correct 748 ms 17000 KB Output is correct
25 Correct 342 ms 19028 KB Output is correct
26 Correct 760 ms 17748 KB Output is correct
27 Correct 510 ms 17488 KB Output is correct
28 Correct 108 ms 20040 KB Output is correct
29 Correct 140 ms 16196 KB Output is correct
30 Correct 106 ms 18256 KB Output is correct
31 Correct 441 ms 18008 KB Output is correct
32 Correct 775 ms 18140 KB Output is correct
33 Correct 342 ms 17124 KB Output is correct
34 Correct 562 ms 16976 KB Output is correct
35 Correct 493 ms 17592 KB Output is correct
36 Correct 101 ms 16208 KB Output is correct
37 Correct 102 ms 18012 KB Output is correct
38 Correct 285 ms 16208 KB Output is correct
39 Correct 432 ms 17280 KB Output is correct
40 Correct 757 ms 17108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 460 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 40 ms 12016 KB Output is correct
12 Correct 46 ms 12020 KB Output is correct
13 Correct 100 ms 12020 KB Output is correct
14 Correct 180 ms 12020 KB Output is correct
15 Correct 86 ms 12132 KB Output is correct
16 Correct 185 ms 12068 KB Output is correct
17 Correct 151 ms 12020 KB Output is correct
18 Correct 39 ms 12116 KB Output is correct
19 Correct 43 ms 11760 KB Output is correct
20 Correct 41 ms 12016 KB Output is correct
21 Correct 106 ms 12112 KB Output is correct
22 Correct 180 ms 12016 KB Output is correct
23 Correct 76 ms 11844 KB Output is correct
24 Correct 145 ms 11848 KB Output is correct
25 Correct 121 ms 12020 KB Output is correct
26 Correct 39 ms 11864 KB Output is correct
27 Correct 40 ms 12108 KB Output is correct
28 Correct 57 ms 11824 KB Output is correct
29 Correct 99 ms 12020 KB Output is correct
30 Correct 197 ms 11832 KB Output is correct
31 Correct 79 ms 12016 KB Output is correct
32 Correct 174 ms 11844 KB Output is correct
33 Correct 123 ms 11868 KB Output is correct
34 Correct 39 ms 11860 KB Output is correct
35 Correct 95 ms 11856 KB Output is correct
36 Correct 95 ms 11916 KB Output is correct
37 Correct 96 ms 12016 KB Output is correct
38 Correct 100 ms 12024 KB Output is correct
39 Correct 96 ms 12016 KB Output is correct
40 Correct 96 ms 12020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 460 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 97 ms 15152 KB Output is correct
12 Correct 119 ms 14648 KB Output is correct
13 Correct 156 ms 13936 KB Output is correct
14 Correct 210 ms 14148 KB Output is correct
15 Correct 147 ms 15136 KB Output is correct
16 Correct 198 ms 14240 KB Output is correct
17 Correct 183 ms 14160 KB Output is correct
18 Correct 99 ms 16208 KB Output is correct
19 Correct 129 ms 13140 KB Output is correct
20 Correct 104 ms 14764 KB Output is correct
21 Correct 103 ms 18000 KB Output is correct
22 Correct 182 ms 18260 KB Output is correct
23 Correct 424 ms 17232 KB Output is correct
24 Correct 748 ms 17000 KB Output is correct
25 Correct 342 ms 19028 KB Output is correct
26 Correct 760 ms 17748 KB Output is correct
27 Correct 510 ms 17488 KB Output is correct
28 Correct 108 ms 20040 KB Output is correct
29 Correct 140 ms 16196 KB Output is correct
30 Correct 106 ms 18256 KB Output is correct
31 Correct 441 ms 18008 KB Output is correct
32 Correct 775 ms 18140 KB Output is correct
33 Correct 342 ms 17124 KB Output is correct
34 Correct 562 ms 16976 KB Output is correct
35 Correct 493 ms 17592 KB Output is correct
36 Correct 101 ms 16208 KB Output is correct
37 Correct 102 ms 18012 KB Output is correct
38 Correct 285 ms 16208 KB Output is correct
39 Correct 432 ms 17280 KB Output is correct
40 Correct 757 ms 17108 KB Output is correct
41 Correct 40 ms 12016 KB Output is correct
42 Correct 46 ms 12020 KB Output is correct
43 Correct 100 ms 12020 KB Output is correct
44 Correct 180 ms 12020 KB Output is correct
45 Correct 86 ms 12132 KB Output is correct
46 Correct 185 ms 12068 KB Output is correct
47 Correct 151 ms 12020 KB Output is correct
48 Correct 39 ms 12116 KB Output is correct
49 Correct 43 ms 11760 KB Output is correct
50 Correct 41 ms 12016 KB Output is correct
51 Correct 106 ms 12112 KB Output is correct
52 Correct 180 ms 12016 KB Output is correct
53 Correct 76 ms 11844 KB Output is correct
54 Correct 145 ms 11848 KB Output is correct
55 Correct 121 ms 12020 KB Output is correct
56 Correct 39 ms 11864 KB Output is correct
57 Correct 40 ms 12108 KB Output is correct
58 Correct 57 ms 11824 KB Output is correct
59 Correct 99 ms 12020 KB Output is correct
60 Correct 197 ms 11832 KB Output is correct
61 Correct 79 ms 12016 KB Output is correct
62 Correct 174 ms 11844 KB Output is correct
63 Correct 123 ms 11868 KB Output is correct
64 Correct 39 ms 11860 KB Output is correct
65 Correct 95 ms 11856 KB Output is correct
66 Correct 95 ms 11916 KB Output is correct
67 Correct 96 ms 12016 KB Output is correct
68 Correct 100 ms 12024 KB Output is correct
69 Correct 96 ms 12016 KB Output is correct
70 Correct 96 ms 12020 KB Output is correct
71 Correct 179 ms 36172 KB Output is correct
72 Correct 305 ms 36340 KB Output is correct
73 Correct 1376 ms 34808 KB Output is correct
74 Execution timed out 2049 ms 27708 KB Time limit exceeded
75 Halted 0 ms 0 KB -