高斯消元解异或线性方程组

cpp 复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;

typedef pair<int, int> PII;
typedef long long ll;
typedef long double ld;

const int N = 110;

int n;
int a[N][N];

int gauss()
{
	int r, c;
	for(r = 0, c = 0; c < n; c ++)
	{
		int t = r;
		for(int i = r; i < n; i ++)
		{
			if(a[i][c])
			{
				t = i;
				break;
			}
		}
		
		if(!a[t][c])continue;
		
		for(int j = c; j <= n; j ++)swap(a[t][j], a[r][j]);
		
		for(int i = r + 1; i < n; i ++)
			if(a[i][c])
				for(int j = n; j >= c; j --)
					a[i][j] ^= a[r][j];
					
		r ++;
	}
	
	if(r < n)
	{
		for(int i = r; i < n; i ++)
			if(a[i][n])
				return 2;
		return 1;
	}
	
	for(int i = n - 1; i >= 0; i --)
		for(int j = i + 1; j < n; j ++)
			a[i][n] ^= a[i][j] & a[j][n];
	
	return 0;
}

int main()
{
	scanf("%d", &n);
	for(int i = 0; i < n; i ++)
		for(int j = 0; j <= n; j ++)
			scanf("%d", &a[i][j]);
			
	int t = gauss();
	
	if(t == 0)
	{
		for(int i = 0; i < n; i ++)
		{
			printf("%d\n", a[i][n]);
		}
	}
	else if(t == 1)printf("Multiple sets of solutions\n");
	else printf("No solution\n");
	
	return 0;
}
相关推荐
jyan_敬言42 分钟前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
liulilittle1 小时前
SNIProxy 轻量级匿名CDN代理架构与实现
开发语言·网络·c++·网关·架构·cdn·通信
张人玉1 小时前
C# 常量与变量
java·算法·c#
tan77º2 小时前
【Linux网络编程】Socket - UDP
linux·服务器·网络·c++·udp
weixin_446122462 小时前
LinkedList剖析
算法
GiraKoo2 小时前
【GiraKoo】C++14的新特性
c++
悠悠小茉莉2 小时前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
坏柠3 小时前
C++ Qt 基础教程:信号与槽机制详解及 QPushButton 实战
c++·qt
泽02023 小时前
C++之红黑树认识与实现
java·c++·rpc
百年孤独_3 小时前
LeetCode 算法题解:链表与二叉树相关问题 打打卡
算法·leetcode·链表