题目

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. 【转】ORACLE的REDO与UNDO

    一.什么是redo?redo:oracle在在线或者归档重做日志文件中的记录的信息,外以出现失败时可以利用这些数据来"重放"事务.每个oracle数据都至少有二个在线重做日志组,每 ...

  2. synchronized使用说明

    好久没有更新博客了,今天试着用简单的语言把synchronized的使用说清楚. synchronized是什么? synchronized是用来保证在多线程环境下代码同步执行的可重入的互斥锁.所谓互 ...

  3. spring mvc controller间跳转 重定向 传参

    http://blog.csdn.net/jackpk/article/details/19121777/

  4. MySQL 优化MySQL Server

    一.使用show variables 和show status 命令查看MySQL的服务器静态参数值和动态运行状态信息. 二.可以使用 mysqld --verbose --help|more 查看某 ...

  5. 转:aliyun阿里云Maven仓库地址——加速你的maven构建

    maven仓库用过的人都知道,国内有多么的悲催.还好有比较好用的镜像可以使用,尽快记录下来.速度提升100倍. http://maven.aliyun.com/nexus/#view-reposito ...

  6. crontab详解

    搜索 纠正错误  添加实例 crontab 提交和管理用户的需要周期性执行的任务 补充说明 crontab命令 被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成 ...

  7. NOIP2016滚粗计

    啦啦啦,第一次写游记~ Day0 早上浪浪浪,开了几盘CS 坐车到衢州,在车上开了几盘 艾萨克,然而好困啊…… 到衢二后围观XJ杭二合力A ztr,不是很懂为什么事情会变成这样 晚上开杀人游戏,wcz ...

  8. Python读写csv

    读取csv: http://blog.csdn.net/lixiang0522/article/details/7755059 读取Excel: http://www.cnblogs.com/lhj5 ...

  9. ACM/ICPC 之 靠墙走-DFS+BFS(POJ3083)

    //POJ3083 //DFS求靠左墙(右墙)走的路径长+BFS求最短路 //Time:0Ms Memory:716K #include<iostream> #include<cst ...

  10. 第3章 拍摄UFO——单一职责原则

    就一个类而言,应该仅有一个引起它变化的原因