整数
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;
const int N=105;
int a[N][N];
int x[N];
bool free_x[N];
inline int gcd(int a,int b){return b?gcd(b,a%b):a;}
inline int lcm(int a,int b){return a/gcd(a,b)*b;}
int gauss(int m,int n)
{
int k,max_r,col=0,ta,tb;
int LCM,temp,num,free_index;
for(int i=0;i<n;i++)x[i]=0,free_x[i]=true;
for(k=0;k<m&&col<n;k++,col++)
{
max_r=k;
for(int i=k+1;i<m;i++)if(abs(a[i][col])>abs(a[max_r][col]))max_r=i;
if(max_r!=k)for(int j=k;j<=n;j++)swap(a[k][j],a[max_r][j]);
if(a[k][col]==0){k--;continue;}
for(int i=k+1;i<m;i++)
{
if(a[i][col]!=0)
{
LCM=lcm(abs(a[i][col]),abs(a[k][col]));
ta=LCM/abs(a[i][col]);
tb=LCM/abs(a[k][col]);
if(a[i][col]*a[k][col]<0)tb=-tb;
for(int j=col;j<=n;j++)a[i][j]=a[i][j]*ta-a[k][j]*tb;
}
}
}
for(int i=k;i<m;i++)if(a[i][col]!=0)return -1;
if(k<n)
{
for(int i=k-1;i>=0;i--)
{
num=0;
for(int j=0;j<n;j++)
{
if(a[i][j]!=0&&free_x[j])num++,free_index=j;
}
if(num>1)continue;
temp=a[i][n];
for(int j=0;j<n;j++)
{
if(a[i][j]!=0&&j!=free_index)temp-=a[i][j]*x[j];
}
x[free_index]=temp/a[i][free_index];
free_x[free_index]=false;
}
return n-k;
}
for(int i=n-1;i>=0;i--)
{
temp=a[i][n];
for(int j=i+1;j<n;j++)
{
if(a[i][j]!=0)temp-=a[i][j]*x[j];
}
if(temp%a[i][i]!=0)return -2;
x[i]=temp/a[i][i];
}return 0;
}
浮点数
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 1010;
const double EPS=1e-7;
int m,n;
double a[N][N],x[N];
int Gauss(int m,int n){
int col=0, k=0;///col为列号,k为行号
for (;k<m&&col<n;++k,++col){
int r = k;
for (int i=k+1;i<m;++i)
if(fabs(a[i][col])>fabs(a[r][col]))r=i;
if (fabs(a[r][col])<EPS){k--;continue;}///列全为0
if (r!=k)for(int i=col;i<=n;++i)
swap(a[k][i],a[r][i]);
for (int i=k+1;i<m;++i)///消元
if(fabs(a[i][col])>EPS){
double t = a[i][col]/a[k][col];
for (int j=col;j<=n;j++)a[i][j]-=a[k][j]*t;
a[i][col] = 0;
}
}
for(int i=k ;i<m ;++i)///无解
if (fabs(a[i][n])>EPS) return -1;
if (k < n) return n - k; ///自由元个数
for (int i =n-1; i>=0; i--){///回带求解
double temp = a[i][n];
for (int j=i+1; j<n; ++j)
temp -= x[j] * a[i][j];
x[i] = (temp / a[i][i]);
}
return 0;
}
异或方程组
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;
const int N = 205;
int a[N][N];
int x[N];
int gauss(int m, int n)
{
int max_r, col=0, k;
for (int i = 0; i < n; i++)x[i] = 0;
for (k = 0; k < m&&col < n; k++, col++)
{
max_r = k;
for (int i = k + 1; i < m; i++)if (abs(a[i][col]) > abs(a[max_r][col]))max_r = i;
if (max_r != k)for (int j = k; j <= n; j++)swap(a[k][j], a[max_r][j]);
if (a[k][col] == 0) { k--; continue; }
for (int i = k + 1; i < m; i++)
{
if (a[i][col] != 0)
{
for (int j = col; j <= n; j++)a[i][j] ^= a[k][j];
}
}
}
for (int i = k;i<m; i++)
{
if (a[i][col] != 0)return -1;
}
if (k < n)return n - k;
for (int i = n - 1; i >= 0; i--)
{
x[i] = a[i][n];
for (int j = i + 1; j < n; j++)
{
x[i]^= (a[i][j] & x[j]);
}
}return 0;
}
线性基
void gauss(int n)
{
for(int i=0;i!=n;i++)
{
for(int j=63;j>=0;j--)
{
if(a[i]>>j&1)
{
if(b[j])a[i]^=b[j];
else
{
b[j]=a[i];
for(int k=j-1;k>=0;k--)if(b[k] and (b[j]>>k&1))b[j]^=b[k];
for(int k=j+1;k<=63;k++)if(b[k]>>j&1)b[k]^=b[j];break;
}
}
}
}
}
©著作权归作者所有:来自51CTO博客作者qinXpeng的原创作品,如需转载,请注明出处,否则将追究法律责任
gauss51cto数学