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

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;
}
相关推荐
To_OC2 小时前
LC 35 搜索插入位置:二分查找谁都会,边界条件谁写谁懵
javascript·算法·leetcode
码哥DFS3 小时前
二叉树的直径
开发语言·javascript·算法
营养充电站6 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
人工智能·算法·docker·jupyter
啊啊啊啊啊!!!!6 小时前
【c++】stack和queue的接口使用以及底层实现
开发语言·c++
技术小黑6 小时前
RNN算法实战系列05 | 天气预测
人工智能·rnn·算法
(Charon)9 小时前
【C++】锁与原子操作(四):自旋锁的完整实现与性能优化
c语言·开发语言·c++
Ivanqhz9 小时前
php8 use-def链
算法·php
evans在进步9 小时前
LeetCode 189 轮转数组:三次反转原地解决,图解 Java 实现
java·算法·leetcode
歌_顿9 小时前
Drawing_To_Model
算法