Submission #101827

#TimeUsernameProblemLanguageResultExecution timeMemory
101827KCSCSnake Escaping (JOI18_snake_escaping)C++14
100 / 100
1161 ms43076 KiB
#include <bits/stdc++.h> using namespace std; const int DIM = 20; char str[1 << DIM], aux[DIM]; int udp[1 << DIM], ddp[1 << DIM], cbit[1 << DIM]; int main(void) { #ifdef HOME freopen("snake.in", "r", stdin); freopen("snake.out", "w", stdout); #endif int n, q; scanf("%d %d\n%s\n", &n, &q, str); for (int msk = 0; msk < (1 << n); ++msk) { cbit[msk] = cbit[msk >> 1] + (msk & 1); udp[msk] = ddp[msk] = str[msk] - '0'; } for (int b = 0; b < n; ++b) { for (int msk = 0; msk < (1 << n); ++msk) { if (msk & (1 << b)) { udp[msk ^ (1 << b)] += udp[msk]; } else { ddp[msk ^ (1 << b)] += ddp[msk]; } } } while (q--) { int msk0 = 0, msk1 = 0, mskq = 0; scanf("%s\n", aux); reverse(aux, aux + n); for (int i = 0; i < n; ++i) { if (aux[i] == '0') { msk0 ^= (1 << i); } if (aux[i] == '1') { msk1 ^= (1 << i); } if (aux[i] == '?') { mskq ^= (1 << i); } } int ans = 0; if (cbit[mskq] <= 7) { for (int axm = mskq, nr = (1 << cbit[mskq]); nr; (--axm) &= mskq, --nr) { ans += str[axm ^ msk1] - '0'; } } else if (cbit[msk0] <= 7) { for (int axm = msk0, nr = (1 << cbit[msk0]); nr; (--axm) &= msk0, --nr) { ans += udp[msk1 ^ axm] * ((cbit[axm] & 1) ? -1 : 1); } } else if (cbit[msk1] <= 7) { for (int axm = msk1, nr = (1 << cbit[msk1]); nr; (--axm) &= msk1, --nr) { ans += ddp[mskq ^ msk1 ^ axm] * ((cbit[axm] & 1) ? -1 : 1); } } printf("%d\n", ans); } return 0; }

Compilation message (stderr)

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:14:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n, q; scanf("%d %d\n%s\n", &n, &q, str);
            ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:26:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s\n", aux); reverse(aux, aux + n);
   ~~~~~^~~~~~~~~~~~~
#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...