思路:乍一看好像和线性代数没什么关系。我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态为:u[1]*mp[1][i]^u[2]*mp[2][i]....^u[n]*mp[n][i](或者改为相加 % 2)。显然,前式等于B[i],所以,问题转化为了求u的解个数:MP*U = B。注意MP矩阵的写法。
关于矩阵:
r(A) = r(A,b) 有解
r(A) = r(A,b) = n 有唯一解 (n是未知量的个数,即A的列数)
r(A) = r(A,b) < n 有无穷多解
代码:
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
int A[maxn][maxn], B[maxn], n;
void Gauss(){
ll R = ;
int row = ;
for(int i = ; i <= n && row <= n; i++,row++){
int max_r = row;
for(int j = row + ; j <= n; j++){
if(A[j][i] > A[row][i]){
max_r = j;break;
}
}
if(max_r != row){
for(int k = i; k <= n + ; k++)
swap(A[max_r][k], A[row][k]);
}
if(A[row][i] == ){
row--;
continue;
}
R++;
for(int j = row + ; j <= n; j++){
if(A[j][i]){
for(int k = i; k <= n + ; k++)
A[j][k] = (A[j][k] - A[row][k] + ) % ;
}
}
}
for(int i = row; i <= n; i++){
if(A[i][n + ]){
printf("Oh,it's impossible~!!\n");
return;
}
}
R = n - R;
R = << R;
printf("%lld\n", R);
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
memset(A, , sizeof(A));
for(int i = ; i <= n; i++)
scanf("%d", &B[i]);
for(int i = ; i <= n; i++){
int v;
scanf("%d", &v);
A[i][n + ] = B[i] ^ v;
A[i][i] = ;
}
int u, v;
while(scanf("%d%d", &u , &v) && u + v){
A[v][u] = ;
}
Gauss();
}
return ;
}