Skip to content

Commit 51aee0e

Browse files
committed
Solved problem 1017D from codeforces
1 parent 6002b17 commit 51aee0e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

CodeForces/1017D. The Wu.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

CodeForces/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@
551551
- [1016B. Segment Occurrences](http://codeforces.com/contest/1016/problem/B)
552552
- [1016C. Vasya And The Mushrooms](http://codeforces.com/contest/1016/problem/C)
553553
- [1016D. Vasya And The Matrix](http://codeforces.com/contest/1016/problem/D)
554+
- [1017D. The Wu](https://codeforces.com/contest/1017/problem/D)
554555
- [1020A. New Building for SIS](http://codeforces.com/contest/1020/problem/A)
555556
- [1020B. Badge](http://codeforces.com/contest/1020/problem/B)
556557
- [1020C. Elections](http://codeforces.com/contest/1020/problem/C)

0 commit comments

Comments
 (0)