Submission #1049351

# Submission time Handle Problem Language Result Execution time Memory
1049351 2024-08-08T16:53:02 Z vjudge1 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
747 ms 47600 KB
#include<bits/stdc++.h>
 
using namespace std;
 
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
 
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
 
const ld PI = 3.1415926535;
const int N = 2e6+10;
const int M = 7e6 + 1;
int mod = 1e9+7;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 2;
 
int mult(int a, int b) {
    return a * 1LL * b % mod;
}
 
int sum(int a, int b) { 
    if (a + b < 0)
        return a + b + mod;
    if (a + b >= mod)
        return a + b - mod;
    return a + b;
}
 
ll binpow(ll a, ll n) {
    if (n == 0)
        return 1;
    if (n % 2 == 1) {
        return binpow(a, n - 1) * a % mod;
    } else {
        ll b = binpow(a, n / 2);
        return b * b % mod;
    }
}
 
int n, q, a[N], sos[N], sou[N];
 
void solve(){
    cin >> n >> q;
    string tmp;
    cin >> tmp;
    for(int i = 0; i < (1 << n); i++)
        sos[i] = sou[i] = a[i] = tmp[i] - '0';
    //sum over subsets
    for(int i = 0; i < n; i++)
        for(int mask = 0; mask < (1 << n); mask++)
            if(mask & (1 << i))
                sos[mask] += sos[mask ^ (1 << i)];
    for(int i = 0; i < n; i++)
        for(int mask = (1 << n) - 1; mask >= 0; mask--)
            if(!(mask & (1 << i)))
                sou[mask] += sou[mask ^ (1 << i)];
    while(q--){
        vector<int> und = {}, zer = {}, one = {};
        string s;
        cin >> s;
        reverse(all(s));
        int mainm = 0, unmask = 0, zermask = 0;
        for(int i = 0; i < sz(s); i++){
            if(s[i] == '?')
                und.pb(i), unmask += (1 << i);
            if(s[i] == '0')
                zer.pb(i), zermask += (1 << i);
            if(s[i] == '1')
                one.pb(i), mainm += (1 << i);
        }
        if(sz(und) <= sz(zer) && sz(und) <= sz(one)){
            int ans = a[mainm];
            for(int mask = unmask; mask > 0; mask = (mask - 1) & unmask){
                ans += a[mask + mainm];
            }
            cout << ans << '\n';
        }else if(sz(zer) <= sz(und) && sz(one) >= sz(zer)){
            int ans = sou[mainm];
            for(int mask = zermask; mask > 0; mask = (mask - 1) & zermask){
                int cnt = __builtin_popcount(mask);
                if(cnt % 2)
                    ans -= sou[mask + mainm];
                else
                    ans += sou[mask + mainm];
            }
            cout << ans << '\n';
        }else{
            int ans = 0;
            if(sz(one) % 2)
                ans -= sos[unmask];
            else
                ans += sos[unmask];
            for(int mask = mainm; mask > 0; mask = (mask - 1) & mainm){
                int cnt = sz(one) - __builtin_popcount(mask);
                if(cnt % 2)
                    ans -= sos[mask + unmask];
                else
                    ans += sos[mask + unmask];
            }
            cout << ans << '\n';
        }
    }
}
 
