제출 #1339608

#제출 시각아이디문제언어결과실행 시간메모리
1339608NAMINCouncil (JOI23_council)C++20
100 / 100
905 ms97400 KiB
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;

int popcnt(int x) {
	int ans = 0;
	while(x>0){
		ans += x & 1;
		x >>= 1;
	}
	return ans;
}

void solve(){
	int N,M;
	cin >> N >> M;
	vector<int> a;
	vector<int> b;
	vector<int> tot_vote(M,0);
	for(int i=0;i<N;i++){
		int x=0;
		for(int j=0;j<M;j++){
			int d;
			cin >> d;
			if(d==1){
				x|=(1<<j);
				tot_vote[j]++;
			}
		}
		a.push_back(x);
	}
	for(int i=0;i<N;i++){
		int x = 0;
		for(int j=0;j<M;j++){
			if((a[i]&(1<<j))==0){
				x|=(1<<j);
			}
		}
		b.push_back(x);
	}

	vector<int> f((1<<M),0);
	for(int i=0;i<N;i++){
		f[b[i]]++;
	}
	for(int i=0;i<M;i++){
		for(int x=0;x<(1<<M);x++){
			if(((1<<i)&x)==0){
				f[x]+=f[x|(1<<i)];
			}
		}
	}

	vector<vector<int>> g(M+1,vector<int>((1<<M)+1,0));
	for(int x=0;x<(1<<M);x++){
		g[popcnt(x)][x]=f[x];
	}
	for(int k=0;k<=M;k++){
		for(int i=0;i<M;i++){
			for(int x=0;x<(1<<M);x++){
				if(((1<<i)&x)!=0){
					g[k][x]+=g[k][x^(1<<i)];
				}
			}
		}
	}

	vector<ll> fact(M+1,1);
	for(int i=1;i<=M;i++){
		fact[i]=fact[i-1]*i;
	}

	auto C = [&](int n,int k){
		//assert(n>=k);
		if(n<k)
			return 0LL;
		return fact[n]/(fact[k]*fact[n-k]);
	};
	

	for(int i=0;i<N;i++){
		int one=0,two=0,zero=0;
		for(int j=0;j<M;j++){
			if(tot_vote[j]>=N/2+2)
				two|=(1<<j);
			else if(tot_vote[j]==N/2+1)
				one|=(1<<j);
			else if(tot_vote[j]==N/2)
				zero|=(1<<j);
		}
		int good=popcnt(two|(~a[i]&one));
		int z=(zero&~a[i])|(one&a[i]);

		int kmx=0;
		for(int k=0;k<=M;k++){
			int c = popcnt(b[i]&z);
			if(g[k][z]-C(c,k)>0){
				kmx=k;
			}
		}
		cout << good+kmx << endl;
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...