This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = (1<<20)+5;
int arr[N];
map<string, int> mp;
void sol() {
int n, q; cin >> n >> q;
int sum = 0;
for (int i = 0; i < (1<<n); i++) {char x; cin >> x; arr[i] = x-'0'; sum += arr[i];}
vector<string> nums;
for (int i = 0; i < (1<<n); i++) {
string s(n, '0');
for (int j = 0; j < n; j++) {
if (i&(1<<j)) s[j] = ('1');
}
reverse(all(s));
nums.pb(s);
mp[s] = arr[i];
}
while (q--) {
string s; cin >> s;
if (mp[s]) {cout << mp[s] << endl; continue;}
int ans = 0;
for (auto curr : nums) {
bool f = true;
for (int j = 0; j < n; j++) {
if (s[j] == curr[j] || s[j] == '?') continue;
else {f = false; break;}
}
if (f) ans += mp[curr];
}
cout << ans << endl;
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |