1. 程式人生 > >BZOJ 3517 翻硬幣

BZOJ 3517 翻硬幣

namespace sha sharp clu [1] body scanf ring int

題解:

亂搞

令x[i][j]表示(i,j)是否操作,a[i][j]表示狀態

先假設都翻到0

則x[i][1]^x[i][2]^^^x[i][n]^x[1][j]^x[2][j]^^^x[n][j]^x[i][j]=a[i][j]

令d[i][j]=x[i][1]^x[i][2]^^^x[i][n]^x[1][j]^x[2][j]^^^x[n][j]^x[i][j]

則d[i][1]^d[i][2]^^^d[i][n]^d[1][j]^d[2][j]^^^d[n][j]^d[i][j]=x[i][j]

然後可以求出x[i][j]

根據式子發現翻轉成0和1的步數和=n^2

然後取min

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1009;

int n;
int a[maxn][maxn];
int r[maxn],l[maxn];

char ss[maxn];
int cnt=0;

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i){
		scanf("%s",ss+1);
		for(int j=1;j<=n;++j)a[i][j]=ss[j]-‘0‘;
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j)r[i]^=a[i][j];
	}
	for(int j=1;j<=n;++j){
		for(int i=1;i<=n;++i)l[j]^=a[i][j];
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j){
			int tm=r[i]^l[j]^a[i][j];
//			if(!tm)cout<<i<<‘ ‘<<j<<endl;
			cnt+=tm;
		}
	}
	
	cout<<min(cnt,n*n-cnt)<<endl;
	return 0;
}

  

BZOJ 3517 翻硬幣