#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const ll MAX_N = 1 << 20;
const ll MAX_RIGHT = 1600000; ll MAX_LEFT = 7;
ll pow3[MAX_N];
ll dp[MAX_RIGHT];
ll price[MAX_N];
string queries[MAX_N];
ll ans[MAX_N];
ll n, q;
vector<ll> toTernary(ll x) {
vector<ll> ret;
for(ll i = 0; i < n - MAX_LEFT; i ++) {
ret.push_back(x % 3);
x /= 3;
}
return ret;
}
void getAns(ll mask) {
//cout << "Getting " << mask << endl;
vector<ll> indices;
for(ll i = 0; i < q; i ++) {
for(ll j = 0; j < MAX_LEFT; j ++) {
if(mask & (1 << j)) {
if(queries[i][j] == '0') {
break;
}
} else {
if(queries[i][j] == '1') {
break;
}
}
if(j == MAX_LEFT - 1) {
indices.push_back(i);
}
}
}
ll triBits = n - MAX_LEFT;
for(ll i = 0; i < pow3[n - MAX_LEFT]; i ++) {
vector<ll> tern = toTernary(i);
bool flag = false;
for(ll j = 0; j < tern.size(); j ++) {
if(tern[j] == 2) {
dp[i] = dp[i - pow3[j]] + dp[i - 2 * pow3[j]];
flag = true;
break;
}
}
if(!flag) {
ll currMask = mask;
for(ll j = 0; j < tern.size(); j ++) {
currMask += tern[j] * (1 << (j + MAX_LEFT));
}
dp[i] = price[currMask];
}
continue;
cout << "Testing " << i << endl;
for(auto it : tern) {
cout << it;
}
cout << endl;
cout << i << " " << dp[i] << endl;
}
for(auto it : indices) {
ll currMask = 0;
for(ll j = n - 1; j >= MAX_LEFT; j --) {
currMask *= 3;
currMask += (ll)(queries[it][j] - '0');
}
//cout << it << " " << out(dp[currMask]) << " " << currMask << endl;
ans[it] += dp[currMask];
}
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
pow3[0] = 1;
for(ll i = 1; i < MAX_N; i ++) {
pow3[i] = pow3[i - 1] * 3;
}
cin >> n >> q;
chkmin(MAX_LEFT, n);
string in;
cin >> in;
for(ll i = 0; i < (1 << n); i ++) {
price[i] = in[i] - '0';
}
for(ll i = 0; i < q; i ++) {
cin >> queries[i];
reverse(queries[i].begin(), queries[i].end());
for(auto &it : queries[i]) {
if(it == '?') {
it = '2';
}
}
}
for(ll mask = 0; mask < (1 << MAX_LEFT); mask ++) {
getAns(mask);
}
for(ll i = 0; i < q; i ++) {
cout << ans[i] << endl;
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'void getAns(ll)':
snake_escaping.cpp:56:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(ll j = 0; j < tern.size(); j ++) {
| ~~^~~~~~~~~~~~~
snake_escaping.cpp:65:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(ll j = 0; j < tern.size(); j ++) {
| ~~^~~~~~~~~~~~~
snake_escaping.cpp:52:5: warning: unused variable 'triBits' [-Wunused-variable]
52 | ll triBits = n - MAX_LEFT;
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
41472 KB |
Output is correct |
2 |
Correct |
32 ms |
41472 KB |
Output is correct |
3 |
Correct |
32 ms |
41464 KB |
Output is correct |
4 |
Correct |
33 ms |
41464 KB |
Output is correct |
5 |
Correct |
33 ms |
41464 KB |
Output is correct |
6 |
Correct |
33 ms |
41472 KB |
Output is correct |
7 |
Correct |
33 ms |
41464 KB |
Output is correct |
8 |
Correct |
33 ms |
41472 KB |
Output is correct |
9 |
Correct |
32 ms |
41464 KB |
Output is correct |
10 |
Correct |
32 ms |
41472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
41472 KB |
Output is correct |
2 |
Correct |
32 ms |
41472 KB |
Output is correct |
3 |
Correct |
32 ms |
41464 KB |
Output is correct |
4 |
Correct |
33 ms |
41464 KB |
Output is correct |
5 |
Correct |
33 ms |
41464 KB |
Output is correct |
6 |
Correct |
33 ms |
41472 KB |
Output is correct |
7 |
Correct |
33 ms |
41464 KB |
Output is correct |
8 |
Correct |
33 ms |
41472 KB |
Output is correct |
9 |
Correct |
32 ms |
41464 KB |
Output is correct |
10 |
Correct |
32 ms |
41472 KB |
Output is correct |
11 |
Execution timed out |
2090 ms |
65536 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
41472 KB |
Output is correct |
2 |
Correct |
32 ms |
41472 KB |
Output is correct |
3 |
Correct |
32 ms |
41464 KB |
Output is correct |
4 |
Correct |
33 ms |
41464 KB |
Output is correct |
5 |
Correct |
33 ms |
41464 KB |
Output is correct |
6 |
Correct |
33 ms |
41472 KB |
Output is correct |
7 |
Correct |
33 ms |
41464 KB |
Output is correct |
8 |
Correct |
33 ms |
41472 KB |
Output is correct |
9 |
Correct |
32 ms |
41464 KB |
Output is correct |
10 |
Correct |
32 ms |
41472 KB |
Output is correct |
11 |
Execution timed out |
2090 ms |
65536 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
41472 KB |
Output is correct |
2 |
Correct |
32 ms |
41472 KB |
Output is correct |
3 |
Correct |
32 ms |
41464 KB |
Output is correct |
4 |
Correct |
33 ms |
41464 KB |
Output is correct |
5 |
Correct |
33 ms |
41464 KB |
Output is correct |
6 |
Correct |
33 ms |
41472 KB |
Output is correct |
7 |
Correct |
33 ms |
41464 KB |
Output is correct |
8 |
Correct |
33 ms |
41472 KB |
Output is correct |
9 |
Correct |
32 ms |
41464 KB |
Output is correct |
10 |
Correct |
32 ms |
41472 KB |
Output is correct |
11 |
Runtime error |
560 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
41472 KB |
Output is correct |
2 |
Correct |
32 ms |
41472 KB |
Output is correct |
3 |
Correct |
32 ms |
41464 KB |
Output is correct |
4 |
Correct |
33 ms |
41464 KB |
Output is correct |
5 |
Correct |
33 ms |
41464 KB |
Output is correct |
6 |
Correct |
33 ms |
41472 KB |
Output is correct |
7 |
Correct |
33 ms |
41464 KB |
Output is correct |
8 |
Correct |
33 ms |
41472 KB |
Output is correct |
9 |
Correct |
32 ms |
41464 KB |
Output is correct |
10 |
Correct |
32 ms |
41472 KB |
Output is correct |
11 |
Execution timed out |
2090 ms |
65536 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |