题目

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. discuz首页设置默认地址不带forum.php后缀的方法

    最近在研究discuz,上传安装几部搞定,打开首页跳到含有"/forum.php"的网址,到管理中心改了好一会儿也没好.那么如何实现discuz首页设置不带forum.php后缀呢 ...

  2. SQL Server 积累

    2016-11-24 sql语句修改某表某字段的数据类型和字段长度: 问题是在修改老功能中暴露出来的,我修改了图片上传功能,结果报图片路径超出数据库字段规定长度,我检查数据库后发现之前设计数据库的人将 ...

  3. getComputedStyle/currentStyle/style之间的爱恨情仇

    getComputedStyle是? getComputedStyle是一个可以获取当前元素所有最终使用的CSS属性值.返回的是一个CSS样式声明对象([object CSSStyleDeclarat ...

  4. (原创)微信支付SDK调用的核心代码与分析(基于Android)

    先上代码,后面会分析 String url = "http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=android"; ...

  5. Gcc的Makefile简单使用

    Gcc的Makefile简单使用http://blog.chinaunix.net/uid-9330295-id-2425867.html

  6. mysql中的行转列

    //查看当前商品库存 function checkProductStock($product_id){ global $wpdb; $sql="SELECT post_id,max(if(( ...

  7. oracle 学习笔记(一)

    1. 数据库原理 1.1. 数据库简介 1.1.1. 文件存储 对数据的存储需求一直存在.保存数据的方式,经历了手工管理.文件管理等阶段,直至数据库管理阶段. 文件存储方式保存数据的弊端: 缺乏对数据 ...

  8. Spring框架学习(一)

    一. spring概述 Spring 框架是一个分层架构,由 7 个定义良好的模块组成.Spring 模块构建在核心容器之上,核心容器定义了创建.配置和管理 bean 的方式,如图 1 所示. 图 1 ...

  9. C# I/O

    获取运行时的动态目录 private static string GetDataDir_Data() { var parent = Directory.GetParent(Directory.GetC ...

  10. ios 和安卓常用图标、启动图 尺寸

    ---------------------------------------------ios---------------------------------------------------- ...