当前位置:网站首页>线代(高斯消元法、线性基)
线代(高斯消元法、线性基)
2022-06-30 10:42:00 【Dαīsч】
一、高斯消元法
1、将问题转化为矩阵方程,再转化为多个n元一次方程,从而使用高斯消元法
使用高斯消元法的关键在于构造增广矩阵
2、需求解的未知数可能是很多类型,例如浮点型、01型
(1)、异或类型解
bitset<maxn>a[maxn]; //a数组代表增广矩阵的系数,常数项在最后
int ans[maxn], Free[maxn], cnt; //ans代表最后方程组的解,Free和cnt是自由元
int Gauss(int equ, int var) //equ个方程组,var个位置数
{
int row, col, MaxRow;
col = 0;
for (row = 0; row < equ && col < var; row++, col++)
{
MaxRow = row;
for (int i = row + 1; i < equ; i++)
{
if (abs(a[i][col]) > abs(a[MaxRow][col]))
MaxRow = i;
}
if (MaxRow != row)
{
swap(a[row], a[MaxRow]);
}
if (a[row][col] == 0)
{
row--;
Free[++cnt] = col;
continue;
}
for (int i = row + 1; i < equ; i++)
{
if (a[i][col])
a[i] ^= a[row];
}
}
for (int i = row; i < equ; i++)
{
if (a[i][col])
return -1;
}
if (row < var)
return var - row;
for (int i = var - 1; i >= 0; i--)
{
ans[i] = a[i][var];
for (int j = i + 1; j < var; j++)
{
if (a[i][j])
ans[i] ^= (a[i][j] && ans[j]);
}
}
return 0;
}(2)、浮点类型解
double a[maxn][maxn], ans[maxn];
int cnt, Free[maxn];
int Gauss(int equ, int var)
{
for (int i = 0; i <= var; i++)
{
ans[i] = 0;
Free[i] = 1;
}
int row, col, MaxRow;
col = 0;
for (row = 0; row < equ && col < var; row++, col++)
{
MaxRow = row;
for (int i = row + 1; i < equ; i++)
{
if (fabs(a[i][col]) > fabs(a[MaxRow][col]))
MaxRow = i;
}
if (MaxRow != row)
{
for (int i = row; i <= var; i++)
swap(a[row][i], a[MaxRow][i]);
}
if (fabs(a[row][col]) < eps)
{
row--;
continue;
}
for (int i = row + 1; i < equ; i++)
{
if (fabs(a[i][col]) > eps)
{
double temp = a[i][col] / a[row][col];
for (int j = col; j <= var; j++)
a[i][j] -= a[row][j] * temp;
a[i][col] = 0;
}
}
}
for (int i = row; i < equ; i++)
{
if (fabs(a[i][col]) > eps)
return -1;
}
double temp;
if (row < var)
{
for (int i = row - 1; i >= 0; i--)
{
int free_num = 0, idx;
for (int j = 0; j < var; j++)
{
if (a[i][j] && Free[j])
{
free_num++;
idx = j;
}
}
if (free_num > 1)
continue;
temp = a[i][var];
for (int j = 0; j < var; j++)
{
if (a[i][j] && j != idx)
temp -= a[i][j] * ans[j];
}
ans[idx] = temp / a[i][idx];
Free[idx] = 0;
}
return var - row;
}
for (int i = var - 1; i >= 0; i--)
{
temp = a[i][var];
for (int j = i + 1; j < var; j++)
{
if (a[i][j])
temp -= a[i][j] * ans[j];
}
ans[i] = temp / a[i][i];
}
return 0;
}(3)、整数类型解
int a[maxn][maxn];
int ans[maxn];
int Free[maxn];
int GCD(int a, int b)
{
if (!b)
return a;
return GCD(b, a % b);
}
int LCM(int a, int b)
{
return a / GCD(a, b) * b;
}
int Fabs(int x)
{
if (x < 0)
return -x;
return x;
}
int Gauss(int equ, int var)
{
for (int i = 0; i <= var; i++)
{
ans[i] = 0;
Free[i] = 1;
}
int row, col, MaxRow;
col = 1;
for (row = 1; row <= equ && col < var; row++, col++)
{
MaxRow = row;
for (int i = row + 1; i <= equ; i++)
{
if (Fabs(a[i][col]) > Fabs(a[MaxRow][col]))
MaxRow = i;
}
if (MaxRow != row)
{
for (int i = row; i <= var; i++)
swap(a[row][i], a[MaxRow][i]);
}
if (!a[row][col])
{
row--;
continue;
}
for (int i = row + 1; i <= equ; i++)
{
if (a[i][col])
{
int lcm = LCM(Fabs(a[i][col]), Fabs(a[row][col]));
int T1 = lcm / Fabs(a[i][col]);
int T2 = lcm / Fabs(a[row][col]);
if (a[i][col] * a[row][col] < 0)
T2 = -T2;
for (int j = col; j <= var; j++)
a[i][j] = a[i][j] * T1 - a[row][j] * T2;
}
}
}
for (int i = row; i <= equ; i++)
{
if (a[i][col])
return -1;
}
int temp;
if (row < var)
{
return var - row;
}
for (int i = var - 1; i > 0; i--)
{
temp = a[i][var];
for (int j = i + 1; j < var; j++)
{
if (a[i][j])
temp -= a[i][j] * ans[j];
}
ans[i] = temp / a[i][i];
}
return 0;
}(4)、模线性方程组
int a[maxn][maxn];
int Gauss(int equ, int var)
{
int row, col = 0;
for (row = 0; row < equ && col < var; row++, col++)
{
int MaxRow = row;
for (int i = row + 1; i < equ; i++)
{
if (abs(a[i][col]) > abs(a[MaxRow][col]))
MaxRow = i;
}
if (row != MaxRow)
{
for (int i = row; i <= var; i++)
swap(a[row][i], a[MaxRow][i]);
}
if (!a[row][col])
{
row--;
continue;
}
for (int i = row + 1; i <= equ; i++)
{
if (a[i][col])
{
int T = a[i][col] * q_pow(a[row][col], mod - 2, mod) % mod;
for (int j = col; j <= var; j++)
a[i][j] = (a[i][j] - a[row][j] * T % mod + mod) % mod;
}
}
}
for (int i = row; i <= equ; i++)
{
if (a[i][col])
return -1;
}
if (row < var)
return var - row;
for (int i = var - 1; i >= 0; i--)
{
int temp = a[i][var];
for (int j = i + 1; j < var; j++)
{
if (a[i][j])
{
temp -= a[i][j] * x[j];
temp = (temp % mod + mod) % mod;
}
}
x[i] = temp * q_pow(a[i][i], mod - 2, mod) % mod;
}
return 0;
}二、线性基
1、线性基是一个数的集合,并且每个序列都拥有至少一个线性基,取线性基中若干个数异或起来可以得到原序列中的任何一个数
2、线性基的构造
int d[maxn];
void add(ll x)
{
for (int i = 60; i >= 0; i--)
{
if (x & (1ll << i))//注意,如果i大于31,前面的1的后面一定要加ll
{
if (d[i])x ^= d[i];
else
{
d[i] = x;
break;//插入成功就退出
}
}
}
}边栏推荐
- Pycharm项目使用pyinstalle打包过程中问题及解决方案
- Viewing technological changes through Huawei Corps (V): smart Park
- OLAP数据库引擎如何选型?
- How can the sports app keep the end-to-side background alive to make the sports record more complete?
- 基于HAL库的LED驱动库
- 200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration
- Foresniffer tutorial: extracting data
- SQL必需掌握的100个重要知识点:组合查询
- ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528
- 小程序中读取腾讯文档的表格数据
猜你喜欢

