这几个题目都是翻转之后关联其他点的翻转,并且都是采用的01翻转,实属良心。
另外建立增广矩阵真的很费劲
poj 1222
- #include<iostream>
- #include<cstring>
- #include <algorithm>
- #include<cstdlib>
- #include<vector>
- #include<cmath>
- #include<stdlib.h>
- #include<iomanip>
- #include<list>
- #include<deque>
- #include<map>
- #include <stdio.h>
- #include <queue>
- const int maxn=1000+5;
- #define inf 0x3f3f3f3f
- #define INF 0x3FFFFFFFFFFFFFFFLL
- #define rep(i,n) for(i=0;i<n;i++)
- #define reP(i,n) for(i=1;i<=n;i++)
- #define ull unsigned long long
- #define ll long long
- #define cle(a) memset(a,0,sizeof(a))
- using namespace std;
- //对2取模的01方程组
- const int MAXN = 40;
- //有equ个方程,var个变元。增广矩阵行数为equ,列数为var+1,分别为0到var
- int equ,var;//方程数和变量数,注意对其赋值
- int a[MAXN][MAXN]; //增广矩阵
- //增广矩阵:在系数矩阵的右边添上一列,这一列是线性方程组的等号右边的值。
- int x[MAXN]; //解集
- int free_x[MAXN];//用来存储自由变元(多解枚举自由变元可以使用)
- int free_num;//自由变元的个数
- void init(){//初始化
- cle(a),cle(x),cle(free_x);
- }
- //返回值为-1表示无解,为0是唯一解,否则返回自由变元个数
- void out(){
- int i,j;
- rep(i,20){
- rep(j,21){
- cout<<a[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<"_____bug______"<<endl;
- }
- int ans[MAXN];
- int now;
- int dfs(int v)
- {
- int i,j;
- int cop_a[MAXN][MAXN];
- if(v==0){
- int tt=0;
- for(i=0;i<20;i++){
- x[i]=ans[i];
- }
- memcpy(cop_a,a,sizeof(a));
- for(i=now-1;i>=0;i--){
- for(j=i;j<var;j++){
- cop_a[i][20]^=(x[j]&cop_a[i][j]);
- }
- x[i]^=cop_a[i][20];
- }
- rep(i,20)tt+=x[i];
- return tt;
- }
- else{
- ans[free_x[v-1]]=0;
- int tt=dfs(v-1);
- ans[free_x[v-1]]=1;
- tt=min(tt,dfs(v-1));
- return tt;
- }
- }
- int Gauss() {
- int max_r,col,k;
- free_num = 0;
- for(k = 0, col = 0 ; k <equ&& col <var ; k++, col++) {
- max_r = k;
- for(int i = k+1; i<equ; i++) {
- if(abs(a[i][col]) > abs(a[max_r][col]))
- max_r = i;
- }
- if(a[max_r][col] == 0) {
- k--;
- free_x[free_num++] = col;//这个是自由变元
- continue;
- }
- if(max_r != k) {
- for(int j = col; j < var+1; j++)
- swap(a[k][j],a[max_r][j]);
- }
- for(int i = k+1; i<equ; i++) {
- if(a[i][col] != 0) {
- for(int j = col; j < var+1; j++)
- a[i][j] ^= a[k][j];
- }
- }
- }
- now=k;
- // cout<<k<<":"<<var<<endl;
- for(int i = k; i<equ; i++)
- if(a[i][col] != 0)
- return -1;//无解
- if(k<var)
- //自由变元个数
- {
- int tt;
- tt=dfs(var-k);
- now=k;
- cout<<tt<<endl;
- return -1;
- }
- //唯一解,回代
- else{
- for(int i = var-1; i>= 0; i--) {
- x[i] = a[i][var];
- for(int j = i+1; j <var; j++)
- x[i] ^= (a[i][j] && x[j]);
- }
- }
- return 0;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- #endif
- int n;init();
- while(cin>>a[0][20]){
- int i;
- for(i=1;i<=19;i++){
- scanf("%d",&a[i][20]);
- }
- equ=var=20;
- rep(i,20){
- a[i][i]=1;
- if(i-1>=0)a[i][i-1]=1;
- if(i+1<20)a[i][i+1]=1;
- }
- int ans=Gauss();
- if(ans==0){
- rep(i,20){
- ans+=x[i];
- }
- cout<<ans<<endl;
- }init();
- }
- return 0;
- }
poj 1681
- #include<iostream>
- #include<cstring>
- #include <algorithm>
- #include<cstdlib>
- #include<vector>
- #include<cmath>
- #include<stdlib.h>
- #include<iomanip>
- #include<list>
- #include<deque>
- #include<map>
- #include <stdio.h>
- #include <queue>
- const int maxn=1000+5;
- #define inf 0x3f3f3f3f
- #define INF 0x3FFFFFFFFFFFFFFFLL
- #define rep(i,n) for(i=0;i<n;i++)
- #define reP(i,n) for(i=1;i<=n;i++)
- #define ull unsigned long long
- #define ll long long
- #define cle(a) memset(a,0,sizeof(a))
- using namespace std;
- const int MAXN = 250;
- //有equ个方程,var个变元。增广矩阵行数为equ,列数为var+1,分别为0到var
- int equ,var;//注意对其赋值
- int a[MAXN][MAXN]; //增广矩阵
- //增广矩阵:在系数矩阵的右边添上一列,这一列是线性方程组的等号右边的值。
- int x[MAXN]; //解集
- int free_x[MAXN];//用来存储自由变元(多解枚举自由变元可以使用)
- int free_num;//自由变元的个数
- void init(){//初始化
- cle(a),cle(x),cle(free_x);
- }
- int n;
- int dir[4][2]={1,0,-1,0,0,1,0,-1};
- int getbool(int x,int y){
- if(x<0||y<0||x>=n||y>=n)return 0;
- return 1;
- }
- void getcon(int x,int y){
- int newx,newy;
- int i;
- rep(i,4){
- newx=x+dir[i][0],newy=y+dir[i][1];
- if(getbool(newx,newy)){
- a[x*n+y][newx*n+newy]=1;
- }
- }
- }
- void out(){
- int i,j;
- rep(i,n*n){
- rep(j,n*n+1){
- cout<<a[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- //返回值为-1表示无解,为0是唯一解,否则返回自由变元个数
- int Gauss() {
- int max_r,col,k;
- free_num = 0;
- for(k = 0, col = 0 ; k <equ&& col <var ; k++, col++) {
- max_r = k;
- for(int i = k+1; i<equ; i++) {
- if(abs(a[i][col]) > abs(a[max_r][col]))
- max_r = i;
- }
- if(a[max_r][col] == 0) {
- k--;
- free_x[free_num++] = col;//这个是自由变元
- continue;
- }
- if(max_r != k) {
- for(int j = col; j < var+1; j++)
- swap(a[k][j],a[max_r][j]);
- }
- for(int i = k+1; i<equ; i++) {
- if(a[i][col] != 0) {
- for(int j = col; j < var+1; j++)
- a[i][j] ^= a[k][j];
- }
- }
- }
- for(int i = k; i<equ; i++)
- if(a[i][col] != 0)
- return -1;//无解
- // if(k <var) return var-k;//自由变元个数
- //唯一解,回代
- for(int i = var-1; i>= 0; i--) {
- x[i] = a[i][var];
- for(int j = i+1; j <var; j++)
- x[i] ^= (a[i][j] && x[j]);
- }
- return 0;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- #endif
- int T;
- cin>>T;
- while(T--){
- cin>>n;
- init();
- equ=var=n*n;
- int i,j;
- for(i=0;i<n*n;i++){
- char c;
- cin>>c;
- if(c=='w'){
- a[i][n*n]=1;
- }
- }
- for(i=0;i<n;i++){
- for(j=0;j<n;j++){
- a[i*n+j][i*n+j]=1;
- getcon(i,j);
- }
- }
- //out();
- int ans=Gauss();
- if(ans==0){
- int sum=0;
- rep(i,n*n){
- sum+=x[i];
- }
- cout<<sum<<endl;
- }
- else if(ans==-1){
- cout<<"inf"<<endl;
- }
- }
- return 0;
- }
poj 1830
- #include<iostream>
- #include<cstring>
- #include <algorithm>
- #include<cstdlib>
- #include<vector>
- #include<cmath>
- #include<stdlib.h>
- #include<iomanip>
- #include<list>
- #include<deque>
- #include<map>
- #include <stdio.h>
- #include <queue>
- const int maxn=1000+5;
- #define inf 0x3f3f3f3f
- #define INF 0x3FFFFFFFFFFFFFFFLL
- #define rep(i,n) for(i=0;i<n;i++)
- #define reP(i,n) for(i=1;i<=n;i++)
- #define ull unsigned long long
- #define ll long long
- #define cle(a) memset(a,0,sizeof(a))
- using namespace std;
- const int MAXN = 910;
- //有equ个方程,var个变元。增广矩阵行数为equ,列数为var+1,分别为0到var
- int equ,var;//注意对其赋值
- int a[MAXN][MAXN]; //增广矩阵
- //增广矩阵:在系数矩阵的右边添上一列,这一列是线性方程组的等号右边的值。
- int x[MAXN]; //解集
- int free_x[MAXN];//用来存储自由变元(多解枚举自由变元可以使用)
- int free_num;//自由变元的个数
- void init(){//初始化
- cle(a),cle(x),cle(free_x);
- }
- int n;
- void out(){
- int i,j;
- rep(i,n){
- rep(j,n+1){
- cout<<a[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- //返回值为-1表示无解,为0是唯一解,否则返回自由变元个数
- int Gauss() {
- int max_r,col,k;
- free_num = 0;
- for(k = 0, col = 0 ; k <equ&& col <var ; k++, col++) {
- max_r = k;
- for(int i = k+1; i<equ; i++) {
- if(abs(a[i][col]) > abs(a[max_r][col]))
- max_r = i;
- }
- if(a[max_r][col] == 0) {
- k--;
- free_x[free_num++] = col;//这个是自由变元
- continue;
- }
- if(max_r != k) {
- for(int j = col; j < var+1; j++)
- swap(a[k][j],a[max_r][j]);
- }
- for(int i = k+1; i<equ; i++) {
- if(a[i][col] != 0) {
- for(int j = col; j < var+1; j++)
- a[i][j] ^= a[k][j];
- }
- }
- }
- for(int i = k; i<equ; i++)
- if(a[i][col] != 0)
- return -1;//无解
- if(k <var)
- return var-k;//自由变元个数
- //唯一解,回代
- for(int i = var-1; i>= 0; i--) {
- x[i] = a[i][var];
- for(int j = i+1; j <var; j++)
- x[i] ^= (a[i][j] && x[j]);
- }
- return 0;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- #endif
- int T;
- cin>>T;
- while(T--){
- cin>>n;
- equ=var=n;
- int i,j;
- int be[maxn];
- int en[maxn];
- rep(i,n){
- cin>>be[i];
- }
- init();
- rep(i,n){
- cin>>en[i];
- if(be[i]!=en[i]){
- a[i][n]=1;
- }
- }
- int x,y;
- while(cin>>x>>y){
- if(x==0&&y==0){
- break;
- }
- a[y-1][x-1]=1;
- }
- rep(i,n)a[i][i]=1;
- int ans=Gauss();
- // cout<<ans<<endl;
- if(ans==-1){
- cout<<"Oh,it's impossible~!!"<<endl;
- }
- else{
- cout<<(1<<ans)<<endl;
- }
- }
- return 0;
- }
poj 3185
- #include<iostream>
- #include<cstring>
- #include <algorithm>
- #include<cstdlib>
- #include<vector>
- #include<cmath>
- #include<stdlib.h>
- #include<iomanip>
- #include<list>
- #include<deque>
- #include<map>
- #include <stdio.h>
- #include <queue>
- const int maxn=1000+5;
- #define inf 0x3f3f3f3f
- #define INF 0x3FFFFFFFFFFFFFFFLL
- #define rep(i,n) for(i=0;i<n;i++)
- #define reP(i,n) for(i=1;i<=n;i++)
- #define ull unsigned long long
- #define ll long long
- #define cle(a) memset(a,0,sizeof(a))
- using namespace std;
- //对2取模的01方程组
- const int MAXN = 40;
- //有equ个方程,var个变元。增广矩阵行数为equ,列数为var+1,分别为0到var
- int equ,var;//方程数和变量数,注意对其赋值
- int a[MAXN][MAXN]; //增广矩阵
- //增广矩阵:在系数矩阵的右边添上一列,这一列是线性方程组的等号右边的值。
- int x[MAXN]; //解集
- int free_x[MAXN];//用来存储自由变元(多解枚举自由变元可以使用)
- int free_num;//自由变元的个数
- void init(){//初始化
- cle(a),cle(x),cle(free_x);
- }
- //返回值为-1表示无解,为0是唯一解,否则返回自由变元个数
- void out(){
- int i,j;
- rep(i,20){
- rep(j,21){
- cout<<a[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<"_____bug______"<<endl;
- }
- int ans[MAXN];
- int now;
- int dfs(int v)
- {
- int i,j;
- int cop_a[MAXN][MAXN];
- if(v==0){
- int tt=0;
- for(i=0;i<20;i++){
- x[i]=ans[i];
- }
- memcpy(cop_a,a,sizeof(a));
- for(i=now-1;i>=0;i--){
- for(j=i;j<var;j++){
- cop_a[i][20]^=(x[j]&cop_a[i][j]);
- }
- x[i]^=cop_a[i][20];
- }
- rep(i,20)tt+=x[i];
- return tt;
- }
- else{
- ans[free_x[v-1]]=0;
- int tt=dfs(v-1);
- ans[free_x[v-1]]=1;
- tt=min(tt,dfs(v-1));
- return tt;
- }
- }
- int Gauss() {
- int max_r,col,k;
- free_num = 0;
- for(k = 0, col = 0 ; k <equ&& col <var ; k++, col++) {
- max_r = k;
- for(int i = k+1; i<equ; i++) {
- if(abs(a[i][col]) > abs(a[max_r][col]))
- max_r = i;
- }
- if(a[max_r][col] == 0) {
- k--;
- free_x[free_num++] = col;//这个是自由变元
- continue;
- }
- if(max_r != k) {
- for(int j = col; j < var+1; j++)
- swap(a[k][j],a[max_r][j]);
- }
- for(int i = k+1; i<equ; i++) {
- if(a[i][col] != 0) {
- for(int j = col; j < var+1; j++)
- a[i][j] ^= a[k][j];
- }
- }
- }
- now=k;
- // cout<<k<<":"<<var<<endl;
- for(int i = k; i<equ; i++)
- if(a[i][col] != 0)
- return -1;//无解
- if(k<var)
- //自由变元个数
- {
- int tt;
- tt=dfs(var-k);
- now=k;
- cout<<tt<<endl;
- return -1;
- }
- //唯一解,回代
- else{
- for(int i = var-1; i>= 0; i--) {
- x[i] = a[i][var];
- for(int j = i+1; j <var; j++)
- x[i] ^= (a[i][j] && x[j]);
- }
- }
- return 0;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- #endif
- int n;init();
- while(cin>>a[0][20]){
- int i;
- for(i=1;i<=19;i++){
- scanf("%d",&a[i][20]);
- }
- equ=var=20;
- rep(i,20){
- a[i][i]=1;
- if(i-1>=0)a[i][i-1]=1;
- if(i+1<20)a[i][i+1]=1;
- }
- int ans=Gauss();
- if(ans==0){
- rep(i,20){
- ans+=x[i];
- }
- cout<<ans<<endl;
- }init();
- }
- return 0;
- }
sicily 1048 是找的题目给大二小朋友做搜索的,结果意外发现可以用高斯定理,然后就给顺带A了
- // Problem#: 1048
- // Submission#: 3258134
- // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
- // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
- // All Copyright reserved by Informatic Lab of Sun Yat-sen University
- /*************************************************************************
- > File Name: 3.cpp
- > Author: cy
- > Mail: 1002@qq.com
- > Created Time: 14/11/23 14:23:15
- ************************************************************************/
- #include<iostream>
- #include<cstring>
- #include <algorithm>
- #include<cstdlib>
- #include<vector>
- #include<cmath>
- #include<stdlib.h>
- #include<iomanip>
- #include<list>
- #include<deque>
- #include<map>
- #include <queue>
- const int maxn=1000+5;
- #define inf 0x3f3f3f3f
- #define INF 0x3FFFFFFFFFFFFFFFLL
- #define rep(i,n) for(i=0;i<n;i++)
- #define reP(i,n) for(i=1;i<=n;i++)
- #define ull unsigned long long
- #define ll long long
- #define cle(a) memset(a,0,sizeof(a))
- using namespace std;
- const int MAXN = 40;
- //有equ个方程,var个变元。增广矩阵行数为equ,列数为var+1,分别为0到var
- int equ,var;//方程数和变量数,注意对其赋值
- int a[MAXN][MAXN]; //增广矩阵
- //增广矩阵:在系数矩阵的右边添上一列,这一列是线性方程组的等号右边的值。
- int x[MAXN]; //解集
- int free_x[MAXN];//用来存储自由变元(多解枚举自由变元可以使用)
- int free_num;//自由变元的个数
- void init(){//初始化
- cle(a),cle(x),cle(free_x);
- equ=var=9;
- }
- //返回值为-1表示无解,为0是唯一解,否则返回自由变元个数
- void out(){
- int i,j;
- rep(i,9){
- rep(j,10){
- cout<<a[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<"_____bug______"<<endl;
- }
- int ans[MAXN];
- int now;
- int Gauss() {
- int max_r,col,k;
- free_num = 0;
- for(k = 0, col = 0 ; k <equ&& col <var ; k++, col++) {
- max_r = k;
- for(int i = k+1; i<equ; i++) {
- if(abs(a[i][col]) > abs(a[max_r][col]))
- max_r = i;
- }
- if(a[max_r][col] == 0) {
- k--;
- free_x[free_num++] = col;//这个是自由变元
- continue;
- }
- if(max_r != k) {
- for(int j = col; j < var+1; j++)
- swap(a[k][j],a[max_r][j]);
- }
- for(int i = k+1; i<equ; i++) {
- if(a[i][col] != 0) {
- for(int j = col; j < var+1; j++)
- a[i][j] ^= a[k][j];
- }
- }
- }
- now=k;
- // cout<<k<<":"<<var<<endl;
- for(int i = k; i<equ; i++)
- if(a[i][col] != 0)
- return -1;//无解
- if(k<var)
- //自由变元个数
- {
- int tt;
- tt=dfs(var-k);
- now=k;
- cout<<tt<<endl;
- return -1;
- }
- //唯一解,回代
- else{
- for(int i = var-1; i>= 0; i--) {
- x[i] = a[i][var];
- for(int j = i+1; j <var; j++)
- x[i] ^= (a[i][j] && x[j]);
- }
- }
- return 0;
- }
- int dir[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
- bool getbool(int x,int y){
- if(x<0||y<0||x>=3||y>=3)return false;
- return true;
- }
- int main()
- {
- int n;
- int T;
- cin>>T;
- while(T--){
- init();
- int i;
- int num=0;
- for(i=0;i<9;i++){
- char c;
- cin>>c;
- if(c=='b')num++,a[i][9]=1;
- }
- if(!num){
- cout<<11<<endl;
- continue;
- }
- int j,k;
- for(i=0;i<3;i++){
- rep(j,3){
- a[i*3+j][i*3+j]=1;
- rep(k,8){
- int newx=i+dir[k][0];
- int newy=j+dir[k][1];
- if(getbool(newx,newy)){
- a[i*3+j][newx*3+newy]=1;
- }
- }
- }
- }
- // out();
- int ans=Gauss();
- rep(i,9){
- if(x[i]){
- cout<<i+1;
- }
- }
- cout<<endl;
- }
- return 0;
- }