Submission #225244

# Submission time Handle Problem Language Result Execution time Memory
225244 2020-04-19T20:07:19 Z MKopchev Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1164 ms 54520 KB
#include<bits/stdc++.h>
using namespace std;
const int nmax=(1<<20)+42;

int l,n,q;

int inp[nmax];

char cur[25];

int cnt_bits[nmax];

long long dp_ones[nmax],dp_zeros[nmax];

int main()
{
    scanf("%i%i",&l,&q);

    n=1<<l;

    for(int i=1;i<n;i++)
    {
        cnt_bits[i]=cnt_bits[i/2]+i%2;
    }

    for(int i=0;i<n;i++)
    {
        char c=getchar();
        while('0'>c||c>'9')c=getchar();

        inp[i]=c-'0';
    }

    for(int i=0;i<n;i++)dp_ones[i]=inp[n-1-i];

    for(int i=0;i<l;i++)
        for(int mask=0;mask<n;mask++)
            if((mask&(1<<i)))dp_ones[mask]+=dp_ones[mask-(1<<i)];

    for(int i=0;i<n;i++)dp_zeros[i]=inp[i];

    for(int i=0;i<l;i++)
        for(int mask=n-1;mask>=0;mask--)
            if((mask&(1<<i)))dp_zeros[mask]+=dp_zeros[mask-(1<<i)];

    for(int t=1;t<=q;t++)
    {
        long long output=0;

        int question=0;

        int one=0;

        int zero=0;

        for(int j=1;j<=l;j++)
        {
            char c=getchar();
            while(c!='?'&&c!='0'&&c!='1')c=getchar();

            if(c=='?')question+=(1<<(l-j));
            if(c=='0')zero+=(1<<(l-j));
            if(c=='1')one+=(1<<(l-j));
        }

        if(cnt_bits[question]<=cnt_bits[zero]&&cnt_bits[question]<=cnt_bits[one])
        {
            int mask=question;

            while(true)
            {
                output+=inp[one+mask];

                if(mask==0)break;

                mask=(mask-1)&question;
            }
        }
        else if(cnt_bits[zero]<=cnt_bits[one])
        {
            int mask=zero;

            while(true)
            {
                if(cnt_bits[mask]%2)output+=dp_ones[question+mask];
                else output-=dp_ones[question+mask];

                if(mask==0)break;
                mask=(mask-1)&zero;
            }
        }
        else
        {
            int mask=one;

            while(true)
            {
                if(cnt_bits[mask]%2)output+=dp_zeros[question+mask];
                else output-=dp_zeros[question+mask];

                if(mask==0)break;
                mask=(mask-1)&one;
            }
        }

        output=abs(output);
        printf("%lld\n",output);
    }
    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i%i",&l,&q);
     ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 288 ms 15100 KB Output is correct
12 Correct 298 ms 14840 KB Output is correct
13 Correct 319 ms 14204 KB Output is correct
14 Correct 304 ms 14072 KB Output is correct
15 Correct 301 ms 15224 KB Output is correct
16 Correct 326 ms 14468 KB Output is correct
17 Correct 322 ms 14328 KB Output is correct
18 Correct 249 ms 16312 KB Output is correct
19 Correct 276 ms 13176 KB Output is correct
20 Correct 291 ms 14912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 288 ms 15100 KB Output is correct
12 Correct 298 ms 14840 KB Output is correct
13 Correct 319 ms 14204 KB Output is correct
14 Correct 304 ms 14072 KB Output is correct
15 Correct 301 ms 15224 KB Output is correct
16 Correct 326 ms 14468 KB Output is correct
17 Correct 322 ms 14328 KB Output is correct
18 Correct 249 ms 16312 KB Output is correct
19 Correct 276 ms 13176 KB Output is correct
20 Correct 291 ms 14912 KB Output is correct
21 Correct 334 ms 18128 KB Output is correct
22 Correct 351 ms 18424 KB Output is correct
23 Correct 392 ms 17656 KB Output is correct
24 Correct 373 ms 17272 KB Output is correct
25 Correct 354 ms 19320 KB Output is correct
26 Correct 389 ms 17912 KB Output is correct
27 Correct 389 ms 17656 KB Output is correct
28 Correct 291 ms 20344 KB Output is correct
29 Correct 326 ms 16356 KB Output is correct
30 Correct 351 ms 18424 KB Output is correct
31 Correct 369 ms 18296 KB Output is correct
32 Correct 395 ms 18424 KB Output is correct
33 Correct 342 ms 17148 KB Output is correct
34 Correct 383 ms 17276 KB Output is correct
35 Correct 393 ms 17784 KB Output is correct
36 Correct 268 ms 16248 KB Output is correct
37 Correct 342 ms 18296 KB Output is correct
38 Correct 331 ms 16248 KB Output is correct
39 Correct 373 ms 17528 KB Output is correct
40 Correct 375 ms 17336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 98 ms 27256 KB Output is correct
12 Correct 108 ms 27256 KB Output is correct
13 Correct 109 ms 27256 KB Output is correct
14 Correct 138 ms 27128 KB Output is correct
15 Correct 101 ms 27256 KB Output is correct
16 Correct 126 ms 27256 KB Output is correct
17 Correct 116 ms 27256 KB Output is correct
18 Correct 91 ms 27384 KB Output is correct
19 Correct 100 ms 27128 KB Output is correct
20 Correct 104 ms 27256 KB Output is correct
21 Correct 112 ms 27256 KB Output is correct
22 Correct 125 ms 27256 KB Output is correct
23 Correct 100 ms 27128 KB Output is correct
24 Correct 124 ms 27128 KB Output is correct
25 Correct 118 ms 27256 KB Output is correct
26 Correct 90 ms 27128 KB Output is correct
27 Correct 103 ms 27256 KB Output is correct
28 Correct 100 ms 27128 KB Output is correct
29 Correct 110 ms 27384 KB Output is correct
30 Correct 108 ms 27128 KB Output is correct
31 Correct 102 ms 27128 KB Output is correct
32 Correct 124 ms 27256 KB Output is correct
33 Correct 122 ms 27256 KB Output is correct
34 Correct 89 ms 27128 KB Output is correct
35 Correct 112 ms 27256 KB Output is correct
36 Correct 113 ms 27256 KB Output is correct
37 Correct 114 ms 27256 KB Output is correct
38 Correct 110 ms 27256 KB Output is correct
39 Correct 108 ms 27256 KB Output is correct
40 Correct 128 ms 27256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 288 ms 15100 KB Output is correct
12 Correct 298 ms 14840 KB Output is correct
13 Correct 319 ms 14204 KB Output is correct
14 Correct 304 ms 14072 KB Output is correct
15 Correct 301 ms 15224 KB Output is correct
16 Correct 326 ms 14468 KB Output is correct
17 Correct 322 ms 14328 KB Output is correct
18 Correct 249 ms 16312 KB Output is correct
19 Correct 276 ms 13176 KB Output is correct
20 Correct 291 ms 14912 KB Output is correct
21 Correct 334 ms 18128 KB Output is correct
22 Correct 351 ms 18424 KB Output is correct
23 Correct 392 ms 17656 KB Output is correct
24 Correct 373 ms 17272 KB Output is correct
25 Correct 354 ms 19320 KB Output is correct
26 Correct 389 ms 17912 KB Output is correct
27 Correct 389 ms 17656 KB Output is correct
28 Correct 291 ms 20344 KB Output is correct
29 Correct 326 ms 16356 KB Output is correct
30 Correct 351 ms 18424 KB Output is correct
31 Correct 369 ms 18296 KB Output is correct
32 Correct 395 ms 18424 KB Output is correct
33 Correct 342 ms 17148 KB Output is correct
34 Correct 383 ms 17276 KB Output is correct
35 Correct 393 ms 17784 KB Output is correct
36 Correct 268 ms 16248 KB Output is correct
37 Correct 342 ms 18296 KB Output is correct
38 Correct 331 ms 16248 KB Output is correct
39 Correct 373 ms 17528 KB Output is correct
40 Correct 375 ms 17336 KB Output is correct
41 Correct 98 ms 27256 KB Output is correct
42 Correct 108 ms 27256 KB Output is correct
43 Correct 109 ms 27256 KB Output is correct
44 Correct 138 ms 27128 KB Output is correct
45 Correct 101 ms 27256 KB Output is correct
46 Correct 126 ms 27256 KB Output is correct
47 Correct 116 ms 27256 KB Output is correct
48 Correct 91 ms 27384 KB Output is correct
49 Correct 100 ms 27128 KB Output is correct
50 Correct 104 ms 27256 KB Output is correct
51 Correct 112 ms 27256 KB Output is correct
52 Correct 125 ms 27256 KB Output is correct
53 Correct 100 ms 27128 KB Output is correct
54 Correct 124 ms 27128 KB Output is correct
55 Correct 118 ms 27256 KB Output is correct
56 Correct 90 ms 27128 KB Output is correct
57 Correct 103 ms 27256 KB Output is correct
58 Correct 100 ms 27128 KB Output is correct
59 Correct 110 ms 27384 KB Output is correct
60 Correct 108 ms 27128 KB Output is correct
61 Correct 102 ms 27128 KB Output is correct
62 Correct 124 ms 27256 KB Output is correct
63 Correct 122 ms 27256 KB Output is correct
64 Correct 89 ms 27128 KB Output is correct
65 Correct 112 ms 27256 KB Output is correct
66 Correct 113 ms 27256 KB Output is correct
67 Correct 114 ms 27256 KB Output is correct
68 Correct 110 ms 27256 KB Output is correct
69 Correct 108 ms 27256 KB Output is correct
70 Correct 128 ms 27256 KB Output is correct
71 Correct 603 ms 51592 KB Output is correct
72 Correct 622 ms 51832 KB Output is correct
73 Correct 754 ms 50424 KB Output is correct
74 Correct 1164 ms 50584 KB Output is correct
75 Correct 670 ms 52468 KB Output is correct
76 Correct 1044 ms 50916 KB Output is correct
77 Correct 952 ms 51060 KB Output is correct
78 Correct 446 ms 54520 KB Output is correct
79 Correct 658 ms 48504 KB Output is correct
80 Correct 613 ms 51804 KB Output is correct
81 Correct 805 ms 51780 KB Output is correct
82 Correct 1125 ms 50728 KB Output is correct
83 Correct 681 ms 49656 KB Output is correct
84 Correct 1022 ms 50680 KB Output is correct
85 Correct 985 ms 50808 KB Output is correct
86 Correct 419 ms 48504 KB Output is correct
87 Correct 607 ms 51572 KB Output is correct
88 Correct 588 ms 48504 KB Output is correct
89 Correct 725 ms 50408 KB Output is correct
90 Correct 972 ms 50552 KB Output is correct
91 Correct 628 ms 49780 KB Output is correct
92 Correct 1095 ms 50804 KB Output is correct
93 Correct 1141 ms 50676 KB Output is correct
94 Correct 415 ms 48628 KB Output is correct
95 Correct 965 ms 50676 KB Output is correct
96 Correct 835 ms 50808 KB Output is correct
97 Correct 831 ms 50692 KB Output is correct
98 Correct 841 ms 50648 KB Output is correct
99 Correct 818 ms 50676 KB Output is correct
100 Correct 819 ms 50664 KB Output is correct