题目

Source

http://codeforces.com/contest/663/problem/E

Description

You are given a table consisting of n rows and m columns. Each cell of the table contains either 0 or 1. In one move, you are allowed to pick any row or any column and invert all values, that is, replace 0 by 1 and vice versa.

What is the minimum number of cells with value 1 you can get after applying some number of operations?

Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 20, 1 ≤ m ≤ 100 000) — the number of rows and the number of columns, respectively.

Then n lines follows with the descriptions of the rows. Each line has length m and contains only digits '0' and '1'.

Output

Output a single integer — the minimum possible number of ones you can get after applying some sequence of operations.

Sample Input

3 4
0110
1010
0111

Sample Output

2

分析

题目大概说有一个n*m的01矩阵,每次可以选择将矩阵一整行或者一整列反转,要使最终矩阵里的1数量最少,问最少是多少。

由于n最大20,容易想到暴力做法(POJ3279),枚举各行是否反转的状态,然后遍历每一列累加各列能得到的最少1的个数。
然后就没有然后了。。

这题的解法这篇博客写得挺清楚的:http://taosama.github.io/2016/09/21/Codeforces%20662C%20C.%20Binary%20Table%EF%BC%88FWT%EF%BC%89/

  • $f[msk]=\sum_{k \in [0,\ 2^n) }cnt_k\times min(Ones_{msk\oplus k},\ n-Ones_{msk\oplus k})\ \ (cnt_k表示状态为k的列的个数)$

得出那个卷积,用FWT去搞,时间复杂度$O(2^nlog2^n)$,即$O(n2^n)$。

感觉这题挺有意思的。。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN (1<<20) void FWT(long long *a,int n){
for(int d=1; d<n; d<<=1){
for(int m=d<<1,i=0; i<n; i+=m){
for(int j=0; j<d; ++j){
long long x=a[i+j],y=a[i+j+d];
a[i+j]=x+y; a[i+j+d]=x-y;
}
}
}
}
void UFWT(long long *a,int n){
for(int d=1; d<n; d<<=1){
for(int m=d<<1,i=0; i<n; i+=m){
for(int j=0; j<d; ++j){
long long x=a[i+j],y=a[i+j+d];
a[i+j]=(x+y)/2; a[i+j+d]=(x-y)/2;
}
}
}
}
void Convolution(long long *a,long long *b,int n){
FWT(a,n); FWT(b,n);
for(int i=0; i<n; ++i) a[i]=a[i]*b[i];
UFWT(a,n);
} int a[20][100000];
long long A[MAXN],B[MAXN]; int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
scanf("%1d",&a[i][j]);
}
}
for(int j=0; j<m; ++j){
int s=0;
for(int i=0; i<n; ++i){
s<<=1;
s|=a[i][j];
}
++A[s];
}
for(int i=0; i<(1<<n); ++i){
int cnt=0;
for(int j=0; j<n; ++j){
if(i>>j&1) ++cnt;
}
B[i]=min(cnt,n-cnt);
}
Convolution(A,B,1<<n);
long long res=INF;
for(int i=0; i<(1<<n); ++i){
res=min(res,A[i]);
}
printf("%I64d",res);
return 0;
}

Codeforces663E Binary Table(FWT)的更多相关文章

  1. 【CF662C】Binary Table(FWT)

    [CF662C]Binary Table(FWT) 题面 洛谷 CF 翻译: 有一个\(n*m\)的表格(\(n<=20,m<=10^5\)), 每个表格里面有一个\(0/1\), 每次可 ...

  2. [CF662C] Binary Table(FWT)

    题意: https://www.cnblogs.com/cjyyb/p/9065801.html 题解:

  3. CF662C Binary Table (FWT板题)

    复习了一发FWT,发现还挺简单的... 没时间写了,就放一个博客吧:Great_Influence 的博客 注意这一句ans[i]=∑j⊗k=i​f[j]∗dp[k]ans[i]= ∑_{j⊗k=i} ...

  4. MySQL--当mysqldump --single-transaction遇到alter table(1)

    部分生产环境采用mysqldump --single-transaction的方式在夜间进行数据库备份,而同事恰好在备份期间执行了alter table操作,操作部分成功部分失败,为啥呢? ##=== ...

  5. 【CF850E】Random Elections(FWT)

    [CF850E]Random Elections(FWT) 题面 洛谷 CF 题解 看懂题就是一眼题了... 显然三个人是等价的,所以只需要考虑一个人赢了另外两个人就好了. 那么在赢另外两个人的过程中 ...

  6. 「WC2018」州区划分(FWT)

    「WC2018」州区划分(FWT) 我去弄了一个升级版的博客主题,比以前好看多了.感谢 @Wider 不过我有阅读模式的话不知为何 \(\text{LATEX}\) 不能用,所以我就把这个功能删掉了. ...

  7. 【HDU5909】Tree Cutting(FWT)

    [HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...

  8. CSS Table(表格)

    CSS Table(表格) 一.表格边框 border 指定CSS表格边框,使用border属性. 下面的例子指定了一个表格的Th和TD元素的黑色边框: table, th, td { border: ...

  9. 【UOJ#310】【UNR#2】黎明前的巧克力(FWT)

    [UOJ#310][UNR#2]黎明前的巧克力(FWT) 题面 UOJ 题解 把问题转化一下,变成有多少个异或和为\(0\)的集合,然后这个集合任意拆分就是答案,所以对于一个大小为\(s\)的集合,其 ...

随机推荐

  1. EBS 中经常用到的一些值集

    名称                                 值集                                                                ...

  2. js事件技巧方法整合

    window.resizeTo(800,600); //js设置浏览器窗口尺寸 window.open (function(){ resizeTo(640,480);//设置浏览器窗口尺寸 moveT ...

  3. 基于.net搭建热插拔式web框架(实现原理)

    第一节:我们为什么需要一个热插拔式的web框架? 模块之间独立开发 假设我们要做一个后台管理系统,其中包括“用户活跃度”.“产品管理”."账单管理"等模块.每个模块中有自己的业务特 ...

  4. js 数组,字符串,json互相转换

    数组转字符串 var arr = [1,2,3,4,'巴德','merge']; var str = arr.join(','); console.log(str); // 1,2,3,4,巴德,me ...

  5. 正确解读free -m

    如下显示free是显示的当前内存的使用,-m的意思是M字节来显示内容.我们来一起看看. $ free -m total used free shared buffers cachedMem: 1002 ...

  6. - > code vs 3038 3n+1问题(递归)

    3038 3n+1问题  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 白银 Silver 题解   题目描述 Description 3n+1问题是一个简单有趣而又没有解决的数 ...

  7. October 26th Week 44th Wednesday 2016

    No matter how far you may fly, never forget where you come from. 无论飞得多高,也不要忘记起飞的地方. I never forget w ...

  8. 模拟搭建Web项目的真实运行环境(四)

    本篇介绍如何部署mongodb环境,主要分为三个部分: 第一部分 介绍如何在ubuntu下安装mongodb, 第二部分 介绍如何在windows下安装使用MongoChef客户端, 第三部分 介绍在 ...

  9. 60阶单群同构于A5的证明

    设$G$是$60$阶的单群,我们来证明他同构于$A_5$,一个比较直观地思路是考虑群表示$\phi:G\to S(\Sigma)$,由同态基本定理得到$$G/{\rm Ker}\phi \simeq ...

  10. .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别

    WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...