답안 #674599

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674599 2022-12-25T09:47:59 Z beedle Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1271 ms 51156 KB
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <stack>
#include <map>
#include <math.h>
#include <bitset>
#include <deque>
#include <string>
#include <tuple>
#include <queue>
#include <numeric>
#include <complex>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define ld long long double
#define rep(i, a, b) for (long long i = a; i <= b; i++)
#define mod 1000000007ll
#define INF 1e18
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin (), (x).end ()
#define sz(x) (ll) (x).size ()
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define rank rnk
#define log lg
#define fast                                                                  \
    ios_base::sync_with_stdio (false);                                        \
    cin.tie (NULL);                                                           \
    cout.tie (NULL)
 
using namespace std;

signed main()
{
    fast;

    ll l,q;
    cin>>l>>q;

    ll dp[1ll<<20], dp2[1ll<<20], v[1ll<<20];

    string s;
    cin>>s;
    rep(i,0,sz(s)-1)
    dp[(~i)&((1ll<<l)-1)]=s[i]-'0', dp2[i]=s[i]-'0', v[i]=s[i]-'0';

    rep(i,0,l-1)
    rep(mask,0,(1ll<<l)-1)
    if(mask&(1ll<<i))
    dp[mask]+=dp[mask^(1ll<<i)];

    rep(i,0,l-1)
    rep(mask,0,(1ll<<l)-1)
    if(mask&(1ll<<i))
    dp2[mask]+=dp2[mask^(1ll<<i)];

    while(q--)
    {
        string str;
        cin>>str;
        reverse(all(str));

        ll c0=0, c1=0, cq=0;
        for(auto x:str)
        if(x=='0')
        c0++;
        else if(x=='1')
        c1++;
        else
        cq++;

        ll c=min({c0,c1,cq});

        ll ans=0;

        if(c0==c)
        {
            ll mask=0, base=0;
            rep(i,0,l-1)
            if(str[i]=='?')
            base^=(1ll<<i);
            else if(str[i]=='0')
            mask^=(1ll<<i);

            for(ll submask=mask;;submask=(submask-1)&mask)
            {
                ll dir=__builtin_popcount(mask)-__builtin_popcount(submask);
                if(dir&1)
                ans-=dp[base^submask];
                else
                ans+=dp[base^submask];

                if(!submask)
                break;
            }
        }
        else if(c1==c)
        {
            ll mask=0, base=0;
            rep(i,0,l-1)
            if(str[i]=='1')
            mask^=(1ll<<i);
            else if(str[i]=='?')
            base^=(1ll<<i);

            for(ll submask=mask;;submask=(submask-1)&mask)
            {
                ll dir=__builtin_popcount(mask)-__builtin_popcount(submask);
                if(dir&1)
                ans-=dp2[base^submask];
                else
                ans+=dp2[base^submask];

                if(!submask)
                break;
            }
        } 
        else if(cq==c)
        {
            ll mask=0, base=0;
            rep(i,0,l-1)
            if(str[i]=='1')
            base^=(1ll<<i);
            else if(str[i]=='?')
            mask^=(1ll<<i);

            for(ll submask=mask;;submask=(submask-1)&mask)
            {
                ans+=v[base^submask];
                if(!submask)
                break;
            }
        }

        cout<<ans<<endl;
    }

    return 0;
}

