|
| 1 | +/* |
| 2 | + Idea: |
| 3 | + - Precalculate the frequency of each element in the multiset S. |
| 4 | + - Precalculate the cost of each mask from 0 to 2^n. |
| 5 | + - Precalculate the number of elements for each pair (i, j) |
| 6 | + that has `k` as cost. |
| 7 | + - Prefix sum the previous point. |
| 8 | + - Answer the queries in O(n). |
| 9 | +*/ |
| 10 | + |
| 11 | +#include <bits/stdc++.h> |
| 12 | + |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +int const N = 12; |
| 16 | +char s[13]; |
| 17 | +int n, m, q, k, a[N], c[1 << N], fr[1 << N], all[1 << N][1201]; |
| 18 | + |
| 19 | +int main() { |
| 20 | + scanf("%d %d %d", &n, &m, &q); |
| 21 | + for(int i = 0; i < n; ++i) |
| 22 | + scanf("%d", a + i); |
| 23 | + for(int i = 0, tmp; i < m; ++i) { |
| 24 | + scanf("%s", s); |
| 25 | + tmp = 0; |
| 26 | + for(int j = 0; j < n; ++j) |
| 27 | + tmp |= ((s[j] - '0') << (n - j - 1)); |
| 28 | + ++fr[tmp]; |
| 29 | + } |
| 30 | + |
| 31 | + for(int i = 0; i < (1 << n); ++i) |
| 32 | + for(int j = 0; j < n; ++j) |
| 33 | + if(((i >> j) & 1) != 0) |
| 34 | + c[i] += a[n - j - 1]; |
| 35 | + |
| 36 | + for(int i = 0; i < (1 << n); ++i) |
| 37 | + for(int j = 0, x; j < (1 << n); ++j) { |
| 38 | + x = 0; |
| 39 | + for(int l = 0; l < n; ++l) |
| 40 | + if(((i >> l) & 1) == ((j >> l) & 1)) |
| 41 | + x |= (1 << l); |
| 42 | + |
| 43 | + all[i][c[x]] += fr[j]; |
| 44 | + } |
| 45 | + |
| 46 | + for(int i = 0; i < (1 << n); ++i) |
| 47 | + for(int j = 1; j < 1201; ++j) |
| 48 | + all[i][j] += all[i][j - 1]; |
| 49 | + |
| 50 | + for(int i = 0, cur; i < q; ++i) { |
| 51 | + scanf("%s %d", s, &k); |
| 52 | + cur = 0; |
| 53 | + for(int j = 0; j < n; ++j) |
| 54 | + cur |= ((s[j] - '0') << (n - j - 1)); |
| 55 | + printf("%d\n", all[cur][k]); |
| 56 | + } |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
0 commit comments