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

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;
}
相关推荐
2301_789015629 分钟前
C++:二叉搜索树
c语言·开发语言·数据结构·c++·算法·排序算法
leiming68 小时前
C++ vector容器
开发语言·c++·算法
Xの哲學9 小时前
Linux流量控制: 内核队列的深度剖析
linux·服务器·算法·架构·边缘计算
yaoh.wang9 小时前
力扣(LeetCode) 88: 合并两个有序数组 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·双指针
apocelipes10 小时前
从源码角度解析C++20新特性如何简化线程超时取消
c++·性能优化·golang·并发·c++20·linux编程
LYFlied10 小时前
【每日算法】 LeetCode 56. 合并区间
前端·算法·leetcode·面试·职场和发展
ozyzo10 小时前
求1~n的累加和
c++
艾醒10 小时前
大模型原理剖析——多头潜在注意力 (MLA) 详解
算法
艾醒10 小时前
大模型原理剖析——DeepSeek-V3深度解析:671B参数MoE大模型的技术突破与实践
算法