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 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() {
solve(); }
|