The reasoning delay on iphone12 is only 1.6 MS! Snap et al. Analyzed the transformer structure latency in detail, and used NAS to find out the efficient network structure of mobile devices

煥發青春的戴爾和蘋果夾擊,兩大老牌PC企業極速衰敗

Mysql database foundation: views and variables

China will force a unified charging interface. If Apple does not bow its head, iPhone will be kicked out of the Chinese market

The first China Digital Collection conference will be held soon

Deep dive kotlin synergy (18): hot and cold data flow

ArrayList and sequence table

语音识别-基础(一):简介【语音转文本】

文件共享服务器

OLAP数据库引擎如何选型?
随机推荐
LVGL 8.2 re-coloring
第一届中国数字藏品大会即将召开
基于HAL库的LED驱动库
CP2112使用USB转IIC通信教学示例
SQL必需掌握的100个重要知识点:插入数据
Cp2112 teaching example of using USB to IIC communication
Pytorch notes: validation, model eval V.S torch. no_ grad
语音识别-基础(一):简介【语音转文本】
文件共享服务器
LVGL 8.2 Simple Drop down list
LVGL 8.2 Image
The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
小程序中读取腾讯文档的表格数据
LVGL8.2 Simple Checkboxes
DataX JSON description
Pytorch notes torch nn. BatchNorm1d
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
LVGL 8.2 Simple Colorwheel
电商两位大佬花边新闻刷屏,代表电商回归正常,将有利于实体经济