// #pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define ull unsigned long long
#define ld long double
#define ff first
#define ss second
#define ln "\n"
#define mp make_pair
#define pb push_back
#define INF (ll)2e18
#define MOD (ll)(1e9+7)
void solve(){
ll n, m; cin >> n >> m;
vector<vector<pair<ll, ll>>> gr(1<<m);
for (ll i=0; i<(1<<m); i++){
for (ll j=0; j<m; j++){
if (i&(1<<j)){
gr[i].push_back({i^(1<<j), 1});
}else{
gr[i].push_back({i^(1<<j), 0});
}
}
}
vector<ll> vals(n);
vector<vector<pair<ll, ll>>> cost(1<<m, vector<pair<ll, ll>>(2, {INF, -1}));
queue<pair<ll, ll>> costcur, costnext;
for (ll i=0; i<n; i++){
ll res=0; for (ll j=0; j<m; j++){
ll x; cin >> x;
if (x) res+=(1<<j);
}
vals[i]=res;
costcur.push({res, i});
}
for (ll w=0; w<(1<<(m+1)); w++){
while (!costcur.empty()){
ll u, cf; tie(u, cf)=costcur.front();
costcur.pop();
if (cost[u][0].ff==INF){
cost[u][0] = {w, cf};
}else if (cost[u][1].ff==INF and cf!=cost[u][0].ss){
cost[u][1] = {w, cf};
}else continue;
for (auto [v, c]:gr[u]){
if (cost[v][0].ff==INF) {
if (w+c==w) costcur.push({v, cf});
else costnext.push({v, cf});
}else if (cost[v][1].ff==INF and cost[v][0].ss!=cf){
if (w+c==w) costcur.push({v, cf});
else costnext.push({v, cf});
}
}
}
swap(costcur, costnext);
}
vector<ll> cnt(m);
for (ll i=0; i<n; i++) for (ll j=0; j<m; j++) if (vals[i]&(1<<j)) cnt[j]++;
for (ll i=0; i<n; i++){
for (ll j=0; j<m; j++){
if (vals[i]&(1<<j)) cnt[j]--;
}
ll res=0, find=0;
for (ll j=0; j<m; j++){
if (cnt[j]>n/2){
res++; find+=(1<<j);
}else if (cnt[j]<n/2){
find+=(1<<j);
}else if (cnt[j]==n/2){
res++;
}
}
// cout << find << ": ";
cout << res-(cost[find][0].ss==i?cost[find][1].ff:cost[find][0].ff) << ln;
for (ll j=0; j<m; j++){
if (vals[i]&(1<<j)) cnt[j]++;
}
}
}
/*
9-2
101110010001
*/
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#ifdef LOCAL
auto start = chrono::high_resolution_clock::now();
#endif
ll t=1;
// cin >> t;
for (ll c=1; c<=t; c++) solve();
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << setprecision(0) << fixed << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
}
# | 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... |