[Love-Hate](https://codeforces.com/contest/1523/problem/D) is one of my favourite questions in Deltix Round.
## Problem Statement
William is hosting a party for 𝑛 of his trader friends. They started a discussion on various currencies they trade, but there's an issue: not all of his trader friends like every currency. They like some currencies, but not others.
For each William's friend 𝑖 it is known whether he likes currency j. There are m currencies in total. It is also known that a trader may not like more than p currencies.
Because friends need to have some common topic for discussions they need to find the largest by cardinality (possibly empty) subset of currencies, such that there are at least ⌈n / 2⌉ friends (rounded up) who like each currency in this subset.
### Input
The first line contains three integers 𝑛,𝑚 and 𝑝 (1 <= n <= 2 * 10 ^ 5,1 <= p <= m <= 60,1 <= p <=15), which is the number of trader friends, the number of currencies, the maximum number of currencies each friend can like.
Each of the next 𝑛 lines contain 𝑚 characters. The 𝑗-th character of 𝑖-th line is 1 if friend 𝑖 likes the currency 𝑗 and 0 otherwise. It is guaranteed that the number of ones in each line does not exceed 𝑝.
###Output
Print a string of length 𝑚, which defines the subset of currencies of the maximum size, which are liked by at least half of all friends. Currencies belonging to this subset must be signified by the character 1.
If there are multiple answers, print any.
# Approach
Since the final answer will be a submask of one of ⌈n / 2⌉ friends, we can use a randomized solution. First we shuffle the all the friends and take N of them as the probability of failing is $ 1 : 2 ^ N$. We can take $ N = min(20, n) $ where $ n $ is the number of trader friends. To shuffle our input $ s $, we can use the built in function ``shuffle`` with ``rng``.
```
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
shuffle(s.begin(), s.end(), rng);
```
Then we can "compress" each mask for each friend to a size no larger than $ p $ by only keeping those true bits which are also true in mask. For exmaple, if $s[it]$ is $ 11001$ then bits would be $[0, 1, 4]$.
```cpp
for(int it = 0, it < min(20, (int) s.size())) {
vector<int> bits;
for(int b = 0; b < m; b++) {
if(s[it][b] == '1') {
bits.push_back(b);
}
}
}
```
We can use ``exact_cnt[mask]`` to store the number of friends who like exactly the currency mask. Let's say we have $ K $ ones in ``b``, then we will have $ 2 ^ K $ possible masks. For example, if $ K = 2 $, then we will have $2 ^ 2 = 4$ masks which are $[00, 01, 10, 11]$. To find out all the counts, we can iterate each friend and build the mask based on the set bits of the first friend.
```
int K = bits.size();
vector<int> exact_cnt(1 << K, 0);
for(auto x : s) {
int mask = 0;
for(int i = 0; i < K; i++) mask |= (x[bits[i]] - '0') << i;
exact_cnt[mask]++;
}
```
Then we use at_least_cnt[mask] to store the number of friends who like at least the currency mask. Given a mask, we need to find the sum of all its supermask in ``exact_cnt``. For example, $111$, $110$, $101$, and $100$ are the supermask of $100$ because the MSB is $1$. To convert from exact_cnt to at_least_cnt, we can adopt an [$O(3 ^ N) $ solution](https://cp-algorithms.com/algebra/all-submasks.html) or $O(N * 2 ^ N) $ using SOS DP and invert all the bitmasks.
```
template<typename T_out, typename T_in>
vector
submask_sums(int n, const vector &values) {
assert(int(values.size()) == 1 << n);
vector dp(values.begin(), values.end());
for (int i = 0; i < n; i++)
for (int base = 0; base < 1 << n; base += 1 << (i + 1))
for (int mask = base; mask < base + (1 << i); mask++)
dp[mask + (1 << i)] += dp[mask];
return dp;
}
template<typename T_out, typename T_in>
vector supermask_sums(int n, vector values) {
reverse(values.begin(), values.end());
vector result = submask_sums(n, values);
reverse(result.begin(), result.end());
return result;
}
```
After that, we can check each mask to see if it has at least ⌈n / 2⌉ friends (rounded up) who like each currency in this subset and checks if it is larger by cardinality subset of currencies. If so, we update our temp answer by decompressing back to the full mask.
```
for(int mask = 0; mask < (1 << K); i++) {
if(
2 * at_least_cnt[mask] >= n &&
__builtin_popcount(mask) > __builtin_popcountll(best)
) {
ll full_mask = 0;
for(int i = 0; i < K; i++) full_mask |= (long long)(mask >> i & 1) << bits[i];
best = full_mask;
}
}
```
At the end, we output the ans in binary format.
```
for(int i = 0; i < m; i++) cout << (best >> i & 1);
```
The full solution can be found [here](https://github.com/wingkwong/competitive-programming/blob/master/codeforces/contests/1523/D.cpp).