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

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;
}
相关推荐
HjhIron22 分钟前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩2 小时前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹3 小时前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术7 小时前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望9 小时前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰9 小时前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法
地平线开发者19 小时前
J6B vio scenario sample
算法
BothSavage1 天前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn1 天前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法