当前位置:网站首页>高斯消元求解异或线性方程组
高斯消元求解异或线性方程组
2022-07-26 09:29:00 【逃夭丶】
与求解普通线性方程组的步骤基本一致,如果矩阵的系数是1或者0,所以不用除以行最大值,直接异或即可。
唯一解的话就是,a[i][n+1]。
void gauss(){
for(int i=1;i<=n;i++){
int k=i;
for(int j=i+1;j<=n;j++){
if(fabs(a[j][i])>fabs(a[k][i]))
k=j;
}
if(a[k][i]==0) continue;
if(i!=k){
for(int j=i;j<=n+1;j++)
swap(a[i][j],a[k][j]);
}
for(k=1;k<=n;k++){
if(k!=i&&a[k][i]){
for(int j=i;j<=n+1;j++)
a[k][j] ^= a[i][j];
}
}
}
}
例题:高斯消元解异或线性方程组
输入一个包含n个方程n个未知数的异或线性方程组。
方程组中的系数和常数为0或1,每个未知数的取值也为0或1。
求解这个方程组。
异或线性方程组示例如下:
M[1][1]x[1] ^ M[1][2]x[2] ^ … ^ M[1][n]x[n] = B[1]
M[2][1]x[1] ^ M[2][2]x[2] ^ … ^ M[2][n]x[n] = B[2]
…
M[n][1]x[1] ^ M[n][2]x[2] ^ … ^ M[n][n]x[n] = B[n]
其中“^”表示异或(XOR),M[i][j]表示第i个式子中x[j]的系数,B[i]是第i个方程右端的常数,取值均为0或1。
输入格式
第一行包含整数n。
接下来n行,每行包含n+1个整数0或1,表示一个方程的n个系数以及等号右侧的常数。
输出格式
如果给定线性方程组存在唯一解,则输出共n行,其中第i行输出第i个未知数的解。
如果给定线性方程组存在多组解,则输出“Multiple sets of solutions”。
如果给定线性方程组无解,则输出“No solution”。
数据范围
1≤n≤100
输入样例:
3
1 1 0 1
0 1 1 0
1 0 0 1
输出样例:
1
0
0
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int maxn = 110;
const int inf = 0x3f3f3f3f3f;
const double eps = 1e-7;
int n;
int a[maxn][maxn];
int k1,k2;
void check(){
for(int i=1,j;i<=n;++i){
for(j=1;a[i][j]==0&&j<=n+1;++j);
if(j>n+1) k2 = 1;
if(j==n+1) k1 = 1;
}
}
void gauss(){
for(int i=1;i<=n;i++){
int k=i;
for(int j=i+1;j<=n;j++){
if(fabs(a[j][i])>fabs(a[k][i]))
k=j;
}
if(a[k][i]==0) continue;
if(i!=k){
for(int j=i;j<=n+1;j++)
swap(a[i][j],a[k][j]);
}
for(k=1;k<=n;k++){
if(k!=i&&a[k][i]){
for(int j=i;j<=n+1;j++)
a[k][j] ^= a[i][j];
}
}
}
check();
if(k1){
printf("No solution");return ;}
if(k2){
printf("Multiple sets of solutions");return ;}
for(int i=1;i<=n;i++){
printf("%d\n",a[i][n+1]);
}
}
int main(void)
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n+1;j++){
scanf("%d",&a[i][j]);
}
}
gauss();
return 0;
}
边栏推荐
猜你喜欢
Selection and practice of distributed tracking system
会议OA项目(三)---我的会议(会议排座、送审)
Personality test system V1.0
Basic use of ArcGIS 4
面试题目大赏
[shutter -- layout] detailed explanation of the use of align, center and padding
v-premission添加权限
Source code analysis of object wait notify notifyAll
【Flutter -- 布局】Align、Center、Padding 使用详解
配置ADCS后访问certsrv的问题
随机推荐
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
Add DLL
wap端微信h5支付,用于非微信浏览器
el-table实现增加/删除行,某参数跟着变
Implementation of fragment lazy loading after multi-layer nesting
微信小程序AvatarCropper 头像裁剪
解决“NOTE: One or more layouts are missing the layout_width or layout_height attributes.”
服务器、客户端双认证(2)
V-for dynamically sets the SRC of img
Basic use of Arc GIS 2
Process32first returns false, error x message 24
Windows通过命令备份数据库到本地
Bloom filter
After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
Windows下Redis哨兵模式搭建
asp.net 使用redis缓存(二)
青少年软件编程等级考试标准解读_二级
多层嵌套后的 Fragment 懒加载实现
2020-12-29
2022 chemical automation control instrument operation certificate test question simulation test platform operation