CROC 2016 - Final Round [Private, For Onsite Finalists Only] C. Binary Table FWT

时间:2022-12-31 09:45:24

C. Binary Table

题目连接:

http://codeforces.com/problemset/problem/662/C

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

Hint

题意

给你一个nm的01矩阵,然后每次操作:你可以挑选任意的某一行或者某一列翻转,然后你需要使得整个矩阵的1的数量尽可能少,问你最少数量是多少。

题解:

首先2^nm这个算法很简单:暴力枚举横着怎么翻转,然后每一列O(1)判断就好了。

然后正解怎么做呢?

我们令ans[i]是异或i之后的1的个数是多少,那么ans[i] = sigma(cnt[i]*num[i^j),cnt[i]表示列那个二进制为i的个数,num[i]表示二进制为i这个数的1的数量是多少。

这个很显然发现 i(ij) = i,这就是一个异或卷积的形式,用FWT加速计算就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = (1<<20)+6;
int n,m,cnt[maxn];
long long x1[maxn],x2[maxn],ans[maxn];
string s[maxn];
long long t[maxn];
void utfxor(long long a[], int n) {
if(n == 1) return;
int x = n >> 1;
for(int i = 0; i < x; ++ i) {
t[i] = (a[i] + a[i + x]) >> 1;
t[i + x] = (a[i + x] - a[i]) >> 1;
}
memcpy(a, t, n * sizeof(long long));
utfxor(a, x); utfxor(a + x, x);
} long long tmp[maxn]; void tfxor(long long a[], int n) {
if(n == 1) return;
int x = n >> 1;
tfxor(a, x); tfxor(a + x, x);
for(int i = 0; i < x; ++ i) {
tmp[i] = a[i] - a[i + x];
tmp[i + x] = a[i] + a[i + x];
}
memcpy(a, tmp, n * sizeof(long long));
} void solve(long long a[],long long b[],int n)
{
tfxor(a,n);
tfxor(b,n);
for(int i=0;i<n;i++) a[i]=1LL*a[i]*b[i];
utfxor(a,n);
} int main()
{
for(int i=0;i<maxn;i++){
int tmp = i;
while(tmp){
if(tmp&1)cnt[i]++;
tmp>>=1;
}
}
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
cin>>s[i];
for(int i=0;i<m;i++){
int tmp = 0;
for(int j=0;j<n;j++){
if(s[j][i]=='1')tmp+=1<<j;
}
x1[tmp]++;
}
for(int i=0;i<(1<<n);i++)
x2[i]=min(cnt[i],n-cnt[i]);
solve(x1,x2,1<<n);
long long ans = 1e15;
for(int i=0;i<(1<<n);i++)
ans=min(ans,x1[i]);
cout<<ans<<endl;
}

CROC 2016 - Final Round [Private, For Onsite Finalists Only] C. Binary Table FWT的更多相关文章

  1. CROC 2016 - Elimination Round &lpar;Rated Unofficial Edition&rpar; D&period; Robot Rapping Results Report 二分&plus;拓扑排序

    D. Robot Rapping Results Report 题目连接: http://www.codeforces.com/contest/655/problem/D Description Wh ...

  2. 8VC Venture Cup 2016 - Final Round &lpar;Div&period; 2 Edition&rpar;

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

  3. CROC 2016 - Elimination Round &lpar;Rated Unofficial Edition&rpar; D&period; Robot Rapping Results Report 拓扑排序&plus;二分

    题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值 ...

  4. CF &num;CROC 2016 - Elimination Round D&period; Robot Rapping Results Report 二分&plus;拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

  5. 8VC Venture Cup 2016 - Final Round &lpar;Div&period; 1 Edition&rpar; E - Preorder Test 树形dp

    E - Preorder Test 思路:想到二分答案了之后就不难啦, 对于每个答案用树形dp取check, 如果二分的值是val, dp[ i ]表示 i 这棵子树答案不低于val的可以访问的 最多 ...

  6. CROC 2016 - Elimination Round &lpar;Rated Unofficial Edition&rpar; F - Cowslip Collections 数论 &plus; 容斥

    F - Cowslip Collections http://codeforces.com/blog/entry/43868 这个题解讲的很好... #include<bits/stdc++.h ...

  7. CROC 2016 - Elimination Round &lpar;Rated Unofficial Edition&rpar; E - Intellectual Inquiry dp

    E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...

  8. CROC 2016 - Elimination Round &lpar;Rated Unofficial Edition&rpar; E&period; Intellectual Inquiry 贪心 构造 dp

    E. Intellectual Inquiry 题目连接: http://www.codeforces.com/contest/655/problem/E Description After gett ...

  9. CROC 2016 - Elimination Round &lpar;Rated Unofficial Edition&rpar; C&period; Enduring Exodus 二分

    C. Enduring Exodus 题目连接: http://www.codeforces.com/contest/655/problem/C Description In an attempt t ...

随机推荐

  1. Android Studio 简介及导入 jar 包和第三方开源库方&lbrack;转&rsqb;

    原文:http://blog.sina.com.cn/s/blog_693301190102v6au.html Android Studio 简介 几天前的晚上突然又想使用 Android Studi ...

  2. Http中Cookie和Session介绍

    先介绍下B/S系统的工作的完整过程.首先客户端的浏览器发出请求,服务端的webserver接受到请求后,调用相关请求的页面进行处理,处理完后将结果发送给客户端的浏览器进行显示.只能是浏览器向webse ...

  3. 基于ASP&period;Net &plus;easyUI框架上传图片,实现图片上传,提交表单

    <body> <link href="../../Easyui/themes/easyui.css" rel="stylesheet" typ ...

  4. Heroku使用

    先要生成一个公钥,使用命令:$ ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save ...

  5. &lbrack;WPF疑难&rsqb;Hide me&excl; not close

    原文 [WPF疑难]Hide me! not close [WPF疑难]Hide me! not close                               周银辉 有朋友遇到这样的一个问 ...

  6. 深度分析如何在Hadoop中控制Map的数量

    深度分析如何在Hadoop中控制Map的数量 guibin.beijing@gmail.com 很多文档中描述,Mapper的数量在默认情况下不可直接控制干预,因为Mapper的数量由输入的大小和个数 ...

  7. win10安装tensorflow-gpu1&period;13&period;1&plus;cuda10&period;0&plus;cudnn7&period;3&period;1

    一,本机配置 Win10 64bit NVIDIA GeForce GTX 960M Python3.7(Anaconda) 二,安装CUDA 亲测,TensorFlow-gpu1.13.1支持cud ...

  8. 自定义数据类型 typedef

    其实就是为数据类型起一个别名. typedef unsigned char AGE; //字符类型AGE x; //等价于 unsigned char x; typedef int * IPointe ...

  9. &lbrack;No0000164&rsqb;C&num;,科学计数法的哽

    NewString = Decimal.Parse(OldString, System.Globalization.NumberStyles.Float).ToString(); //Convert. ...

  10. java设计模式-----23、命令模式

    概念: Command模式也叫命令模式 ,是行为设计模式的一种.Command模式通过被称为Command的类封装了对目标对象的调用行为以及调用参数. 命令模式(Command Pattern)是一种 ...