记
\(a_{i,j}\) 表示第 \(j\) 个开关对第 \(i\) 号开关产生的影响,\(x_i\) 为对第 \(i\) 个开关的操作,则
\[\begin{cases}
a_{1,1}x_1\ \mathrm{xor}\ a_{1,2}x_2\ \mathrm{xor}\ \cdots\ \mathrm{xor}\ a_{1,n}x_n=start_1 \ \mathrm{xor}\ end_1 \a_{2,1}x_1\ \mathrm{xor}\ a_{2,2}x_2\ \mathrm{xor}\ \cdots\ \mathrm{xor}\ a_{2,n}x_n=start_2 \ \mathrm{xor}\ end_2 \\cdots \a_{n,1}x_1\ \mathrm{xor}\ a_{n,2}x_2\ \mathrm{xor}\ \cdots\ \mathrm{xor}\ a_{n,n}x_n=start_n \ \mathrm{xor}\ end_n \\end{cases}\]
解就是了
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int T, a[35], sta, end, n, uu, vv;
int gauss(){
for(int i=1; i<=n; i++){
int maxi=i;
for(int j=i+1; j<=n; j++)
if(a[j]>a[maxi])
maxi = j;
swap(a[maxi], a[i]);
if(a[i]==0) return 1<<(n-i+1);
if(a[i]==1) return -1;
for(int j=n; j; j--)
if(a[i]&(1<<j)){
for(int k=1; k<=n; k++)
if(k!=i && a[k]&(1<<j))
a[k] ^= a[i];
break;
}
}
return 1;
}
int main(){
cin>>T;
while(T--){
memset(a, 0, sizeof(a));
sta = end = 0;
scanf("%d", &n);
for(int i=1; i<=n; i++){
scanf("%d", &uu);
a[i] ^= uu;
}
for(int i=1; i<=n; i++){
scanf("%d", &uu);
a[i] ^= uu;
a[i] |= 1<<i;
}
while(scanf("%d %d", &uu, &vv)!=EOF){
if(!uu && !vv) break;
a[vv] |= 1<<uu;
}
int re=gauss();
if(re<0) printf("Oh,it‘s impossible~!!\n");
else printf("%d\n", re);
}
return 0;
}