signed main() {
    mispertion;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 273 ms 19156 KB Output is correct
12 Correct 252 ms 19000 KB Output is correct
13 Correct 284 ms 18120 KB Output is correct
14 Correct 312 ms 18260 KB Output is correct
15 Correct 272 ms 19100 KB Output is correct
16 Correct 307 ms 18512 KB Output is correct
17 Correct 307 ms 18280 KB Output is correct
18 Correct 168 ms 20352 KB Output is correct
19 Correct 245 ms 17212 KB Output is correct
20 Correct 272 ms 18772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 273 ms 19156 KB Output is correct
12 Correct 252 ms 19000 KB Output is correct
13 Correct 284 ms 18120 KB Output is correct
14 Correct 312 ms 18260 KB Output is correct
15 Correct 272 ms 19100 KB Output is correct
16 Correct 307 ms 18512 KB Output is correct
17 Correct 307 ms 18280 KB Output is correct
18 Correct 168 ms 20352 KB Output is correct
19 Correct 245 ms 17212 KB Output is correct
20 Correct 272 ms 18772 KB Output is correct
21 Correct 280 ms 22100 KB Output is correct
22 Correct 292 ms 22612 KB Output is correct
23 Correct 356 ms 21328 KB Output is correct
24 Correct 347 ms 21144 KB Output is correct
25 Correct 315 ms 23096 KB Output is correct
26 Correct 360 ms 21840 KB Output is correct
27 Correct 374 ms 21724 KB Output is correct
28 Correct 198 ms 24148 KB Output is correct
29 Correct 272 ms 20216 KB Output is correct
30 Correct 286 ms 22360 KB Output is correct
31 Correct 367 ms 22332 KB Output is correct
32 Correct 447 ms 22096 KB Output is correct
33 Correct 314 ms 21116 KB Output is correct
34 Correct 375 ms 21528 KB Output is correct
35 Correct 387 ms 21584 KB Output is correct
36 Correct 174 ms 20092 KB Output is correct
37 Correct 293 ms 22132 KB Output is correct
38 Correct 297 ms 20308 KB Output is correct
39 Correct 340 ms 21304 KB Output is correct
40 Correct 355 ms 21328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 37 ms 20464 KB Output is correct
12 Correct 39 ms 20464 KB Output is correct
13 Correct 44 ms 20208 KB Output is correct
14 Correct 65 ms 20684 KB Output is correct
15 Correct 42 ms 20472 KB Output is correct
16 Correct 50 ms 20464 KB Output is correct
17 Correct 51 ms 21232 KB Output is correct
18 Correct 30 ms 20468 KB Output is correct
19 Correct 38 ms 20212 KB Output is correct
20 Correct 38 ms 20472 KB Output is correct
21 Correct 47 ms 20464 KB Output is correct
22 Correct 53 ms 20208 KB Output is correct
23 Correct 40 ms 20204 KB Output is correct
24 Correct 47 ms 20212 KB Output is correct
25 Correct 48 ms 20208 KB Output is correct
26 Correct 31 ms 20224 KB Output is correct
27 Correct 38 ms 20512 KB Output is correct
28 Correct 44 ms 20236 KB Output is correct
29 Correct 51 ms 20208 KB Output is correct
30 Correct 46 ms 20408 KB Output is correct
31 Correct 40 ms 20212 KB Output is correct
32 Correct 49 ms 20336 KB Output is correct
33 Correct 47 ms 20208 KB Output is correct
34 Correct 35 ms 20408 KB Output is correct
35 Correct 44 ms 20212 KB Output is correct
36 Correct 48 ms 20208 KB Output is correct
37 Correct 44 ms 20224 KB Output is correct
38 Correct 45 ms 20208 KB Output is correct
39 Correct 47 ms 20496 KB Output is correct
40 Correct 44 ms 20244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 273 ms 19156 KB Output is correct
12 Correct 252 ms 19000 KB Output is correct
13 Correct 284 ms 18120 KB Output is correct
14 Correct 312 ms 18260 KB Output is correct
15 Correct 272 ms 19100 KB Output is correct
16 Correct 307 ms 18512 KB Output is correct
17 Correct 307 ms 18280 KB Output is correct
18 Correct 168 ms 20352 KB Output is correct
19 Correct 245 ms 17212 KB Output is correct
20 Correct 272 ms 18772 KB Output is correct
21 Correct 280 ms 22100 KB Output is correct
22 Correct 292 ms 22612 KB Output is correct
23 Correct 356 ms 21328 KB Output is correct
24 Correct 347 ms 21144 KB Output is correct
25 Correct 315 ms 23096 KB Output is correct
26 Correct 360 ms 21840 KB Output is correct
27 Correct 374 ms 21724 KB Output is correct
28 Correct 198 ms 24148 KB Output is correct
29 Correct 272 ms 20216 KB Output is correct
30 Correct 286 ms 22360 KB Output is correct
31 Correct 367 ms 22332 KB Output is correct
32 Correct 447 ms 22096 KB Output is correct
33 Correct 314 ms 21116 KB Output is correct
34 Correct 375 ms 21528 KB Output is correct
35 Correct 387 ms 21584 KB Output is correct
36 Correct 174 ms 20092 KB Output is correct
37 Correct 293 ms 22132 KB Output is correct
38 Correct 297 ms 20308 KB Output is correct
39 Correct 340 ms 21304 KB Output is correct
40 Correct 355 ms 21328 KB Output is correct
41 Correct 37 ms 20464 KB Output is correct
42 Correct 39 ms 20464 KB Output is correct
43 Correct 44 ms 20208 KB Output is correct
44 Correct 65 ms 20684 KB Output is correct
45 Correct 42 ms 20472 KB Output is correct
46 Correct 50 ms 20464 KB Output is correct
47 Correct 51 ms 21232 KB Output is correct
48 Correct 30 ms 20468 KB Output is correct
49 Correct 38 ms 20212 KB Output is correct
50 Correct 38 ms 20472 KB Output is correct
51 Correct 47 ms 20464 KB Output is correct
52 Correct 53 ms 20208 KB Output is correct
53 Correct 40 ms 20204 KB Output is correct
54 Correct 47 ms 20212 KB Output is correct
55 Correct 48 ms 20208 KB Output is correct
56 Correct 31 ms 20224 KB Output is correct
57 Correct 38 ms 20512 KB Output is correct
58 Correct 44 ms 20236 KB Output is correct
59 Correct 51 ms 20208 KB Output is correct
60 Correct 46 ms 20408 KB Output is correct
61 Correct 40 ms 20212 KB Output is correct
62 Correct 49 ms 20336 KB Output is correct
63 Correct 47 ms 20208 KB Output is correct
64 Correct 35 ms 20408 KB Output is correct
65 Correct 44 ms 20212 KB Output is correct
66 Correct 48 ms 20208 KB Output is correct
67 Correct 44 ms 20224 KB Output is correct
68 Correct 45 ms 20208 KB Output is correct
69 Correct 47 ms 20496 KB Output is correct
70 Correct 44 ms 20244 KB Output is correct
71 Correct 404 ms 44644 KB Output is correct
72 Correct 424 ms 44852 KB Output is correct
73 Correct 528 ms 43376 KB Output is correct
74 Correct 747 ms 43760 KB Output is correct
75 Correct 463 ms 45552 KB Output is correct
76 Correct 658 ms 43836 KB Output is correct
77 Correct 595 ms 43760 KB Output is correct
78 Correct 243 ms 47600 KB Output is correct
79 Correct 424 ms 41972 KB Output is correct
80 Correct 407 ms 44692 KB Output is correct
81 Correct 566 ms 44528 KB Output is correct
82 Correct 721 ms 43648 KB Output is correct
83 Correct 436 ms 42740 KB Output is correct
84 Correct 605 ms 43524 KB Output is correct
85 Correct 699 ms 43788 KB Output is correct
86 Correct 244 ms 41716 KB Output is correct
87 Correct 433 ms 44548 KB Output is correct
88 Correct 408 ms 41712 KB Output is correct
89 Correct 533 ms 43248 KB Output is correct
90 Correct 592 ms 43560 KB Output is correct
91 Correct 444 ms 42708 KB Output is correct
92 Correct 654 ms 44020 KB Output is correct
93 Correct 651 ms 43760 KB Output is correct
94 Correct 255 ms 41680 KB Output is correct
95 Correct 544 ms 43804 KB Output is correct
96 Correct 529 ms 43812 KB Output is correct
97 Correct 528 ms 43760 KB Output is correct
98 Correct 521 ms 43764 KB Output is correct
99 Correct 597 ms 43760 KB Output is correct
100 Correct 528 ms 43680 KB Output is correct