[poj1830]开关问题(高斯消元)
题意:求高斯消元中自由元的个数,输出1<<ans;
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 const int N=35; 9 int a[N][N],ans[N]; 10 int T,n,x,nn; 11 int gauss(int nn){ 12 int i,j,k,l; 13 for(i=1,j=1;i<=nn&&j<=nn;j++){ 14 for(k=i;k<=nn;k++)if(a[k][j])break; 15 if(a[k][j]){ 16 for(l=1;l<=nn+1;l++)swap(a[i][l],a[k][l]); 17 for(l=1;l<=nn;l++){ 18 if(l!=i&&a[l][j])for(k=1;k<=nn+1;k++)a[l][k]^=a[i][k]; 19 } 20 i++; 21 } 22 } 23 for(j=i;j<=nn;j++)if(a[j][n+1])return -1; 24 return 1<<(n-i+1); 25 } 26 27 int main(){ 28 scanf("%d",&T); 29 while(T--){ 30 memset(a,0,sizeof a); 31 scanf("%d",&n); 32 for(int i=1;i<=n;i++)scanf("%d",&a[i][n+1]); 33 for(int i=1;i<=n;i++)scanf("%d",&x),a[i][n+1]^=x; 34 int tx,ty; 35 while(scanf("%d%d",&tx,&ty)&&(tx||ty))a[ty][tx]=1;//注意 36 for(int i=1;i<=n;i++)a[i][i]=1; 37 int ans=gauss(n); 38 if(ans==-1)printf("Oh,it's impossible~!!\n"); 39 else printf("%d\n",ans); 40 } 41 return 0; 42 }