Submission #957950

#TimeUsernameProblemLanguageResultExecution timeMemory
957950BhavayGoyalSnake Escaping (JOI18_snake_escaping)C++14
100 / 100
1188 ms38796 KiB
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define ll long long #define ld long double #define ar array #define vi vector<int> #define vii vector<vector<int>> #define pii pair<int, int> #define pb push_back #define all(x) x.begin(), x.end() #define f first #define s second #define endl "\n" const int MOD = 1e9+7; const int inf = 1e9; const ll linf = 1e18; const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1}; const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1}; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // -------------------------------------------------- Main Code -------------------------------------------------- const int N = (1<<20)+5; int n, q, arr[N], dp[N], dp2[N]; void sol() { cin >> n >> q; for (int i = 0; i < (1<<n); i++) {char x; cin >> x; arr[i] = x-'0';} for (int i = 0; i < (1<<n); i++) dp[i] = arr[i], dp2[i] = arr[i]; for (int i = 0; i < n; i++) { for (int mask = 0; mask < (1<<n); mask++) { if (mask&(1<<i)) dp[mask] += dp[mask^(1<<i)]; if ((mask&(1<<i)) == 0) dp2[mask] += dp2[mask^(1<<i)]; } } while (q--) { string s; cin >> s; int one = 0, zero = 0, question = 0; for (int i = 0; i < n; i++) { one += s[i]=='1'; zero += s[i]=='0'; question += s[i]=='?'; } if (question <= 6) { vi temp; int num = 0; reverse(all(s)); for (int i = 0; i < n; i++) { if (s[i] == '?') temp.pb(i); if (s[i] == '1') num += (1<<i); } int ans = 0; for (int i = 0; i < (1<<temp.size()); i++) { int tempNum = num; for (int j = 0; j < temp.size(); j++) { if (i&(1<<j)) { tempNum ^= (1<<temp[j]); } } ans += arr[tempNum]; } cout << ans << endl; } else if (one <= 6) { vi temp; int num = 0; reverse(all(s)); for (int i = 0; i < n; i++) { if (s[i] == '1') temp.pb(i); if (s[i] == '1' || s[i] == '?') num += (1<<i); } int ans = 0; for (int i = 0; i < (1<<temp.size()); i++) { int tempNum = num, ct = 0; for (int j = 0; j < temp.size(); j++) { if (i&(1<<j)) { tempNum ^= (1<<temp[j]); ct++; } } if (ct%2 != 0) ans -= dp[tempNum]; else ans += dp[tempNum]; } cout << ans << endl; } else { vi temp; int num = 0; reverse(all(s)); for (int i = 0; i < n; i++) { if (s[i] == '0') temp.pb(i); if (s[i] == '1') num += (1<<i); } int ans = 0; for (int i = 0; i < (1<<temp.size()); i++) { int tempNum = num, ct = 0; for (int j = 0; j < temp.size(); j++) { if (!(i&(1<<j))) { tempNum ^= (1<<temp[j]); ct++; } } if (ct%2 != 0) ans -= dp2[tempNum]; else ans += dp2[tempNum]; } cout << ans << endl; } } } int main () { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) { sol(); } return 0; }

Compilation message (stderr)

snake_escaping.cpp: In function 'void sol()':
snake_escaping.cpp:72:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |                 for (int j = 0; j < temp.size(); j++) {
      |                                 ~~^~~~~~~~~~~~~
snake_escaping.cpp:93:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |                 for (int j = 0; j < temp.size(); j++) {
      |                                 ~~^~~~~~~~~~~~~
snake_escaping.cpp:117:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |                 for (int j = 0; j < temp.size(); j++) {
      |                                 ~~^~~~~~~~~~~~~
#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...