/*
3 6
12345678 
011
000
0??
1?0
?11
???
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24916 KB Output is correct
2 Correct 10 ms 24916 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 11 ms 24916 KB Output is correct
5 Correct 10 ms 24916 KB Output is correct
6 Correct 12 ms 24836 KB Output is correct
7 Correct 11 ms 24924 KB Output is correct
8 Correct 10 ms 24948 KB Output is correct
9 Correct 10 ms 24916 KB Output is correct
10 Correct 10 ms 24908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24916 KB Output is correct
2 Correct 10 ms 24916 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 11 ms 24916 KB Output is correct
5 Correct 10 ms 24916 KB Output is correct
6 Correct 12 ms 24836 KB Output is correct
7 Correct 11 ms 24924 KB Output is correct
8 Correct 10 ms 24948 KB Output is correct
9 Correct 10 ms 24916 KB Output is correct
10 Correct 10 ms 24908 KB Output is correct
11 Correct 222 ms 39608 KB Output is correct
12 Correct 220 ms 39260 KB Output is correct
13 Correct 260 ms 38512 KB Output is correct
14 Correct 278 ms 38644 KB Output is correct
15 Correct 262 ms 39656 KB Output is correct
16 Correct 281 ms 38772 KB Output is correct
17 Correct 282 ms 38732 KB Output is correct
18 Correct 175 ms 40696 KB Output is correct
19 Correct 211 ms 37568 KB Output is correct
20 Correct 241 ms 39312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24916 KB Output is correct
2 Correct 10 ms 24916 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 11 ms 24916 KB Output is correct
5 Correct 10 ms 24916 KB Output is correct
6 Correct 12 ms 24836 KB Output is correct
7 Correct 11 ms 24924 KB Output is correct
8 Correct 10 ms 24948 KB Output is correct
9 Correct 10 ms 24916 KB Output is correct
10 Correct 10 ms 24908 KB Output is correct
11 Correct 222 ms 39608 KB Output is correct
12 Correct 220 ms 39260 KB Output is correct
13 Correct 260 ms 38512 KB Output is correct
14 Correct 278 ms 38644 KB Output is correct
15 Correct 262 ms 39656 KB Output is correct
16 Correct 281 ms 38772 KB Output is correct
17 Correct 282 ms 38732 KB Output is correct
18 Correct 175 ms 40696 KB Output is correct
19 Correct 211 ms 37568 KB Output is correct
20 Correct 241 ms 39312 KB Output is correct
21 Correct 260 ms 42576 KB Output is correct
22 Correct 258 ms 42836 KB Output is correct
23 Correct 322 ms 41840 KB Output is correct
24 Correct 377 ms 41536 KB Output is correct
25 Correct 320 ms 43568 KB Output is correct
26 Correct 369 ms 42140 KB Output is correct
27 Correct 365 ms 42124 KB Output is correct
28 Correct 191 ms 44600 KB Output is correct
29 Correct 275 ms 40520 KB Output is correct
30 Correct 288 ms 42756 KB Output is correct
31 Correct 346 ms 42600 KB Output is correct
32 Correct 381 ms 42648 KB Output is correct
33 Correct 296 ms 41544 KB Output is correct
34 Correct 375 ms 41620 KB Output is correct
35 Correct 365 ms 42064 KB Output is correct
36 Correct 215 ms 40516 KB Output is correct
37 Correct 242 ms 42576 KB Output is correct
38 Correct 299 ms 40560 KB Output is correct
39 Correct 322 ms 41744 KB Output is correct
40 Correct 366 ms 41620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24916 KB Output is correct
2 Correct 10 ms 24916 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 11 ms 24916 KB Output is correct
5 Correct 10 ms 24916 KB Output is correct
6 Correct 12 ms 24836 KB Output is correct
7 Correct 11 ms 24924 KB Output is correct
8 Correct 10 ms 24948 KB Output is correct
9 Correct 10 ms 24916 KB Output is correct
10 Correct 10 ms 24908 KB Output is correct
11 Correct 92 ms 28368 KB Output is correct
12 Correct 76 ms 28432 KB Output is correct
13 Correct 97 ms 28304 KB Output is correct
14 Correct 123 ms 28396 KB Output is correct
15 Correct 77 ms 28376 KB Output is correct
16 Correct 112 ms 28292 KB Output is correct
17 Correct 110 ms 28344 KB Output is correct
18 Correct 80 ms 28504 KB Output is correct
19 Correct 77 ms 28308 KB Output is correct
20 Correct 85 ms 28360 KB Output is correct
21 Correct 112 ms 28440 KB Output is correct
22 Correct 126 ms 28368 KB Output is correct
23 Correct 82 ms 28268 KB Output is correct
24 Correct 113 ms 28368 KB Output is correct
25 Correct 115 ms 28320 KB Output is correct
26 Correct 77 ms 28272 KB Output is correct
27 Correct 87 ms 28296 KB Output is correct
28 Correct 80 ms 28296 KB Output is correct
29 Correct 91 ms 28272 KB Output is correct
30 Correct 95 ms 28436 KB Output is correct
31 Correct 104 ms 28428 KB Output is correct
32 Correct 122 ms 28428 KB Output is correct
33 Correct 112 ms 28380 KB Output is correct
34 Correct 70 ms 28248 KB Output is correct
35 Correct 94 ms 28340 KB Output is correct
36 Correct 108 ms 28264 KB Output is correct
37 Correct 102 ms 28364 KB Output is correct
38 Correct 95 ms 28420 KB Output is correct
39 Correct 97 ms 28312 KB Output is correct
40 Correct 100 ms 28264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24916 KB Output is correct
2 Correct 10 ms 24916 KB Output is correct
3 Correct 11 ms 24916 KB Output is correct
4 Correct 11 ms 24916 KB Output is correct
5 Correct 10 ms 24916 KB Output is correct
6 Correct 12 ms 24836 KB Output is correct
7 Correct 11 ms 24924 KB Output is correct
8 Correct 10 ms 24948 KB Output is correct
9 Correct 10 ms 24916 KB Output is correct
10 Correct 10 ms 24908 KB Output is correct
11 Correct 222 ms 39608 KB Output is correct
12 Correct 220 ms 39260 KB Output is correct
13 Correct 260 ms 38512 KB Output is correct
14 Correct 278 ms 38644 KB Output is correct
15 Correct 262 ms 39656 KB Output is correct
16 Correct 281 ms 38772 KB Output is correct
17 Correct 282 ms 38732 KB Output is correct
18 Correct 175 ms 40696 KB Output is correct
19 Correct 211 ms 37568 KB Output is correct
20 Correct 241 ms 39312 KB Output is correct
21 Correct 260 ms 42576 KB Output is correct
22 Correct 258 ms 42836 KB Output is correct
23 Correct 322 ms 41840 KB Output is correct
24 Correct 377 ms 41536 KB Output is correct
25 Correct 320 ms 43568 KB Output is correct
26 Correct 369 ms 42140 KB Output is correct
27 Correct 365 ms 42124 KB Output is correct
28 Correct 191 ms 44600 KB Output is correct
29 Correct 275 ms 40520 KB Output is correct
30 Correct 288 ms 42756 KB Output is correct
31 Correct 346 ms 42600 KB Output is correct
32 Correct 381 ms 42648 KB Output is correct
33 Correct 296 ms 41544 KB Output is correct
34 Correct 375 ms 41620 KB Output is correct
35 Correct 365 ms 42064 KB Output is correct
36 Correct 215 ms 40516 KB Output is correct
37 Correct 242 ms 42576 KB Output is correct
38 Correct 299 ms 40560 KB Output is correct
39 Correct 322 ms 41744 KB Output is correct
40 Correct 366 ms 41620 KB Output is correct
41 Correct 92 ms 28368 KB Output is correct
42 Correct 76 ms 28432 KB Output is correct
43 Correct 97 ms 28304 KB Output is correct
44 Correct 123 ms 28396 KB Output is correct
45 Correct 77 ms 28376 KB Output is correct
46 Correct 112 ms 28292 KB Output is correct
47 Correct 110 ms 28344 KB Output is correct
48 Correct 80 ms 28504 KB Output is correct
49 Correct 77 ms 28308 KB Output is correct
50 Correct 85 ms 28360 KB Output is correct
51 Correct 112 ms 28440 KB Output is correct
52 Correct 126 ms 28368 KB Output is correct
53 Correct 82 ms 28268 KB Output is correct
54 Correct 113 ms 28368 KB Output is correct
55 Correct 115 ms 28320 KB Output is correct
56 Correct 77 ms 28272 KB Output is correct
57 Correct 87 ms 28296 KB Output is correct
58 Correct 80 ms 28296 KB Output is correct
59 Correct 91 ms 28272 KB Output is correct
60 Correct 95 ms 28436 KB Output is correct
61 Correct 104 ms 28428 KB Output is correct
62 Correct 122 ms 28428 KB Output is correct
63 Correct 112 ms 28380 KB Output is correct
64 Correct 70 ms 28248 KB Output is correct
65 Correct 94 ms 28340 KB Output is correct
66 Correct 108 ms 28264 KB Output is correct
67 Correct 102 ms 28364 KB Output is correct
68 Correct 95 ms 28420 KB Output is correct
69 Correct 97 ms 28312 KB Output is correct
70 Correct 100 ms 28264 KB Output is correct
71 Correct 508 ms 47476 KB Output is correct
72 Correct 451 ms 47740 KB Output is correct
73 Correct 690 ms 46256 KB Output is correct
74 Correct 1224 ms 46588 KB Output is correct
75 Correct 501 ms 48848 KB Output is correct
76 Correct 1147 ms 47252 KB Output is correct
77 Correct 1032 ms 47116 KB Output is correct
78 Correct 316 ms 51156 KB Output is correct
79 Correct 494 ms 45256 KB Output is correct
80 Correct 565 ms 48524 KB Output is correct
81 Correct 741 ms 48476 KB Output is correct
82 Correct 1271 ms 47440 KB Output is correct
83 Correct 529 ms 46836 KB Output is correct
84 Correct 1250 ms 47724 KB Output is correct
85 Correct 1085 ms 47844 KB Output is correct
86 Correct 352 ms 45556 KB Output is correct
87 Correct 480 ms 48784 KB Output is correct
88 Correct 541 ms 45932 KB Output is correct
89 Correct 733 ms 47636 KB Output is correct
90 Correct 768 ms 48144 KB Output is correct
91 Correct 512 ms 47420 KB Output is correct
92 Correct 1258 ms 48724 KB Output is correct
93 Correct 963 ms 48616 KB Output is correct
94 Correct 307 ms 46456 KB Output is correct
95 Correct 844 ms 48808 KB Output is correct
96 Correct 845 ms 48800 KB Output is correct
97 Correct 850 ms 49064 KB Output is correct
98 Correct 811 ms 48892 KB Output is correct
99 Correct 825 ms 49044 KB Output is correct
100 Correct 889 ms 48868 KB Output is correct