I - 开关问题
高斯消元求异或方程组。
每个灯连续操作两次等于没有操作,所以每盏灯只有0/1的操作状态,记为\(x_i\)
第\(i\)盏灯对第\(j\)盏灯有影响,则\(a_{j,i} = 1\),反之\(a_{j,i} = 0\)
第\(i\)盏灯初末状态不一样,\(a_{i,n+1} = 1\)反之为\(0\)
然后我们得到矩阵
\[\left[
\begin{array}{ccc}
a_{1,1} & a_{1,2} & \ldots & a_{1,n} & | & a_{1,n + 1} \\\\
a_{2,1} & a_{2,2} & \ldots & a_{2,n} & | & a_{2,n + 1} \\\\
\vdots & \vdots & \vdots & \vdots & | & \vdots \\\\
a_{n,1} & a_{n,2} & \ldots & a_{n,n} & | & a_{1,n + 1} \\\\
\end{array}
\right]
\]
直接高斯消元,用异或消去其他位上的\(1\)
如果出现第\(j\)列上所有的\(1\)都消去了,那么第\(j\)盏灯操不操作都可以,我们称为一个自由元。
统计自由元的个数\(k\),\(Ans = 2^k\)
如果第\(i\)行\(a_{i,1} ~ a_{i,n}\)均为\(0\),但是\(a_{i,n+1} = 1\)则无解。
以上
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n;
int a[35],b[35];
int g[35][35];
long long ksm(long long x,int y){
long long z = 1;
while(y){
if(y & 1) z = z * x;
y >>= 1;
x = x * x;
}
return z;
}
int main(){
int T; scanf("%d",&T);
while(T --){
memset(g,0,sizeof(g));
scanf("%d",&n);
for(int i = 1; i <= n; ++ i) scanf("%d",&a[i]);
for(int i = 1; i <= n; ++ i) scanf("%d",&b[i]);
for(int i = 1; i <= n; ++ i) if(a[i] == b[i]) g[i][n + 1] = 0; else g[i][n + 1] = 1;
int x,y;
while(1){
scanf("%d%d",&x,&y);
if(x == 0 && y == 0) break;
g[y][x] = 1;
}
for(int i = 1; i <= n; ++ i) g[i][i] = 1;
int now = 1;
for(int i = 1; i <= n; ++ i){
bool flag = 0; int pos = -1;
for(int j = now; j <= n; ++ j){
if(g[j][i] == 1) { flag = 1; pos = j; break; }
}
if(!flag) continue;
for(int j = 1; j <= n + 1; ++ j) swap(g[now][j],g[pos][j]);
for(int j = now + 1; j <= n; ++ j){
if(g[j][i] == 0) continue;
for(int k = 1; k <= n + 1; ++ k)
g[j][k] ^= g[now][k];
}
++ now;
}
bool flag = 1;
for(int i = now; i <= n; ++ i){
if(g[i][n + 1] != 0) { flag = 0; break; }
}
if(!flag) puts("Oh,it's impossible~!!");
else{
printf("%lld\n",ksm(2, n - now + 1));
}
}
return 0;
}