CF662C Binary Table

考虑把每一列当成一个不超过 $2^{n}$ 之内的数,于是进行完行的修改后相当于是每个数 xor 上了一个值 $x$ ,于是我们写成式子,这种情况答案就是

我们可以设

于是枚举 xor 出来的值

稍微移下项

我们设 $t[x]$ 为 $a[i]=x$ 的 $i$ 的个数,那么上面东西就是

这就是个 xor 卷积,然后取 min 就好了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#include "queue"
using namespace std;
#define MAXN 200006
//#define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin() , (x).end()
#define mem( a ) memset( a , 0 , sizeof a )
typedef long long ll;
int n , m;
ll A[MAXN] , a[( 1 << 20 ) + 6] , f[( 1 << 20 ) + 6];
char ch[MAXN];

void FWT( ll A[] , int len ) {
ll a1 , a2;
for( int mid = 2 ; mid <= len ; mid <<= 1 )
for( int i = 0 ; i < len ; i += mid )
for( int j = 0 ; j < ( mid >> 1 ) ; ++ j ) {
a1 = A[i + j] , a2 = A[i + ( mid >> 1 ) + j];
A[i + j] = ( a1 + a2 ) , A[i + ( mid >> 1 ) + j] = ( a1 - a2 );
}
}

void IFWT( ll A[] , int len ) {
ll a1 , a2;
for( int mid = 2 ; mid <= len ; mid <<= 1 )
for( int i = 0 ; i < len ; i += mid )
for( int j = 0 ; j < ( mid >> 1 ) ; ++ j ) {
a1 = A[i + j] , a2 = A[i + ( mid >> 1 ) + j];
A[i + j] = ( a1 + a2 ) / 2 , A[i + ( mid >> 1 ) + j] = ( a1 - a2 ) / 2;
}
}

void solve() {
cin >> n >> m;
rep( i , 1 , n ) {
scanf("%s",ch + 1);
rep( j , 1 , m ) A[j] |= ( ch[j] - '0' << ( n - i ) );
}
rep( j , 1 , m ) ++ a[A[j]];
int len = ( 1 << n );
rep( i , 0 , len - 1 ) f[i] = min( __builtin_popcount( i ) , n - __builtin_popcount( i ) );
FWT( a , len ) , FWT( f , len );
rep( i , 0 , len - 1 ) a[i] = a[i] * f[i];
IFWT( a , len );
ll mn = 0x3f3f3f3f3f3f3f3f;
rep( i , 0 , len - 1 ) mn = min( mn , a[i] );
cout << mn << endl;
}

signed main() {
// int T;cin >> T;while( T-- ) solve();
solve();
}


文章作者: yijan
文章链接: https://yijan.co/cf662c-binary-table/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Yijan's Blog