답안 #1095985

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1095985 2024-10-03T14:13:29 Z phamducluong Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
445 ms 43248 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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 150 ms 15144 KB Output is correct
12 Correct 148 ms 14672 KB Output is correct
13 Correct 161 ms 13908 KB Output is correct
14 Correct 157 ms 14160 KB Output is correct
15 Correct 150 ms 15188 KB Output is correct
16 Correct 175 ms 14208 KB Output is correct
17 Correct 177 ms 14232 KB Output is correct
18 Correct 101 ms 16028 KB Output is correct
19 Correct 134 ms 13136 KB Output is correct
20 Correct 143 ms 14656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 150 ms 15144 KB Output is correct
12 Correct 148 ms 14672 KB Output is correct
13 Correct 161 ms 13908 KB Output is correct
14 Correct 157 ms 14160 KB Output is correct
15 Correct 150 ms 15188 KB Output is correct
16 Correct 175 ms 14208 KB Output is correct
17 Correct 177 ms 14232 KB Output is correct
18 Correct 101 ms 16028 KB Output is correct
19 Correct 134 ms 13136 KB Output is correct
20 Correct 143 ms 14656 KB Output is correct
21 Correct 151 ms 18256 KB Output is correct
22 Correct 175 ms 18264 KB Output is correct
23 Correct 197 ms 17232 KB Output is correct
24 Correct 201 ms 17024 KB Output is correct
25 Correct 187 ms 19280 KB Output is correct
26 Correct 218 ms 17596 KB Output is correct
27 Correct 201 ms 17484 KB Output is correct
28 Correct 106 ms 20268 KB Output is correct
29 Correct 160 ms 16124 KB Output is correct
30 Correct 162 ms 18260 KB Output is correct
31 Correct 199 ms 18136 KB Output is correct
32 Correct 202 ms 18256 KB Output is correct
33 Correct 174 ms 17024 KB Output is correct
34 Correct 188 ms 17236 KB Output is correct
35 Correct 202 ms 17552 KB Output is correct
36 Correct 108 ms 16464 KB Output is correct
37 Correct 161 ms 18256 KB Output is correct
38 Correct 177 ms 16148 KB Output is correct
39 Correct 203 ms 17400 KB Output is correct
40 Correct 182 ms 17264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 42 ms 16120 KB Output is correct
12 Correct 41 ms 16112 KB Output is correct
13 Correct 45 ms 16116 KB Output is correct
14 Correct 50 ms 16116 KB Output is correct
15 Correct 40 ms 16112 KB Output is correct
16 Correct 49 ms 16108 KB Output is correct
17 Correct 45 ms 16136 KB Output is correct
18 Correct 37 ms 16120 KB Output is correct
19 Correct 37 ms 15856 KB Output is correct
20 Correct 42 ms 16120 KB Output is correct
21 Correct 43 ms 16136 KB Output is correct
22 Correct 47 ms 16100 KB Output is correct
23 Correct 37 ms 15964 KB Output is correct
24 Correct 43 ms 15956 KB Output is correct
25 Correct 45 ms 16116 KB Output is correct
26 Correct 39 ms 15860 KB Output is correct
27 Correct 38 ms 16392 KB Output is correct
28 Correct 37 ms 15860 KB Output is correct
29 Correct 40 ms 16112 KB Output is correct
30 Correct 43 ms 16116 KB Output is correct
31 Correct 38 ms 16076 KB Output is correct
32 Correct 46 ms 16112 KB Output is correct
33 Correct 45 ms 16120 KB Output is correct
34 Correct 33 ms 15852 KB Output is correct
35 Correct 45 ms 16116 KB Output is correct
36 Correct 64 ms 16116 KB Output is correct
37 Correct 43 ms 16136 KB Output is correct
38 Correct 42 ms 16112 KB Output is correct
39 Correct 45 ms 16104 KB Output is correct
40 Correct 49 ms 16012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 150 ms 15144 KB Output is correct
12 Correct 148 ms 14672 KB Output is correct
13 Correct 161 ms 13908 KB Output is correct
14 Correct 157 ms 14160 KB Output is correct
15 Correct 150 ms 15188 KB Output is correct
16 Correct 175 ms 14208 KB Output is correct
17 Correct 177 ms 14232 KB Output is correct
18 Correct 101 ms 16028 KB Output is correct
19 Correct 134 ms 13136 KB Output is correct
20 Correct 143 ms 14656 KB Output is correct
21 Correct 151 ms 18256 KB Output is correct
22 Correct 175 ms 18264 KB Output is correct
23 Correct 197 ms 17232 KB Output is correct
24 Correct 201 ms 17024 KB Output is correct
25 Correct 187 ms 19280 KB Output is correct
26 Correct 218 ms 17596 KB Output is correct
27 Correct 201 ms 17484 KB Output is correct
28 Correct 106 ms 20268 KB Output is correct
29 Correct 160 ms 16124 KB Output is correct
30 Correct 162 ms 18260 KB Output is correct
31 Correct 199 ms 18136 KB Output is correct
32 Correct 202 ms 18256 KB Output is correct
33 Correct 174 ms 17024 KB Output is correct
34 Correct 188 ms 17236 KB Output is correct
35 Correct 202 ms 17552 KB Output is correct
36 Correct 108 ms 16464 KB Output is correct
37 Correct 161 ms 18256 KB Output is correct
38 Correct 177 ms 16148 KB Output is correct
39 Correct 203 ms 17400 KB Output is correct
40 Correct 182 ms 17264 KB Output is correct
41 Correct 42 ms 16120 KB Output is correct
42 Correct 41 ms 16112 KB Output is correct
43 Correct 45 ms 16116 KB Output is correct
44 Correct 50 ms 16116 KB Output is correct
45 Correct 40 ms 16112 KB Output is correct
46 Correct 49 ms 16108 KB Output is correct
47 Correct 45 ms 16136 KB Output is correct
48 Correct 37 ms 16120 KB Output is correct
49 Correct 37 ms 15856 KB Output is correct
50 Correct 42 ms 16120 KB Output is correct
51 Correct 43 ms 16136 KB Output is correct
52 Correct 47 ms 16100 KB Output is correct
53 Correct 37 ms 15964 KB Output is correct
54 Correct 43 ms 15956 KB Output is correct
55 Correct 45 ms 16116 KB Output is correct
56 Correct 39 ms 15860 KB Output is correct
57 Correct 38 ms 16392 KB Output is correct
58 Correct 37 ms 15860 KB Output is correct
59 Correct 40 ms 16112 KB Output is correct
60 Correct 43 ms 16116 KB Output is correct
61 Correct 38 ms 16076 KB Output is correct
62 Correct 46 ms 16112 KB Output is correct
63 Correct 45 ms 16120 KB Output is correct
64 Correct 33 ms 15852 KB Output is correct
65 Correct 45 ms 16116 KB Output is correct
66 Correct 64 ms 16116 KB Output is correct
67 Correct 43 ms 16136 KB Output is correct
68 Correct 42 ms 16112 KB Output is correct
69 Correct 45 ms 16104 KB Output is correct
70 Correct 49 ms 16012 KB Output is correct
71 Correct 264 ms 40440 KB Output is correct
72 Correct 316 ms 40472 KB Output is correct
73 Correct 295 ms 38900 KB Output is correct
74 Correct 445 ms 39388 KB Output is correct
75 Correct 295 ms 41400 KB Output is correct
76 Correct 427 ms 39664 KB Output is correct
77 Correct 385 ms 39412 KB Output is correct
78 Correct 151 ms 43248 KB Output is correct
79 Correct 228 ms 37208 KB Output is correct
80 Correct 255 ms 40432 KB Output is correct
81 Correct 366 ms 40576 KB Output is correct
82 Correct 423 ms 39408 KB Output is correct
83 Correct 247 ms 38640 KB Output is correct
84 Correct 377 ms 39408 KB Output is correct
85 Correct 398 ms 39464 KB Output is correct
86 Correct 148 ms 37360 KB Output is correct
87 Correct 238 ms 40432 KB Output is correct
88 Correct 233 ms 37360 KB Output is correct
89 Correct 287 ms 39108 KB Output is correct
90 Correct 344 ms 39256 KB Output is correct
91 Correct 252 ms 38464 KB Output is correct
92 Correct 402 ms 39624 KB Output is correct
93 Correct 385 ms 39496 KB Output is correct
94 Correct 176 ms 37260 KB Output is correct
95 Correct 338 ms 39408 KB Output is correct
96 Correct 347 ms 39348 KB Output is correct
97 Correct 343 ms 39408 KB Output is correct
98 Correct 345 ms 39408 KB Output is correct
99 Correct 318 ms 39408 KB Output is correct
100 Correct 347 ms 39452 KB Output is correct