跳到正文

题解

题解:P10499 开关问题

Double_Light约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月8日
篇幅
约 3 分钟
题目
P10499

版本

  1. v1当前

考虑高斯消元。

对于每一个被 yy 影响到的 xx,考虑矩阵 AA,令 Ax,y=1A_{x,y}=1,否则 Ax,y=0A_{x,y}=0。特殊的,Ai,i=1A_{i,i}=1。

然后对 AA 跑高斯消元,只是把原来的加减消元变成异或消元。对于每一个消元后形如 0=00=0 的式子,则多一个自由变量,答案乘 22。否则若形如 0=x0=x 的式子(xx 不等于 00),则无解。注意答案初始化为 11。

代码如下:

C++
#include<iostream>
#define int long long
using namespace std;
int T;
int n;
int s[50],e[50];
int a[50][50];
int Ans[50];
int ans,f;
signed main(){
	cin>>T;
	while(T--){
		f=1;
		ans=1;
		cin>>n;
		for(int i=1;i<=n+1;i++){
			for(int j=1;j<=n+1;j++){
				a[i][j]=0;
			}
		}
		for(int i=1;i<=n;i++)cin>>s[i];
		for(int i=1;i<=n;i++)cin>>e[i];
		for(int i=1;i<=n;i++)a[i][n+1]=s[i]^e[i];
		while(1){
			int x,y;
			cin>>x>>y;
			if(x==0&&y==0)break;
			a[y][x]=1;
		}
		for(int i=1;i<=n;i++)a[i][i]=1;
		for(int i=1;i<=n;i++){
			int t=i;
			for(int j=i+1;j<=n;j++){
				if(a[j][i]>a[t][i])t=j;
			}
			swap(a[i],a[t]);
			if(a[i][i]==0)continue;
			for(int j=i+1;j<=n;j++){
				int x=a[j][i];
				for(int k=i;k<=n+1;k++){
					a[j][k]^=a[i][k]*x;
				}
			}
		}
		Ans[n]=a[n][n+1];
		if(a[n][n]==0){
			if(Ans[n]>0)f=0;
			else ans*=2;
		}
		for(int i=n-1;i;i--){
			for(int j=i+1;j<=n;j++){
				a[i][n+1]^=a[i][j]*Ans[j];
			}
			Ans[i]=a[i][n+1];
			if(a[i][i]==0){
				if(Ans[i]>0)f=0;
				else ans*=2;
			}
		}
		if(f)cout<<ans<<endl;
		else cout<<"Oh,it's impossible~!!"<<endl;
	}
	return 0;
}
原文