Submission #970551

#TimeUsernameProblemLanguageResultExecution timeMemory
970551mychecksedadSnake Escaping (JOI18_snake_escaping)C++17
100 / 100
599 ms41484 KiB
/* Author : Mychecksdead  */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 2e6+100, M = 1e5+10, K = 13, MX = 30;

int n, q, dp[N], dp2[N];
string s;
void solve(){
  cin >> n >> q >> s;
  for(int i = 0; i < (1<<n); ++i) dp[i] = dp2[i] = s[i] - '0';
  for(int i = 0; i < n; ++i){
    for(int mask = 0; mask < (1<<n); ++mask){
      if(((1<<i)&mask) == 0) dp[mask] += dp[(1<<i) ^ mask];
      if(((1<<i)&mask)) dp2[mask] += dp2[(1<<i) ^ mask];
    }
  }
  for(int i = 0; i < q; ++i){
    string t; cin >> t;
    int A = 0, B = 0, C = 0;
    for(int j = 0; j < n; ++j){
      if(t[n-j-1] == '0') A += (1<<j);
      if(t[n-j-1] == '1') B += (1<<j);
      if(t[n-j-1] == '?') C += (1<<j);
    }
    if(__builtin_popcount(C) <= 6){
      int ans = 0;
      for(int S = C; S > 0; S = (S-1)&C){
        ans += s[S | B] - '0';
      }
      ans += s[B] - '0';
      cout << ans << '\n';
    }else if(__builtin_popcount(A) <= 6){
      int ans = 0;
      for(int S = A; S > 0; S = (S-1)&A){
        ans += dp[B | S] * (__builtin_popcount(S) % 2 == 0 ? 1 : -1);
      }
      ans += dp[B];
      cout << ans << '\n';
    }else{
      int ans = 0;
      for(int S = B; S > 0; S = (S-1)&B){
        ans += dp2[S | C] * ((__builtin_popcount(B) - (int)__builtin_popcount(S)) % 2 == 0 ? 1 : -1);
      }
      ans += dp2[C] * ((__builtin_popcount(B)) % 2 == 0 ? 1 : -1);
      cout << ans << '\n';
    }
  }

}



int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tt = 1, aa;
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // cin >> tt;
  while(tt--){
    solve();
    // en;
  }
  cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
  return 0;
} 

Compilation message (stderr)

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:61:15: warning: unused variable 'aa' [-Wunused-variable]
   61 |   int tt = 1, aa;
      |               ^~
#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...