Submission #1095961

# Submission time Handle Problem Language Result Execution time Memory
1095961 2024-10-03T13:21:11 Z phamducluong Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
493 ms 43272 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

//using namespace __gnu_pbds;
using namespace std;
//typedef tree<int,null_type,less_equal<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define Sakura_chan    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mem(a,x)       memset(a,x,sizeof(a))
#define fast(s)        s.reserve(2000); s.max_load_factor(0.5);
#define ll             long long
#define F              first
#define S              second
#define pb             push_back
#define ins            insert
#define pii            pair <long long, long long>
#define vpii           vector <pii>
#define sz             size()
#define all(p)         p.begin(), p.end()
#define For(i,a,b)     for(int i=a; i<=b; ++i)
#define rFor(i,a,b)    for(int i=a; i>=b; --i)
#define Auto(it,a)     for(auto it: a)
const int mod=1e9+7;
const int N=1e6+5, M=20;
int n, q, f[1<<M], g[1<<M], cnt[1<<M];
string s, t;
void file()
{
    #define task "snakes"
    if(fopen(task".inp","r"))
    {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    Sakura_chan;
}
void Solve()
{
    cin>>n>>q>>s;
    for(int i=0; i<(1<<n); ++i) f[i]=g[i]=(s[i]-'0');
    for(int i=0; i<n; ++i) for(int mask=0; mask<(1<<n); ++mask) if(mask&(1<<i)) {f[mask]+=f[mask^(1<<i)]; g[mask^(1<<i)]+=g[mask];}
    for(int i=0; i<(1<<n); ++i) cnt[i]=__builtin_popcount(i);
    while(q--)
    {
        cin>>t;
        reverse(all(t));
        int a=0, b=0, c=0;
        ll res=0;
        for(int i=0; i<n; ++i)
        {
            if(t[i]=='?') a|=(1<<i);
            else if(t[i]=='0') b|=(1<<i);
            else c|=(1<<i);
        }
        if(cnt[a]<=n/3)
        {
            for(int mask=a;;mask=(mask-1)&a)
            {
                res+=s[c|mask]-'0';
                if(mask==0) break;
            }
        }
        else if(cnt[b]<=n/3)
        {
            for(int mask=b;;mask=(mask-1)&b)
            {
                if(cnt[mask]&1) res-=g[c|mask];
                else res+=g[c|mask];
                if(mask==0) break;
            }
        }
        else
        {
            for(int mask=c;;mask=(mask-1)&c)
            {
                if((cnt[c]-cnt[mask])&1) res-=f[a|mask];
                else res+=f[a|mask];
                if(mask==0) break;
            }
        }
        cout<<res<<"\n";
    }
}

main()
{
    file();
    Solve();
    return 0;
}

Compilation message

snake_escaping.cpp:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   84 | main()
      | ^~~~
snake_escaping.cpp: In function 'void file()':
snake_escaping.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 436 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 436 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 148 ms 15188 KB Output is correct
12 Correct 165 ms 14676 KB Output is correct
13 Correct 168 ms 13908 KB Output is correct
14 Correct 169 ms 14016 KB Output is correct
15 Correct 163 ms 15092 KB Output is correct
16 Correct 174 ms 14164 KB Output is correct
17 Correct 195 ms 14132 KB Output is correct
18 Correct 105 ms 16208 KB Output is correct
19 Correct 142 ms 13140 KB Output is correct
20 Correct 150 ms 14644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 436 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 148 ms 15188 KB Output is correct
12 Correct 165 ms 14676 KB Output is correct
13 Correct 168 ms 13908 KB Output is correct
14 Correct 169 ms 14016 KB Output is correct
15 Correct 163 ms 15092 KB Output is correct
16 Correct 174 ms 14164 KB Output is correct
17 Correct 195 ms 14132 KB Output is correct
18 Correct 105 ms 16208 KB Output is correct
19 Correct 142 ms 13140 KB Output is correct
20 Correct 150 ms 14644 KB Output is correct
21 Correct 164 ms 18260 KB Output is correct
22 Correct 187 ms 18304 KB Output is correct
23 Correct 196 ms 17208 KB Output is correct
24 Correct 205 ms 17256 KB Output is correct
25 Correct 189 ms 19280 KB Output is correct
26 Correct 199 ms 17676 KB Output is correct
27 Correct 211 ms 17672 KB Output is correct
28 Correct 107 ms 20096 KB Output is correct
29 Correct 162 ms 16212 KB Output is correct
30 Correct 173 ms 18256 KB Output is correct
31 Correct 227 ms 18092 KB Output is correct
32 Correct 205 ms 18208 KB Output is correct
33 Correct 189 ms 16976 KB Output is correct
34 Correct 222 ms 17232 KB Output is correct
35 Correct 214 ms 17700 KB Output is correct
36 Correct 123 ms 16204 KB Output is correct
37 Correct 164 ms 18260 KB Output is correct
38 Correct 178 ms 16128 KB Output is correct
39 Correct 192 ms 17356 KB Output is correct
40 Correct 231 ms 17032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 436 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 41 ms 16116 KB Output is correct
12 Correct 42 ms 16208 KB Output is correct
13 Correct 42 ms 16112 KB Output is correct
14 Correct 50 ms 16112 KB Output is correct
15 Correct 44 ms 16112 KB Output is correct
16 Correct 50 ms 16112 KB Output is correct
17 Correct 49 ms 16108 KB Output is correct
18 Correct 35 ms 16200 KB Output is correct
19 Correct 40 ms 15856 KB Output is correct
20 Correct 41 ms 16116 KB Output is correct
21 Correct 47 ms 16112 KB Output is correct
22 Correct 49 ms 16128 KB Output is correct
23 Correct 42 ms 15956 KB Output is correct
24 Correct 47 ms 15904 KB Output is correct
25 Correct 60 ms 16120 KB Output is correct
26 Correct 35 ms 15860 KB Output is correct
27 Correct 42 ms 16184 KB Output is correct
28 Correct 42 ms 16044 KB Output is correct
29 Correct 46 ms 15916 KB Output is correct
30 Correct 49 ms 15920 KB Output is correct
31 Correct 49 ms 16112 KB Output is correct
32 Correct 52 ms 15972 KB Output is correct
33 Correct 50 ms 16116 KB Output is correct
34 Correct 43 ms 15864 KB Output is correct
35 Correct 46 ms 15952 KB Output is correct
36 Correct 46 ms 16112 KB Output is correct
37 Correct 46 ms 16112 KB Output is correct
38 Correct 46 ms 15960 KB Output is correct
39 Correct 47 ms 15956 KB Output is correct
40 Correct 50 ms 16112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 436 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 148 ms 15188 KB Output is correct
12 Correct 165 ms 14676 KB Output is correct
13 Correct 168 ms 13908 KB Output is correct
14 Correct 169 ms 14016 KB Output is correct
15 Correct 163 ms 15092 KB Output is correct
16 Correct 174 ms 14164 KB Output is correct
17 Correct 195 ms 14132 KB Output is correct
18 Correct 105 ms 16208 KB Output is correct
19 Correct 142 ms 13140 KB Output is correct
20 Correct 150 ms 14644 KB Output is correct
21 Correct 164 ms 18260 KB Output is correct
22 Correct 187 ms 18304 KB Output is correct
23 Correct 196 ms 17208 KB Output is correct
24 Correct 205 ms 17256 KB Output is correct
25 Correct 189 ms 19280 KB Output is correct
26 Correct 199 ms 17676 KB Output is correct
27 Correct 211 ms 17672 KB Output is correct
28 Correct 107 ms 20096 KB Output is correct
29 Correct 162 ms 16212 KB Output is correct
30 Correct 173 ms 18256 KB Output is correct
31 Correct 227 ms 18092 KB Output is correct
32 Correct 205 ms 18208 KB Output is correct
33 Correct 189 ms 16976 KB Output is correct
34 Correct 222 ms 17232 KB Output is correct
35 Correct 214 ms 17700 KB Output is correct
36 Correct 123 ms 16204 KB Output is correct
37 Correct 164 ms 18260 KB Output is correct
38 Correct 178 ms 16128 KB Output is correct
39 Correct 192 ms 17356 KB Output is correct
40 Correct 231 ms 17032 KB Output is correct
41 Correct 41 ms 16116 KB Output is correct
42 Correct 42 ms 16208 KB Output is correct
43 Correct 42 ms 16112 KB Output is correct
44 Correct 50 ms 16112 KB Output is correct
45 Correct 44 ms 16112 KB Output is correct
46 Correct 50 ms 16112 KB Output is correct
47 Correct 49 ms 16108 KB Output is correct
48 Correct 35 ms 16200 KB Output is correct
49 Correct 40 ms 15856 KB Output is correct
50 Correct 41 ms 16116 KB Output is correct
51 Correct 47 ms 16112 KB Output is correct
52 Correct 49 ms 16128 KB Output is correct
53 Correct 42 ms 15956 KB Output is correct
54 Correct 47 ms 15904 KB Output is correct
55 Correct 60 ms 16120 KB Output is correct
56 Correct 35 ms 15860 KB Output is correct
57 Correct 42 ms 16184 KB Output is correct
58 Correct 42 ms 16044 KB Output is correct
59 Correct 46 ms 15916 KB Output is correct
60 Correct 49 ms 15920 KB Output is correct
61 Correct 49 ms 16112 KB Output is correct
62 Correct 52 ms 15972 KB Output is correct
63 Correct 50 ms 16116 KB Output is correct
64 Correct 43 ms 15864 KB Output is correct
65 Correct 46 ms 15952 KB Output is correct
66 Correct 46 ms 16112 KB Output is correct
67 Correct 46 ms 16112 KB Output is correct
68 Correct 46 ms 15960 KB Output is correct
69 Correct 47 ms 15956 KB Output is correct
70 Correct 50 ms 16112 KB Output is correct
71 Correct 269 ms 40436 KB Output is correct
72 Correct 298 ms 40436 KB Output is correct
73 Correct 326 ms 39000 KB Output is correct
74 Correct 493 ms 39416 KB Output is correct
75 Correct 313 ms 41420 KB Output is correct
76 Correct 453 ms 39920 KB Output is correct
77 Correct 409 ms 39504 KB Output is correct
78 Correct 182 ms 43272 KB Output is correct
79 Correct 255 ms 37280 KB Output is correct
80 Correct 257 ms 40512 KB Output is correct
81 Correct 365 ms 40180 KB Output is correct
82 Correct 436 ms 39252 KB Output is correct
83 Correct 263 ms 38640 KB Output is correct
84 Correct 385 ms 39408 KB Output is correct
85 Correct 452 ms 39580 KB Output is correct
86 Correct 154 ms 37344 KB Output is correct
87 Correct 270 ms 40432 KB Output is correct
88 Correct 259 ms 37368 KB Output is correct
89 Correct 344 ms 38964 KB Output is correct
90 Correct 382 ms 39384 KB Output is correct
91 Correct 273 ms 38480 KB Output is correct
92 Correct 441 ms 39668 KB Output is correct
93 Correct 432 ms 39416 KB Output is correct
94 Correct 167 ms 37328 KB Output is correct
95 Correct 357 ms 39416 KB Output is correct
96 Correct 352 ms 39408 KB Output is correct
97 Correct 376 ms 39340 KB Output is correct
98 Correct 385 ms 39476 KB Output is correct
99 Correct 401 ms 39504 KB Output is correct
100 Correct 422 ms 39416 KB Output is correct