跳到正文

题解

题解 P2447 【[SDOI2010]外星千足虫】

hylhyl约 2 分钟
原文
文章信息与版本

文章信息

发布
2018年12月26日
收录
2026年8月8日
更新
2026年11月18日
篇幅
约 2 分钟
获赞
8
题目
P2447

版本

  1. v1当前

线性基求解异或方程组

用高斯消元求解时不难发现消元的过程实际是对两个方程的对应系数异或 那么是否可以用线性基求解呢?答案是肯定的。


插入操作

把每个01串插入p中,常数项建议用另一个数组w分开存(方便查询)其插入步骤与p同步


查询操作

查询每个消元后的二进制01串是否存在。如n=4时查询1000,0100,0010,0001即可。


判断是否有解

易得p必须插满才能保证每个01串都能通过异或得到。记录已插入的数量和最后插入的01编号,在此之后插入的都是无用的数据。

C++
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<bitset>
using namespace std;
bitset<1005> p[1005];
bitset<1005> a;
int w[1005],ans[1005],n,m;
int main()
{
	char ss[1005];
	scanf("%d %d",&n,&m);
	int q,v,cnt=0;
	for(int i=1;i<=m;i++)
	{
		scanf("%s",ss);scanf("%d",&v);
		for(int j=0;j<n;j++) a[j]=ss[j]-'0';
		for(int j=n-1;j>=0;j--)
		{
			if(!a[j]) continue;
			if(p[j]==0)
			{
				p[j]=a;
				w[j]=v;//记录常数
				q=i;//记录最后插入的编号
				cnt++;//记录插入的数量
				break;
			}
			a^=p[j];
			v^=w[j];//常数和p一起异或
		}
	}
	if(cnt!=n)
	{
		printf("Cannot Determine");
		return 0;
	}
	printf("%d\n",q);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++) a[j]=0;a[i]=1;
		for(int j=i;j>=0;j--)
		{
			if(a[j]==1)
			{
				a^=p[j];
				ans[i]^=w[j];//将w异或进ans数组。
			}
		}
		if(ans[i]==0) printf("Earth\n");
		else printf("?y7M#\n");
	}
	return 0;
}
原文
存档评论2 条抓取于