# CF662C Binary Table——FWTXOR

2022-01-04 21:02:21  阅读：65  来源： 互联网

# 题解

A n s = min ⁡ x ∈ [ 0 , 2 n ) { ∑ i = 0 m − 1 f ( a i   x o r   x ) } Ans=\min_{x\in [0,2^n)}\{\sum_{i=0}^{m-1}f(a_i \,xor\,x)\} Ans=x∈[0,2n)min​{i=0∑m−1​f(ai​xorx)}

A n s = min ⁡ x ∈ [ 0 , 2 n ) { ∑ i = 0 2 n − 1 f ( i   x o r   x ) ⋅ b i } Ans=\min_{x\in [0,2^n)}\{\sum_{i=0}^{2^n-1}f(i \,xor\,x)\cdot b_i\} Ans=x∈[0,2n)min​{i=0∑2n−1​f(ixorx)⋅bi​}

A n s = min ⁡ x ∈ [ 0 , 2 n ) { ∑ i   x o r   j = x f ( j ) ⋅ b i } Ans=\min_{x\in [0,2^n)}\{\sum_{i\,xor\,j=x}f(j)\cdot b_i\} Ans=x∈[0,2n)min​{ixorj=x∑​f(j)⋅bi​}

# 代码

#include<bits/stdc++.h>//JZM yyds!!
#define ll long long
#define uns unsigned
#define IF (it->first)
#define IS (it->second)
#define END putchar('\n')
using namespace std;
const int MAXN=(1<<20)+5;
const ll INF=1e18;
ll x=0;bool f=1;char s=getchar();
while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+(s^48),s=getchar();
return f?x:-x;
}
int ptf[30],lpt;
inline void print(ll x,char c='\n'){
if(x<0)putchar('-'),x=-x;
ptf[lpt=1]=x%10;
while(x>9)x/=10,ptf[++lpt]=x%10;
while(lpt)putchar(ptf[lpt--]^48);
putchar(c);
}

const ll MOD=1e9+7;//习惯性取模
int n,m,k,in[MAXN];
char s[MAXN];
ll a[MAXN],b[MAXN],c[MAXN],ans;
inline void FWTXOR(ll*a,int n,int inv){
ll in2=inv>0?1:((MOD+1)>>1),a0,a1;
for(int k=n;k>1;k>>=1)
for(int i=0,d=k>>1;i<n;i+=k)
for(int j=i;j<i+d;j++)
a0=a[j],a1=a[j+d],a[j]=(a0+a1)*in2%MOD,a[j+d]=(a0-a1+MOD)*in2%MOD;
}
signed main()
{
for(int j=0;j<m;j++){
scanf("%s",s);
for(int i=0;i<n;i++)if(s[i]=='1')in[i]|=(1<<j);
}
k=(1<<m);
for(int i=0;i<n;i++)a[in[i]]++;
for(int i=0;i<k;i++)b[i]=b[i>>1]+(i&1);
for(int i=0;i<k;i++)b[i]=min(b[i],m-b[i]);
FWTXOR(a,k,1),FWTXOR(b,k,1);
for(int i=0;i<k;i++)c[i]=a[i]*b[i]%MOD;
FWTXOR(c,k,-1);
ans=n*m;
for(int i=0;i<k;i++)ans=min(ans,c[i]);
print(ans);
return 0;
}