[FWT] Codeforces 663E. Binary Table

Description

给定一个 n×m 的网格,每个位置是 0 1。每次可以选择一行或者一列使其中的数全部取反。
要求最小化 1 的个数。

Solution

ZJOI Day1 wjz大佬讲过的题啊。
打vp碰到了。
那时候一直不懂FWT,不明白对称差卷积啊。。
可以看出是要状压的。
首先要枚举翻转哪些行。就相当于枚举一个集合 S ,使所有数异或上S
那么第 i 列的贡献就是

min(nBitCount(aiS),BitCount(aiS))

bi=min(nBitCount(i),BitCount(i))
ci i a出现的次数。就有
ansS=TUbTcTS
那就是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;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值