Submission #330396

# Submission time Handle Problem Language Result Execution time Memory
330396 2020-11-25T05:19:19 Z casperwang Snake Escaping (JOI18_snake_escaping) C++14
0 / 100
1 ms 364 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXL = 20;
const int MAXQ = 1000000;
int L, Q;
string str;
int val[1<<MAXL];
int sum0[1<<MAXL];
int sum1[1<<MAXL];
int cnt[1<<MAXL];
int p0, p1, pq;
int ans;

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> L >> Q;
  cin >> str;
  for (int i = 0; i < (1<<L); i++) {
    val[i] = sum0[i] = sum1[i] = str[i] - '0';
    cnt[i] = cnt[i>>1] + (i&1);
  }
  for (int i = 0; i < L; i++) {
    for (int j = 0; j < (1<<L); j++) {
      if ((1<<i) & j) {
        sum1[j] += sum1[(1<<i) ^ j];
        sum0[(1<<i) ^ j] += sum0[j];
      }
    }
  }
  for (int i = 0; i < Q; i++) {
    cin >> str;
    p0 = p1 = pq = 0;
    for (int j = 0; j < L; j++) {
      if (str[L-1-j] == '0') p0 |= (1<<j);
      if (str[L-1-j] == '1') p1 |= (1<<j);
      if (str[L-1-j] == '?') pq |= (1<<j);
    }
    ans = 0;
    if (cnt[p0] <= 6) {
      for (int j = p0; ; j=(j-1)&p0) {
        if (cnt[j] & 1) ans -= sum0[p1 | j];
        else ans += sum0[p1 | j];
        if (!j) break;
      }
    } else if (cnt[p1] <= 6) {
      for (int j = p1; ; j=(j-1)&p1) {
        if (cnt[p1 ^ j] & 1) ans -= sum1[p0 | j];
        else ans += sum1[p0 | j];
        if (!j) break;
      }
    } else {
      for (int j = pq; ; j=(j-1)&pq) {
        ans += val[p1 | j];
        if (!j) break;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -