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

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;
}
相关推荐
小雅痞8 分钟前
[Java][Leetcode simple] 1. 两数之和
java·算法·leetcode
somi711 分钟前
ARM-驱动-09-LCD FrameBuffer
arm开发·驱动开发·算法·自用
乐迪信息13 分钟前
乐迪信息:智慧港口AI防爆摄像机实现船舶违规靠岸自动抓拍
大数据·人工智能·算法·安全·目标跟踪
winxp-pic16 分钟前
图片校正软件 操作说明及算法介绍
算法
wayz1122 分钟前
Day 6 编程实战:决策树与过拟合分析
算法·决策树·机器学习
ฅ ฅBonnie30 分钟前
vLLM 推理后端简介
人工智能·python·算法
历程里程碑31 分钟前
Linux 50 IP协议深度解析:从报头结构到子网划分与NAT
java·linux·开发语言·网络·c++·python·智能路由器
贾斯汀玛尔斯31 分钟前
每天学一个算法--堆排序(Heap Sort)
数据结构·算法
programhelp_32 分钟前
ZipRecruiter CodeSignal OA 2026|最新真题分享 + 速通攻略
数据结构·经验分享·算法·面试
犹怜草木青1 小时前
const关键字
c++