题目

好吧,我连板子都不会了

有一个非常显然的做法就是\(O(2^nm)\)做法就是枚举每一行的状态,之后我们贪心去看看每一列是否需要翻转就好啦

显然这个做法非常垃圾过不去

首先我们发现每一列都不超过\(20\),考虑把每一列都压成一个状态

我们考虑设一些奇怪的东西

设\(g_i\)表示行的翻转状态为\(i\)的最优解,\(f_i\)表示有多少列的状态为\(i\),\(dp_i\)表示\(i\)这个状态最少有多少个\(1\)

显然\(dp_i=min\{bit(i),n-bit(i)\}\)

我们考虑有一列原来的状态是\(k\),行的翻转状态为\(i\),翻转之后这一列的状态是\(j\)

就会存在\(i\bigoplus k=j\),也就是\(i=j\bigoplus k\)

也就是说

\[g_i=\sum_{j\bigoplus k=i}f_k\times dp_j
\]

发现这是一个异或卷积,于是我们\(fwt\)一下就好了

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
const int maxn=(1<<20)+6;
LL cnt[maxn],dp[maxn],f[maxn];
int n,m,len;
char S[21][100005];
inline int Fwt(LL *t,int o) {
for(re int i=2;i<=len;i<<=1)
for(re int ln=i>>1,l=0;l<len;l+=i)
for(re int x=l;x<l+ln;++x) {
LL g=t[x],h=t[x+ln];
t[x]=g+h,t[ln+x]=g-h;
if(o) t[x]/=2ll,t[x+ln]/=2ll;
}
}
int main() {
scanf("%d%d",&n,&m);len=(1<<n);
for(re int i=0;i<len;i++) cnt[i]=cnt[i>>1]+(i&1);
for(re int i=0;i<len;i++) dp[i]=min(cnt[i],cnt[(len-1)^i]);
for(re int i=1;i<=n;i++) scanf("%s",S[i]+1);
for(re int i=1;i<=m;i++) {
int now=0;
for(re int j=1;j<=n;j++) {
if(S[j][i]=='1') now|=1;
now<<=1;
}
f[now>>1]++;
}
Fwt(f,0),Fwt(dp,0);
for(re int i=0;i<len;i++) f[i]*=dp[i];
Fwt(f,1);LL ans=f[0];
for(re int i=1;i<len;i++) ans=min(ans,f[i]);
std::cout<<ans;
return 0;
}

【CF662C】Binary Table的更多相关文章

  1. 【CF662C】Binary Table(FWT)

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

  2. 【CF662C】Binary Table 按位处理

    [CF662C]Binary Table 题意:给你一个$n\times m$的01网格,你可以进行任意次操作,每次操作是将一行或一列的数都取反,问你最多可以得到多少个1? $n\le 20,m\le ...

  3. 「CF662C」 Binary Table

    「CF662C」 Binary Table 题目链接 题目所给的 \(n\) 很小,于是我们可以考虑这样一种朴素做法:暴力枚举第 \(i\) 行是否翻转,这样每一行的状态就确定了,这时取每一列 \(0 ...

  4. 涉及到【分页】的table的请求模式

    step:1 点击分页器的内容 trigger事件句柄 (pagination, filters, sorter) => {//或者(page, pageSize)等 this.props.on ...

  5. 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)

    [Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...

  6. 【Leetcode】【Easy】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  7. 【翻译】Flink Table Api & SQL —— 数据类型

    本文翻译自官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/types.html Flink Table ...

  8. 【翻译】Flink Table Api & SQL — SQL

    本文翻译自官网:SQL https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/sql.html Flink Tab ...

  9. 【翻译】Flink Table Api & SQL — Hive Beta

    本文翻译自官网:Hive Beta https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/hive/ Flink ...

随机推荐

  1. SpringBoot整合mybatis-plus入门

    pom.xml中加入如下依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId>myba ...

  2. Centos7初次开机提示Initial setup of CentOS Linux 7 (core)

    安装完成centos7后出现如下提示: Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License informat ...

  3. shell与expect结合使用

    在linux操作系统下,使用脚本自动化,一般由两种方案,方案一:telnet+ftp,方案二:ssh+scp+expect. 以下主要使用ssh+scp+expect为例进行说明使用方式. 第一步:安 ...

  4. HDU 1142

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  5. Messenger与AIDL的异同

    Messenger与AIDL的异同 最近做项目需要使用进程间通信,大家知道应用层的进程间通信无非Broadcast,Activity,Service,Content Provider四大组件.Broa ...

  6. (利用DOM)在新打开的页面点击关闭当前浏览器窗口

    1.在开发过程中我们前端的用户体验中有时候会要求点击一个按钮,关闭当前浏览器窗口.用html DOM就可做到. 2.注意:本次写法要求在新窗口中关闭. target="_blank" ...

  7. CSS-带尖角的对话框

    效果图: box1的代码: .box{ position: relative; width: 200px; height: 200px; border: 2px solid #000; backgro ...

  8. Cannot perform conversion to XML from legacy HTML:

    错误信息:Cannot perform conversion to XML from legacy HTML: The nekoHTML library is not in classpath. ne ...

  9. js二分查找树实现

    function BinaryTree() { var Node = function(key) { this.key = key; this.left = null; this.right = nu ...

  10. ID3决策树算法实现(Python版)

    # -*- coding:utf-8 -*- from numpy import * import numpy as np import pandas as pd from math import l ...