#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define sui cout.tie(NULL); cin.tie(NULL); ios_base::sync_with_stdio(false)
const int MAX_N = 1e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
const int LOG = 30;
pair<int, int> dp[1 << 20][21];
void solve() {
int n, m;
cin >> n >> m;
int num[n];
vector<int> al[1 << m];
for (int i = 0; i < n; i++)
{
num[i] = 0;
for (int j = 0; j < m; j++)
{
int x;
cin >> x;
if (x) num[i] += (1 << j);
}
al[num[i]].push_back(i);
}
for (int i = 0; i < (1 << m); i++)
{
if (al[i].size() >= 2) dp[i][m] = {al[i][0], al[i][1]};
else if (al[i].size() == 1) dp[i][m] = {al[i][0], -1};
else dp[i][m] = {-1, -1};
}
for (int j = m - 1; j >= 0; j--)
{
for (int i = 0; i < (1 << m); i++)
{
if (i & (1 << j))
{
vector<pair<int, int>> al1;
if (dp[i][j + 1].first != -1) al1.push_back({1, dp[i][j + 1].first});
if (dp[i][j + 1].second != -1) al1.push_back({1, dp[i][j + 1].second});
if (dp[i ^ (1 << j)][j + 1].first != -1) al1.push_back({0, dp[i ^ (1 << j)][j + 1].first});
if (dp[i ^ (1 << j)][j + 1].second != -1) al1.push_back({0, dp[i ^ (1 << j)][j + 1].second});
for (int p = 0; p < al1.size(); p++) al1[p].first += __builtin_popcount(num[al1[p].second] & i);
sort(all(al1));
if (al1.size() >= 2) dp[i][j] = {al1[0].second, al1[1].second};
else if (al1.size() == 1) dp[i][j] = {al1[0].second, -1};
else dp[i][j] = {-1, -1};
} else
{
vector<pair<int, int>> al1;
if (dp[i][j + 1].first != -1) al1.push_back({0, dp[i][j + 1].first});
if (dp[i][j + 1].second != -1) al1.push_back({0, dp[i][j + 1].second});
if (dp[i ^ (1 << j)][j + 1].first != -1) al1.push_back({0, dp[i ^ (1 << j)][j + 1].first});
if (dp[i ^ (1 << j)][j + 1].second != -1) al1.push_back({0, dp[i ^ (1 << j)][j + 1].second});
for (int p = 0; p < al1.size(); p++) al1[p].first += __builtin_popcount(num[al1[p].second] & i);
sort(all(al1));
if (al1.size() >= 2) dp[i][j] = {al1[0].second, al1[1].second};
else if (al1.size() == 1) dp[i][j] = {al1[0].second, -1};
else dp[i][j] = {-1, -1};
}
}
}
int cnt[m];
fill(cnt, cnt + m, 0);
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (num[i] & (1 << j)) cnt[j]++;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++) if (num[i] & (1 << j)) cnt[j]--;
int mask = 0;
int cnt1 = 0;
for (int j = 0; j < m; j++)
{
if (cnt[j] >= n / 2 + 1) cnt1++;
else if (cnt[j] == n / 2) mask ^= (1 << j), cnt1++;
}
auto num1 = dp[mask][0];
if (num1.first == i) cnt1 -= __builtin_popcount(num[num1.second] & mask);
else cnt1 -= __builtin_popcount(num[num1.first] & mask);
cout << cnt1 << "\n";
for (int j = 0; j < m; j++) if (num[i] & (1 << j)) cnt[j]++;
}
}
int main() {
sui;
int tc = 1;
//cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
}
}
| # | 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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |