답안 #1001205

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1001205 2024-06-18T16:59:05 Z mispertion Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
874 ms 46064 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 292 ms 6412 KB Output is correct
12 Correct 285 ms 8020 KB Output is correct
13 Correct 352 ms 7304 KB Output is correct
14 Correct 336 ms 7504 KB Output is correct
15 Correct 333 ms 8468 KB Output is correct
16 Correct 359 ms 7508 KB Output is correct
17 Correct 375 ms 7708 KB Output is correct
18 Correct 184 ms 9552 KB Output is correct
19 Correct 282 ms 6488 KB Output is correct
20 Correct 300 ms 8176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 292 ms 6412 KB Output is correct
12 Correct 285 ms 8020 KB Output is correct
13 Correct 352 ms 7304 KB Output is correct
14 Correct 336 ms 7504 KB Output is correct
15 Correct 333 ms 8468 KB Output is correct
16 Correct 359 ms 7508 KB Output is correct
17 Correct 375 ms 7708 KB Output is correct
18 Correct 184 ms 9552 KB Output is correct
19 Correct 282 ms 6488 KB Output is correct
20 Correct 300 ms 8176 KB Output is correct
21 Correct 314 ms 6484 KB Output is correct
22 Correct 335 ms 8752 KB Output is correct
23 Correct 413 ms 7764 KB Output is correct
24 Correct 409 ms 5560 KB Output is correct
25 Correct 448 ms 7504 KB Output is correct
26 Correct 485 ms 7924 KB Output is correct
27 Correct 468 ms 7988 KB Output is correct
28 Correct 217 ms 10580 KB Output is correct
29 Correct 313 ms 6480 KB Output is correct
30 Correct 351 ms 8624 KB Output is correct
31 Correct 474 ms 8528 KB Output is correct
32 Correct 452 ms 8532 KB Output is correct
33 Correct 366 ms 7520 KB Output is correct
34 Correct 458 ms 7476 KB Output is correct
35 Correct 474 ms 7956 KB Output is correct
36 Correct 255 ms 6484 KB Output is correct
37 Correct 326 ms 8528 KB Output is correct
38 Correct 373 ms 4432 KB Output is correct
39 Correct 402 ms 7804 KB Output is correct
40 Correct 415 ms 7468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 51 ms 17588 KB Output is correct
12 Correct 48 ms 18672 KB Output is correct
13 Correct 57 ms 18644 KB Output is correct
14 Correct 66 ms 18604 KB Output is correct
15 Correct 54 ms 18684 KB Output is correct
16 Correct 62 ms 18664 KB Output is correct
17 Correct 60 ms 18672 KB Output is correct
18 Correct 40 ms 18928 KB Output is correct
19 Correct 48 ms 18680 KB Output is correct
20 Correct 48 ms 18860 KB Output is correct
21 Correct 57 ms 18676 KB Output is correct
22 Correct 71 ms 18676 KB Output is correct
23 Correct 57 ms 18532 KB Output is correct
24 Correct 58 ms 18800 KB Output is correct
25 Correct 59 ms 18672 KB Output is correct
26 Correct 37 ms 18676 KB Output is correct
27 Correct 44 ms 18672 KB Output is correct
28 Correct 55 ms 18672 KB Output is correct
29 Correct 54 ms 18672 KB Output is correct
30 Correct 56 ms 18640 KB Output is correct
31 Correct 49 ms 18676 KB Output is correct
32 Correct 74 ms 18672 KB Output is correct
33 Correct 67 ms 18588 KB Output is correct
34 Correct 40 ms 18472 KB Output is correct
35 Correct 53 ms 18672 KB Output is correct
36 Correct 57 ms 18672 KB Output is correct
37 Correct 55 ms 18768 KB Output is correct
38 Correct 59 ms 18604 KB Output is correct
39 Correct 55 ms 18672 KB Output is correct
40 Correct 55 ms 18588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 292 ms 6412 KB Output is correct
12 Correct 285 ms 8020 KB Output is correct
13 Correct 352 ms 7304 KB Output is correct
14 Correct 336 ms 7504 KB Output is correct
15 Correct 333 ms 8468 KB Output is correct
16 Correct 359 ms 7508 KB Output is correct
17 Correct 375 ms 7708 KB Output is correct
18 Correct 184 ms 9552 KB Output is correct
19 Correct 282 ms 6488 KB Output is correct
20 Correct 300 ms 8176 KB Output is correct
21 Correct 314 ms 6484 KB Output is correct
22 Correct 335 ms 8752 KB Output is correct
23 Correct 413 ms 7764 KB Output is correct
24 Correct 409 ms 5560 KB Output is correct
25 Correct 448 ms 7504 KB Output is correct
26 Correct 485 ms 7924 KB Output is correct
27 Correct 468 ms 7988 KB Output is correct
28 Correct 217 ms 10580 KB Output is correct
29 Correct 313 ms 6480 KB Output is correct
30 Correct 351 ms 8624 KB Output is correct
31 Correct 474 ms 8528 KB Output is correct
32 Correct 452 ms 8532 KB Output is correct
33 Correct 366 ms 7520 KB Output is correct
34 Correct 458 ms 7476 KB Output is correct
35 Correct 474 ms 7956 KB Output is correct
36 Correct 255 ms 6484 KB Output is correct
37 Correct 326 ms 8528 KB Output is correct
38 Correct 373 ms 4432 KB Output is correct
39 Correct 402 ms 7804 KB Output is correct
40 Correct 415 ms 7468 KB Output is correct
41 Correct 51 ms 17588 KB Output is correct
42 Correct 48 ms 18672 KB Output is correct
43 Correct 57 ms 18644 KB Output is correct
44 Correct 66 ms 18604 KB Output is correct
45 Correct 54 ms 18684 KB Output is correct
46 Correct 62 ms 18664 KB Output is correct
47 Correct 60 ms 18672 KB Output is correct
48 Correct 40 ms 18928 KB Output is correct
49 Correct 48 ms 18680 KB Output is correct
50 Correct 48 ms 18860 KB Output is correct
51 Correct 57 ms 18676 KB Output is correct
52 Correct 71 ms 18676 KB Output is correct
53 Correct 57 ms 18532 KB Output is correct
54 Correct 58 ms 18800 KB Output is correct
55 Correct 59 ms 18672 KB Output is correct
56 Correct 37 ms 18676 KB Output is correct
57 Correct 44 ms 18672 KB Output is correct
58 Correct 55 ms 18672 KB Output is correct
59 Correct 54 ms 18672 KB Output is correct
60 Correct 56 ms 18640 KB Output is correct
61 Correct 49 ms 18676 KB Output is correct
62 Correct 74 ms 18672 KB Output is correct
63 Correct 67 ms 18588 KB Output is correct
64 Correct 40 ms 18472 KB Output is correct
65 Correct 53 ms 18672 KB Output is correct
66 Correct 57 ms 18672 KB Output is correct
67 Correct 55 ms 18768 KB Output is correct
68 Correct 59 ms 18604 KB Output is correct
69 Correct 55 ms 18672 KB Output is correct
70 Correct 55 ms 18588 KB Output is correct
71 Correct 461 ms 43124 KB Output is correct
72 Correct 448 ms 43228 KB Output is correct
73 Correct 625 ms 41800 KB Output is correct
74 Correct 779 ms 41972 KB Output is correct
75 Correct 586 ms 44016 KB Output is correct
76 Correct 849 ms 42356 KB Output is correct
77 Correct 867 ms 42228 KB Output is correct
78 Correct 288 ms 46064 KB Output is correct
79 Correct 517 ms 38644 KB Output is correct
80 Correct 538 ms 40436 KB Output is correct
81 Correct 805 ms 40528 KB Output is correct
82 Correct 871 ms 39532 KB Output is correct
83 Correct 558 ms 39816 KB Output is correct
84 Correct 765 ms 42060 KB Output is correct
85 Correct 757 ms 42224 KB Output is correct
86 Correct 297 ms 39916 KB Output is correct
87 Correct 513 ms 43088 KB Output is correct
88 Correct 513 ms 40144 KB Output is correct
89 Correct 676 ms 41588 KB Output is correct
90 Correct 704 ms 41980 KB Output is correct
91 Correct 544 ms 41312 KB Output is correct
92 Correct 874 ms 42228 KB Output is correct
93 Correct 784 ms 42244 KB Output is correct
94 Correct 311 ms 39936 KB Output is correct
95 Correct 625 ms 42224 KB Output is correct
96 Correct 634 ms 42228 KB Output is correct
97 Correct 621 ms 42152 KB Output is correct
98 Correct 621 ms 42228 KB Output is correct
99 Correct 657 ms 42204 KB Output is correct
100 Correct 636 ms 42144 KB Output is correct