Description
给定一个
n×m
的网格,每个位置是
0
或
要求最小化
1
的个数。
Solution
ZJOI Day1 wjz大佬讲过的题啊。
打vp碰到了。
那时候一直不懂FWT,不明白对称差卷积啊。。
可以看出是要状压的。
首先要枚举翻转哪些行。就相当于枚举一个集合
S
,使所有数异或上
那么第
i
列的贡献就是
设
bi=min(n−BitCount(i),BitCount(i))
设
ci
为
i
在
ansS=∑T⊆UbTcT⊕S
那就是FWT大裸题啦~
vp的时候电脑卡的要死啊。晚了 1s 交。。。。
是拷的模板,所以直接去膜了。。
#include <bits/stdc++.h>
using namespace std;
const int N = 21;
const int M = 3010101;
const int MOD = 1e9 + 7;
const int INV2 = (MOD + 1) >> 1;
typedef long long ll;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
template<typename T>
inline void read(T &x) {
static char c; x = 0; int sgn = 0;
for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
if (sgn) x = -x;
}
int a[M], b[M];
int c[M];
int n, m, x, lim, ans;
inline int BitCount(int x) {
int cnt = 0;
for (; x; x -= (x & -x)) cnt++;
return cnt;
}
void FWT(int* a, int f, int n) {
static int x, y;
for (int i = 1; i < n; i <<= 1)
for (int j = 0; j < n; j += (i << 1))
for (int k = 0; k < i; k++) {
x = a[j + k]; y = a[j + k + i];
a[j + k] = (x + y) % MOD;
a[j + k + i] = (x - y + MOD) % MOD;
if (f == -1) {
a[j + k] = (ll)a[j + k] * INV2 % MOD;
a[j + k + i] = (ll)a[j + k + i] * INV2 % MOD;
}
}
}
int main(void) {
read(n); read(m); lim = 1 << n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
x = get() - '0';
a[j] = a[j] << 1 | x;
}
get();
}
for (int i = 1; i <= m; i++) c[a[i]]++;
for (int i = 0; i < lim; i++) {
b[i] = min(BitCount(i), n - BitCount(i));
}
FWT(b, 1, lim); FWT(c, 1, lim);
for (int i = 0; i < lim; i++)
c[i] = (ll)c[i] * b[i] % MOD;
FWT(c, -1, lim); ans = MOD;
for (int i = 0; i < lim; i++)
ans = min(ans, c[i]);
cout << ans << endl;
return 0;
}