跳到正文

个人记录

AcWing 208. 开关问题

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

文章信息

发布
2021年3月29日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 3 分钟

版本

  1. v1当前

题目大意:

有 NN 个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关,如果为关就变为开。

你的目标是经过若干次开关操作后使得最后 NN 个开关达到一个特定的状态。

对于任意一个开关,最多只能进行一次开关操作。

你的任务是,计算有多少种可以达到指定状态的方法。(不计开关操作的顺序)。

Solution:Solution:

一道很好的题,用到的是高斯消元的基础知识。

高斯消元的扩展:异或也可以用高斯消元法来解决,其具体实现也是消成简化阶梯型矩阵。

code:code:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define reg register
#define x1 x11
#define y1 y11
#define x2 x22
#define y2 y22
#define z1 z11
#define z2 z22
const int maxn=4e5+5;
const int INF=2e9;
const int mod=998244353;
const double Exp=1e-4;
const double Pi=acos(-1.0);
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch=='-')
		f=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int T;
int a[maxn];
int n;
signed main()
{
	T=read();
	while(T--)
	{
		n=read();
		for(int i=1;i<=n;i++)
		{
			a[i]=read();
		}
		for(int i=1;i<=n;i++)
		{
			int x=read();
			a[i]=a[i] ^ x;
			a[i]=a[i] | (1<<i);
		}
		int x,y;
		while(cin>>x>>y)
		{
			if(x==0 && y==0)
			break;
			a[y]=a[y] | (1<<x);
		}
		int ans=1;
		for(int i=1;i<=n;i++)
		{
			for(int j=i+1;j<=n;j++)
			{
				if(a[j]>a[i])
				swap(a[i],a[j]);
			}
			if(a[i]==0)
			{
				ans=(1<<(n-i+1));
				break;
			}
			else
			if(a[i]==1)
			{
				ans=0;
				break;
			}
			for(int j=n;j>=1;j--)
			{
				if(a[i] & (1<<j))
				{
					for(int k=i+1;k<=n;k++)
					{
						if(a[k] & (1<<j))
						a[k]=a[k] ^ a[i];
					}
					break;
				}
			}
		}
		if(!ans)
		{
			printf("Oh,it's impossible~!!\n");
		}
		else
		printf("%lld\n",ans);
	}
	return 0;
}
原文