【Codeforces 662C】Binary Table(FWT)

Description

有一个 n m 列的表格,每个元素都是 0 1 ,每次操作可以选择一行或一列,把 0 1 翻转,即把 0 换为 1 ,把 1 换为 0 。请问经过若干次操作后,表格中最少有多少个 1
n 20 , m 10 5


Solution

考虑到 n 很小,如果我们确定了哪些行被翻转了,那么可以单独考虑每一列需不需要翻转。
f ( i ) 表示状态为 i 的列有多少个;
g ( i ) = m i n { c o u n t ( i ) , c o u n t ( r e v ( i ) ) } c o u n t ( i ) 表示 i 二进制下 1 的个数, r e v ( i ) 表示将 i 的二进制位全部翻转;
h ( i ) 表示行的翻转情况为 i 时的最少的 1 的个数。
那么有:

h ( i ) = j i = k f ( j ) g ( k ) = j k = i f ( j ) g ( k )

然后FWT即可。


Code

/************************************************
 * Au: Hany01
 * Date: Jul 6th, 2018
 * Prob: CF662C
 * Email: [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1 << 20, maxm = 100005;

int n, m, all;
LL f[maxn], g[maxn], Ans;

inline void FWT(LL *a, int N, int ty)
{
    for (register int i = 2, p = 1; i <= N; p = i, i <<= 1)
        for (register int j = 0; j < N; j += i)
            rep(k, p) {
                register LL x = a[j + k], y = a[j + k + p];
                a[j + k] = x + y, a[j + k + p] = x - y;
                if (ty < 0) a[j + k] >>= 1, a[j + k + p] >>= 1;
            }
}

int main()
{
#ifdef hany01
    File("cf662c");
#endif

    static char s[maxm];
    static int  a[maxm];

    n = read(), m = read();
    For(i, 1, n) {
        scanf("%s", s + 1);
        For(j, 1, m) a[j] |= ((s[j] ^ 48) << (i - 1));
    }
    For(i, 1, m) ++ f[a[i]];
    all = 1 << n;
    rep(i, all) g[i] = min(__builtin_popcount(i), __builtin_popcount(i ^ (all - 1)));

    FWT(f, all, 1), FWT(g, all, 1);
    rep(i, all) f[i] *= g[i];
    FWT(f, all, -1), Ans = INF;
    rep(i, all) chkmin(Ans, f[i]);
    cout << Ans << endl;

    return 0;
}
//小楼一夜听春雨,深巷明朝卖杏花。
//    -- 陆游《临安春雨初霁》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/80945588