#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
const int B1 = 7;
const int N = 1<<B1;
vector<array<int,2>> v, w[N];
string s;
int l, q, ans[1000010], dp[1594324];
int calc(string lol){
int p = 1, ans = 0, xd;
for(int i = 0; i < sz(lol); i++){
if(lol[i]=='?') xd=2;
else xd = lol[i]-'0';
ans+=xd*p, p*=3;
}
return ans;
}
int calc(int lol){
int p = 1, ans = 0;
for(int i = 0; i < B1; i++)
ans+=(lol>>i&1)*p, p*=3;
return ans;
}
int submask(int x, int y){
for(int i = 0; i < 20; i++,x/=3, y/=3){
int r1 = x%3, r2 = y%3;
if(r1==2) continue;
if(r1!=r2) return false;
}
return true;
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> l >> q >> s;
for(int mask = 0; mask < (1<<l); mask++){
int m1 = 0, m2 = mask;
if(l>B1) m1 = mask&((1<<B1)-1), m2>>=B1;
w[m1].pb({calc(m2),s[mask]-'0'});
}
for(int i = 0; i < q; i++){
string t; cin >> t;
reverse(all(t)); string x="";
if(l>B1) x = t.substr(0,B1),t=t.substr(B1);
v.pb({calc(x),calc(t)});
}
for(int _ = 0; _ < N; _++){
if(!sz(v[_])) continue;
memset(dp,0,sizeof(dp));
for(auto [xd,sc] : w[_]) dp[xd]+=sc;
for(int i = 0; i < 1594324; i++){
int xd = i, p = 1, ok = 0;
while(xd){
int r = xd%3;
if(r==2) {
dp[i]+=dp[i-2*p]+dp[i-p];
ok = 1;
break;
}
xd/=3; p*=3;
}
}
int z = calc(_);
for(int i = 0; i < q; i++){
auto [x,y] = v[i];
if(submask(x,z)) ans[i]+=dp[y];
}
}
for(int i = 0; i < q; i++) cout << ans[i] << "\n";
}
Compilation message
snake_escaping.cpp: In function 'int32_t main()':
snake_escaping.cpp:57:23: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
57 | int xd = i, p = 1, ok = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
794 ms |
6820 KB |
Output is correct |
2 |
Correct |
842 ms |
6820 KB |
Output is correct |
3 |
Correct |
822 ms |
6816 KB |
Output is correct |
4 |
Correct |
774 ms |
6816 KB |
Output is correct |
5 |
Correct |
795 ms |
6736 KB |
Output is correct |
6 |
Correct |
773 ms |
6816 KB |
Output is correct |
7 |
Correct |
793 ms |
6816 KB |
Output is correct |
8 |
Correct |
786 ms |
6736 KB |
Output is correct |
9 |
Correct |
846 ms |
6812 KB |
Output is correct |
10 |
Correct |
815 ms |
6904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
794 ms |
6820 KB |
Output is correct |
2 |
Correct |
842 ms |
6820 KB |
Output is correct |
3 |
Correct |
822 ms |
6816 KB |
Output is correct |
4 |
Correct |
774 ms |
6816 KB |
Output is correct |
5 |
Correct |
795 ms |
6736 KB |
Output is correct |
6 |
Correct |
773 ms |
6816 KB |
Output is correct |
7 |
Correct |
793 ms |
6816 KB |
Output is correct |
8 |
Correct |
786 ms |
6736 KB |
Output is correct |
9 |
Correct |
846 ms |
6812 KB |
Output is correct |
10 |
Correct |
815 ms |
6904 KB |
Output is correct |
11 |
Execution timed out |
2005 ms |
24528 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
794 ms |
6820 KB |
Output is correct |
2 |
Correct |
842 ms |
6820 KB |
Output is correct |
3 |
Correct |
822 ms |
6816 KB |
Output is correct |
4 |
Correct |
774 ms |
6816 KB |
Output is correct |
5 |
Correct |
795 ms |
6736 KB |
Output is correct |
6 |
Correct |
773 ms |
6816 KB |
Output is correct |
7 |
Correct |
793 ms |
6816 KB |
Output is correct |
8 |
Correct |
786 ms |
6736 KB |
Output is correct |
9 |
Correct |
846 ms |
6812 KB |
Output is correct |
10 |
Correct |
815 ms |
6904 KB |
Output is correct |
11 |
Execution timed out |
2005 ms |
24528 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
794 ms |
6820 KB |
Output is correct |
2 |
Correct |
842 ms |
6820 KB |
Output is correct |
3 |
Correct |
822 ms |
6816 KB |
Output is correct |
4 |
Correct |
774 ms |
6816 KB |
Output is correct |
5 |
Correct |
795 ms |
6736 KB |
Output is correct |
6 |
Correct |
773 ms |
6816 KB |
Output is correct |
7 |
Correct |
793 ms |
6816 KB |
Output is correct |
8 |
Correct |
786 ms |
6736 KB |
Output is correct |
9 |
Correct |
846 ms |
6812 KB |
Output is correct |
10 |
Correct |
815 ms |
6904 KB |
Output is correct |
11 |
Incorrect |
1056 ms |
21368 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
794 ms |
6820 KB |
Output is correct |
2 |
Correct |
842 ms |
6820 KB |
Output is correct |
3 |
Correct |
822 ms |
6816 KB |
Output is correct |
4 |
Correct |
774 ms |
6816 KB |
Output is correct |
5 |
Correct |
795 ms |
6736 KB |
Output is correct |
6 |
Correct |
773 ms |
6816 KB |
Output is correct |
7 |
Correct |
793 ms |
6816 KB |
Output is correct |
8 |
Correct |
786 ms |
6736 KB |
Output is correct |
9 |
Correct |
846 ms |
6812 KB |
Output is correct |
10 |
Correct |
815 ms |
6904 KB |
Output is correct |
11 |
Execution timed out |
2005 ms |
24528 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |