답안 #897553

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
897553 2024-01-03T11:54:57 Z avighna Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 44616 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const ll N = 20;

ll dp[1 << N], dp2[1 << N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll l, q;
  cin >> l >> q;
  string s;
  cin >> s;

  for (ll i = 0; i < s.length(); ++i) {
    dp[i] = dp2[i] = s[i] - '0';
  }
  for (ll i = 0; i < N; ++i) {
    for (ll mask = 0; mask < (1 << N); ++mask) {
      if (mask & (1 << i)) {
        dp[mask] += dp[mask ^ (1 << i)];
      } else {
        dp2[mask] += dp2[mask ^ (1 << i)];
      }
    }
  }

  while (q--) {
    string t;
    cin >> t;
    vector<ll> cnt(256);
    ll ans = 0;
    for (auto &i : t) {
      cnt[i]++;
    }
    if (cnt['?'] <= 6) {
      for (ll mask = 0; mask < (1 << cnt['?']); ++mask) {
        ll val = 0;
        ll m = mask;
        ll p = 1;
        for (ll i = t.length() - 1; i >= 0; --i) {
          char c = t[i];
          if (c == '?') {
            c = m % 2 + '0';
            m /= 2;
          }
          val += (c - '0') * p;
          p *= 2;
        }
        ans += s[val] - '0';
      }
    } else if (cnt['1'] <= 6) {
      for (ll mask = 0; mask < (1 << cnt['1']); ++mask) {
        ll val = 0;
        ll m = mask;
        ll p = 1;
        for (ll i = t.length() - 1; i >= 0; --i) {
          char c = t[i];
          if (c == '1') {
            c = m % 2 + '0';
            m /= 2;
          }
          if (c == '?') {
            c = '1';
          }
          val += (c - '0') * p;
          p *= 2;
        }
        ans +=
            dp[val] * (__builtin_popcount(mask) % 2 == cnt['1'] % 2 ? 1 : -1);
      }
    } else {
      for (ll mask = 0; mask < (1 << cnt['0']); ++mask) {
        ll val = 0;
        ll m = mask;
        ll p = 1;
        for (ll i = t.length() - 1; i >= 0; --i) {
          char c = t[i];
          if (c == '0') {
            c = m % 2 + '0';
            m /= 2;
          }
          if (c == '?') {
            c = '0';
          }
          val += (c - '0') * p;
          p *= 2;
        }
        ans += dp2[val] * (__builtin_popcount(mask) % 2 == 0 ? 1 : -1);
      }
    }
    cout << ans << "\n";
  }
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:20:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |   for (ll i = 0; i < s.length(); ++i) {
      |                  ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 16728 KB Output is correct
2 Correct 36 ms 16976 KB Output is correct
3 Correct 27 ms 16732 KB Output is correct
4 Correct 28 ms 17112 KB Output is correct
5 Correct 28 ms 16728 KB Output is correct
6 Correct 27 ms 16728 KB Output is correct
7 Correct 28 ms 16732 KB Output is correct
8 Correct 36 ms 16876 KB Output is correct
9 Correct 29 ms 16728 KB Output is correct
10 Correct 29 ms 16728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 16728 KB Output is correct
2 Correct 36 ms 16976 KB Output is correct
3 Correct 27 ms 16732 KB Output is correct
4 Correct 28 ms 17112 KB Output is correct
5 Correct 28 ms 16728 KB Output is correct
6 Correct 27 ms 16728 KB Output is correct
7 Correct 28 ms 16732 KB Output is correct
8 Correct 36 ms 16876 KB Output is correct
9 Correct 29 ms 16728 KB Output is correct
10 Correct 29 ms 16728 KB Output is correct
11 Correct 720 ms 31596 KB Output is correct
12 Correct 696 ms 31516 KB Output is correct
13 Correct 409 ms 30456 KB Output is correct
14 Correct 380 ms 30404 KB Output is correct
15 Correct 1095 ms 31768 KB Output is correct
16 Correct 562 ms 30568 KB Output is correct
17 Correct 535 ms 30620 KB Output is correct
18 Correct 253 ms 32596 KB Output is correct
19 Correct 241 ms 29380 KB Output is correct
20 Correct 724 ms 31120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 16728 KB Output is correct
2 Correct 36 ms 16976 KB Output is correct
3 Correct 27 ms 16732 KB Output is correct
4 Correct 28 ms 17112 KB Output is correct
5 Correct 28 ms 16728 KB Output is correct
6 Correct 27 ms 16728 KB Output is correct
7 Correct 28 ms 16732 KB Output is correct
8 Correct 36 ms 16876 KB Output is correct
9 Correct 29 ms 16728 KB Output is correct
10 Correct 29 ms 16728 KB Output is correct
11 Correct 720 ms 31596 KB Output is correct
12 Correct 696 ms 31516 KB Output is correct
13 Correct 409 ms 30456 KB Output is correct
14 Correct 380 ms 30404 KB Output is correct
15 Correct 1095 ms 31768 KB Output is correct
16 Correct 562 ms 30568 KB Output is correct
17 Correct 535 ms 30620 KB Output is correct
18 Correct 253 ms 32596 KB Output is correct
19 Correct 241 ms 29380 KB Output is correct
20 Correct 724 ms 31120 KB Output is correct
21 Correct 1533 ms 34392 KB Output is correct
22 Correct 764 ms 34596 KB Output is correct
23 Correct 657 ms 33988 KB Output is correct
24 Correct 652 ms 33448 KB Output is correct
25 Correct 583 ms 35524 KB Output is correct
26 Correct 807 ms 33876 KB Output is correct
27 Correct 772 ms 34008 KB Output is correct
28 Correct 274 ms 36580 KB Output is correct
29 Correct 288 ms 32340 KB Output is correct
30 Correct 1129 ms 34512 KB Output is correct
31 Correct 1422 ms 34688 KB Output is correct
32 Correct 946 ms 34376 KB Output is correct
33 Correct 463 ms 33792 KB Output is correct
34 Correct 656 ms 33464 KB Output is correct
35 Correct 770 ms 34008 KB Output is correct
36 Correct 270 ms 32340 KB Output is correct
37 Correct 1417 ms 34392 KB Output is correct
38 Correct 271 ms 32340 KB Output is correct
39 Correct 666 ms 33784 KB Output is correct
40 Correct 657 ms 33476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 16728 KB Output is correct
2 Correct 36 ms 16976 KB Output is correct
3 Correct 27 ms 16732 KB Output is correct
4 Correct 28 ms 17112 KB Output is correct
5 Correct 28 ms 16728 KB Output is correct
6 Correct 27 ms 16728 KB Output is correct
7 Correct 28 ms 16732 KB Output is correct
8 Correct 36 ms 16876 KB Output is correct
9 Correct 29 ms 16728 KB Output is correct
10 Correct 29 ms 16728 KB Output is correct
11 Correct 44 ms 20212 KB Output is correct
12 Correct 45 ms 20284 KB Output is correct
13 Correct 76 ms 20212 KB Output is correct
14 Correct 142 ms 20212 KB Output is correct
15 Correct 67 ms 20216 KB Output is correct
16 Correct 137 ms 20312 KB Output is correct
17 Correct 104 ms 20212 KB Output is correct
18 Correct 39 ms 20464 KB Output is correct
19 Correct 38 ms 19952 KB Output is correct
20 Correct 52 ms 20576 KB Output is correct
21 Correct 105 ms 20308 KB Output is correct
22 Correct 146 ms 20212 KB Output is correct
23 Correct 61 ms 20052 KB Output is correct
24 Correct 119 ms 20112 KB Output is correct
25 Correct 116 ms 20064 KB Output is correct
26 Correct 43 ms 19952 KB Output is correct
27 Correct 42 ms 20312 KB Output is correct
28 Correct 41 ms 20060 KB Output is correct
29 Correct 83 ms 21236 KB Output is correct
30 Correct 118 ms 20212 KB Output is correct
31 Correct 61 ms 19984 KB Output is correct
32 Correct 146 ms 20216 KB Output is correct
33 Correct 106 ms 20052 KB Output is correct
34 Correct 39 ms 19956 KB Output is correct
35 Correct 87 ms 20216 KB Output is correct
36 Correct 83 ms 20224 KB Output is correct
37 Correct 94 ms 20108 KB Output is correct
38 Correct 97 ms 20464 KB Output is correct
39 Correct 82 ms 20212 KB Output is correct
40 Correct 83 ms 20208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 16728 KB Output is correct
2 Correct 36 ms 16976 KB Output is correct
3 Correct 27 ms 16732 KB Output is correct
4 Correct 28 ms 17112 KB Output is correct
5 Correct 28 ms 16728 KB Output is correct
6 Correct 27 ms 16728 KB Output is correct
7 Correct 28 ms 16732 KB Output is correct
8 Correct 36 ms 16876 KB Output is correct
9 Correct 29 ms 16728 KB Output is correct
10 Correct 29 ms 16728 KB Output is correct
11 Correct 720 ms 31596 KB Output is correct
12 Correct 696 ms 31516 KB Output is correct
13 Correct 409 ms 30456 KB Output is correct
14 Correct 380 ms 30404 KB Output is correct
15 Correct 1095 ms 31768 KB Output is correct
16 Correct 562 ms 30568 KB Output is correct
17 Correct 535 ms 30620 KB Output is correct
18 Correct 253 ms 32596 KB Output is correct
19 Correct 241 ms 29380 KB Output is correct
20 Correct 724 ms 31120 KB Output is correct
21 Correct 1533 ms 34392 KB Output is correct
22 Correct 764 ms 34596 KB Output is correct
23 Correct 657 ms 33988 KB Output is correct
24 Correct 652 ms 33448 KB Output is correct
25 Correct 583 ms 35524 KB Output is correct
26 Correct 807 ms 33876 KB Output is correct
27 Correct 772 ms 34008 KB Output is correct
28 Correct 274 ms 36580 KB Output is correct
29 Correct 288 ms 32340 KB Output is correct
30 Correct 1129 ms 34512 KB Output is correct
31 Correct 1422 ms 34688 KB Output is correct
32 Correct 946 ms 34376 KB Output is correct
33 Correct 463 ms 33792 KB Output is correct
34 Correct 656 ms 33464 KB Output is correct
35 Correct 770 ms 34008 KB Output is correct
36 Correct 270 ms 32340 KB Output is correct
37 Correct 1417 ms 34392 KB Output is correct
38 Correct 271 ms 32340 KB Output is correct
39 Correct 666 ms 33784 KB Output is correct
40 Correct 657 ms 33476 KB Output is correct
41 Correct 44 ms 20212 KB Output is correct
42 Correct 45 ms 20284 KB Output is correct
43 Correct 76 ms 20212 KB Output is correct
44 Correct 142 ms 20212 KB Output is correct
45 Correct 67 ms 20216 KB Output is correct
46 Correct 137 ms 20312 KB Output is correct
47 Correct 104 ms 20212 KB Output is correct
48 Correct 39 ms 20464 KB Output is correct
49 Correct 38 ms 19952 KB Output is correct
50 Correct 52 ms 20576 KB Output is correct
51 Correct 105 ms 20308 KB Output is correct
52 Correct 146 ms 20212 KB Output is correct
53 Correct 61 ms 20052 KB Output is correct
54 Correct 119 ms 20112 KB Output is correct
55 Correct 116 ms 20064 KB Output is correct
56 Correct 43 ms 19952 KB Output is correct
57 Correct 42 ms 20312 KB Output is correct
58 Correct 41 ms 20060 KB Output is correct
59 Correct 83 ms 21236 KB Output is correct
60 Correct 118 ms 20212 KB Output is correct
61 Correct 61 ms 19984 KB Output is correct
62 Correct 146 ms 20216 KB Output is correct
63 Correct 106 ms 20052 KB Output is correct
64 Correct 39 ms 19956 KB Output is correct
65 Correct 87 ms 20216 KB Output is correct
66 Correct 83 ms 20224 KB Output is correct
67 Correct 94 ms 20108 KB Output is correct
68 Correct 97 ms 20464 KB Output is correct
69 Correct 82 ms 20212 KB Output is correct
70 Correct 83 ms 20208 KB Output is correct
71 Correct 409 ms 44496 KB Output is correct
72 Correct 482 ms 44616 KB Output is correct
73 Correct 1111 ms 43032 KB Output is correct
74 Execution timed out 2075 ms 39780 KB Time limit exceeded
75 Halted 0 ms 0 KB -