#include <bits/stdc++.h>
using namespace std;
static int value[2000000];
static int memo[300][300];
int n , q;
int tleft;
int tostring(string s){
int v = 0;
for(int i = 0;i < n - tleft;i++){
v *= 3;
if(s[i] == '0') v++;
else if(s[i] == '1') v += 2;
}
return v;
}
int dp(string s, int t){
int x = tostring(s);
if(memo[x][t] != -1) return memo[x][t];
for(int i = 0;i < n - tleft;i++){
if(s[i] == '?'){
string s0 = s;
string s1 = s;
s0[i] = '0';
s1[i] = '1';
int ans = dp(s0,t) + dp(s1,t);
//cout << s << " " << ans << "\n";
memo[x][t] = ans;
return ans;
}
}
int v = 0;
for(int i = 0;i < n - tleft;i++){
v *= 2;
if(s[i] == '1') v++;
}
v <<= tleft;
v += t;
//cout << s << " " << t << " " << v << "ffff\n";
memo[x][t] = value[v];
return value[v];
}
vector<int> options;
void genop(string s){
for(int i = 0;i < tleft;i++){
if(s[i] == '?'){
string s0 = s;
string s1 = s;
s0[i] = '0';
s1[i] = '1';
genop(s1);
genop(s0);
return;
}
}
int v = 0;
for(int i = 0;i < tleft;i++){
v <<= 1;
if(s[i] == '1')v++;
}
options.push_back(v);
}
int main()
{
//freopen("i.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
for(int i = 0;i < 6600;i++){
for(int j = 0;j < 4100;j++){
memo[i][j] = -1;
}
}
cin >> n >> q;
string s;
cin >> s;
for(int i = 0;i < s.length();i++){
value[i] = s[i] - '0';
}
int split = 8;
tleft = max(0, n - split);
for(int qq = 0;qq < q;qq++){
cin >> s;
string s1 = "";
for(int i = 0;i < split;i++){
s1 += s[i];
}
string s2 = "";
for(int i = split;i < n;i++){
s2 += s[i];
}
//cout << s1 << " " << s2 << "\n";
int ans = 0;
options.clear();
genop(s2);
for(int ss : options){
//cout << ss << "\n";
ans += dp(s1,ss);
}
cout << ans << "\n";
//cout << dp(s) << "\n";
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:83:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < s.length();i++){
~~^~~~~~~~~~~~
snake_escaping.cpp:76:24: warning: iteration 300 invokes undefined behavior [-Waggressive-loop-optimizations]
memo[i][j] = -1;
~~~~~~~~~~~^~~~
snake_escaping.cpp:75:25: note: within this loop
for(int j = 0;j < 4100;j++){
~~^~~~~~
snake_escaping.cpp:76:24: warning: iteration 300 invokes undefined behavior [-Waggressive-loop-optimizations]
memo[i][j] = -1;
~~~~~~~~~~~^~~~
snake_escaping.cpp:74:21: note: within this loop
for(int i = 0;i < 6600;i++){
~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8192 KB |
Output is correct |
2 |
Correct |
19 ms |
8064 KB |
Output is correct |
3 |
Correct |
19 ms |
8064 KB |
Output is correct |
4 |
Correct |
18 ms |
8064 KB |
Output is correct |
5 |
Incorrect |
20 ms |
8184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8192 KB |
Output is correct |
2 |
Correct |
19 ms |
8064 KB |
Output is correct |
3 |
Correct |
19 ms |
8064 KB |
Output is correct |
4 |
Correct |
18 ms |
8064 KB |
Output is correct |
5 |
Incorrect |
20 ms |
8184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8192 KB |
Output is correct |
2 |
Correct |
19 ms |
8064 KB |
Output is correct |
3 |
Correct |
19 ms |
8064 KB |
Output is correct |
4 |
Correct |
18 ms |
8064 KB |
Output is correct |
5 |
Incorrect |
20 ms |
8184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8192 KB |
Output is correct |
2 |
Correct |
19 ms |
8064 KB |
Output is correct |
3 |
Correct |
19 ms |
8064 KB |
Output is correct |
4 |
Correct |
18 ms |
8064 KB |
Output is correct |
5 |
Incorrect |
20 ms |
8184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8192 KB |
Output is correct |
2 |
Correct |
19 ms |
8064 KB |
Output is correct |
3 |
Correct |
19 ms |
8064 KB |
Output is correct |
4 |
Correct |
18 ms |
8064 KB |
Output is correct |
5 |
Incorrect |
20 ms |
8184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |