제출 #897560

#제출 시각아이디문제언어결과실행 시간메모리
897560avighnaSnake Escaping (JOI18_snake_escaping)C++17
100 / 100
1859 ms47416 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const ll N = 20;

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

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;
    ll ans = 0;
    cnt['1'] = cnt['0'] = cnt['?'] = 0;
    for (auto &i : t) {
      cnt[i]++;
    }
    if (cnt['?'] <= 6) {
      vector<ll> loc;
      ll val = 0;
      for (ll i = 0; i < t.length(); ++i) {
        if (t[i] == '?') {
          loc.push_back(t.length() - i - 1);
        }
        val += (1 << (t.length() - i - 1)) * (t[i] == '1');
      }
      queue<pair<ll, ll>> q;
      q.push({val, -1});
      for (ll i = 0; i < cnt['?']; ++i) {
        ll sz = q.size();
        while (sz--) {
          ll v = q.front().first;
          ll idx = q.front().second + 1;
          q.pop();
          q.push({v, idx});
          v ^= (1 << loc[idx]);
          q.push({v, idx});
        }
      }
      while (!q.empty()) {
        ll v = q.front().first;
        q.pop();
        ans += s[v] - '0';
      }
    } else if (cnt['1'] <= 6) {
      vector<ll> loc;
      ll val = 0;
      for (ll i = 0; i < t.length(); ++i) {
        if (t[i] == '1') {
          loc.push_back(t.length() - i - 1);
        }
        val += (1 << (t.length() - i - 1)) * (t[i] == '1' || t[i] == '?');
      }
      queue<pair<ll, ll>> q;
      q.push({val, -1});
      for (ll i = 0; i < cnt['1']; ++i) {
        ll sz = q.size();
        while (sz--) {
          ll v = q.front().first;
          ll idx = q.front().second + 1;
          q.pop();
          q.push({v, idx});
          v ^= (1 << loc[idx]);
          q.push({v, idx});
        }
      }
      while (!q.empty()) {
        ll v = q.front().first;
        q.pop();
        ans +=
            dp[v] *
            ((__builtin_popcount(v) - cnt['?']) % 2 == cnt['1'] % 2 ? 1 : -1);
      }
    } else {
      vector<ll> loc;
      ll val = 0;
      for (ll i = 0; i < t.length(); ++i) {
        if (t[i] == '0') {
          loc.push_back(t.length() - i - 1);
        }
        val += (1 << (t.length() - i - 1)) * (t[i] == '1');
      }
      queue<pair<ll, ll>> q;
      q.push({val, -1});
      for (ll i = 0; i < cnt['0']; ++i) {
        ll sz = q.size();
        while (sz--) {
          ll v = q.front().first;
          ll idx = q.front().second + 1;
          q.pop();
          q.push({v, idx});
          v ^= (1 << loc[idx]);
          q.push({v, idx});
        }
      }
      while (!q.empty()) {
        ll v = q.front().first;
        q.pop();
        ans += dp2[v] * ((__builtin_popcount(v) - cnt['1']) % 2 == 0 ? 1 : -1);
      }
    }
    cout << ans << "\n";
  }
}

컴파일 시 표준 에러 (stderr) 메시지

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:21: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]
   21 |   for (ll i = 0; i < s.length(); ++i) {
      |                  ~~^~~~~~~~~~~~
snake_escaping.cpp:40:11: warning: array subscript has type 'char' [-Wchar-subscripts]
   40 |       cnt[i]++;
      |           ^
snake_escaping.cpp:45:24: 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]
   45 |       for (ll i = 0; i < t.length(); ++i) {
      |                      ~~^~~~~~~~~~~~
snake_escaping.cpp:72:24: 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]
   72 |       for (ll i = 0; i < t.length(); ++i) {
      |                      ~~^~~~~~~~~~~~
snake_escaping.cpp:101:24: 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]
  101 |       for (ll i = 0; i < t.length(); ++i) {
      |                      ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...