Submission #949451

#TimeUsernameProblemLanguageResultExecution timeMemory
949451mansurSnake Escaping (JOI18_snake_escaping)C++17
100 / 100
1502 ms36536 KiB
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3") #include<bits/stdc++.h> using namespace std; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define sz(a) (int)a.size() #define s second #define f first using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); vector<pii> rid = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; vector<pii> dir = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; const int N = 2e5 + 1, mod = 1e9 + 7; const ll inf = 1e9; double eps = 1e-15; bool flg = 0; int dp[2][(1 << 20)]; int f(int x, int mk, vector<int> &s) { int ans = dp[x][mk]; for (int msk = 1; msk < (1 << sz(s)); msk++) { int mask = mk; for (int i = 0; i < sz(s); i++) { if (msk >> i & 1) mask += (1 << s[i]); } if ((__builtin_popcount(msk)) & 1) ans -= dp[x][mask]; else ans += dp[x][mask]; } return ans; } void slv() { int l, q; cin >> l >> q; string s; cin >> s; for (int i = 0; i < sz(s); i++) { int m0 = 0, m1 = 0; for (int b = 0; b < l; b++) { if (i >> b & 1) m1 += (1 << b); else m0 += (1 << b); } dp[0][m0] += s[i] - '0'; dp[1][m1] += s[i] - '0'; } for (int i = 0; i < l; i++) { for (int msk = 0; msk < (1 << l); msk++) { if (!(msk >> i & 1)) { dp[1][msk] += dp[1][msk + (1 << i)]; dp[0][msk] += dp[0][msk + (1 << i)]; } } } while (q--) { string h; cin >> h; vector<int> p, p0, p1; int msk0 = 0, msk1 = 0; for (int i = 0; i < sz(h); i++) { if (h[i] == '0') { p0.push_back(sz(h) - i - 1); msk0 += (1 << p0.back()); } if (h[i] == '1') { p1.push_back(sz(h) - i - 1); msk1 += (1 << p1.back()); } if (h[i] == '?') p.push_back(sz(h) - i - 1); } if (sz(p) < 7) { int ans = 0; for (int mask = 0; mask < (1 << sz(p)); mask++) { int msk = msk1; for (int i = 0; i < sz(p); i++) { if (mask >> i & 1) msk += (1 << p[i]); } ans += s[msk] - '0'; } cout << ans << '\n'; continue; } if (sz(p0) > sz(p1)) cout << f(0, msk0, p1) << '\n'; else cout << f(1, msk1, p0) << '\n'; } } main() { //freopen("rsq.in", "r", stdin); //freopen("rsq.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); int tp = 1; if (flg) cin >> tp; while (tp--) { slv(); } } //wenomechainsama

Compilation message (stderr)

snake_escaping.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  101 | main() {
      | ^~~~
#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...