当前位置:网站首页>高斯消元求解矩阵的逆(gauss)
高斯消元求解矩阵的逆(gauss)
2022-07-26 09:29:00 【逃夭丶】
基本理论就是线性代数中的:
若A是可逆矩阵,(A,E) ~ (E,B),那么B就是A的逆矩阵。
模板题目:P4783 【模板】矩阵求逆

#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 = 410;
const int inf = 0x3f3f3f3f3f;
const ll mod = 1e9+7;
const double eps = 1e-7;
int n;
ll a[maxn][maxn<<1];
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==0){
x = 1;
y = 0;
return a;
}
ll g = exgcd(b,a%b,y,x);
y -= a/b * x;
return g;
}
ll inverse(ll a,ll m)
{
ll x,y;
ll g = exgcd(a,m,x,y);
return (x%m + m) % m;
}
bool gauss(){
for(int i=1;i<=n;i++){
int k=i;
for(int j=i+1;j<=n;j++){
if(a[j][i]>a[k][i]){
k=j;
}
}
if(!a[k][i]){
printf("No Solution\n");
return false;
}
if(i!=k) swap(a[k],a[i]);
ll kp = inverse(a[i][i],mod);
for(k = 1;k<=n;k++){
if(k!=i){
ll p = a[k][i]*kp%mod;
for(int j=i;j<=(n<<1);j++)
a[k][j] = ((a[k][j]-a[i][j]*p)%mod+mod)%mod;
}
}
for(int j=1;j<=(n<<1);j++)
a[i][j] = a[i][j]*kp%mod;
}
for(int i=1;i<=n;i++){
for(int j=n+1;j<(n<<1);j++){
printf("%lld ",a[i][j]);
}
printf("%lld\n",a[i][n<<1]);
}
return true;
}
int main(void)
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%lld",&a[i][j]);
}
a[i][i+n] = 1;
}
gauss();
return 0;
}
边栏推荐
- 大二上第五周学习笔记
- 点击input时,不显示边框!
- Global variables in DLL
- 微信小程序AvatarCropper 头像裁剪
- Add DLL
- Custom password input box, no rounded corners
- Implementation of fragment lazy loading after multi-layer nesting
- Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
- asp.net 使用redis缓存
- js中树与数组的相互转化(树的子节点若为空隐藏children字段)
猜你喜欢

Zipkin installation and use
![[shutter -- layout] detailed explanation of the use of align, center and padding](/img/01/c588f75313580063cf32cc01677600.jpg)
[shutter -- layout] detailed explanation of the use of align, center and padding

keepalived 实现mysql自动故障切换

mysql5.7.25主从复制(单向)

tabbarController的使用

Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown

MySql5.7.25源码安装记录

正则表达式

matlab simulink实现模糊pid对中央空调时延温度控制系统控制

注册模块用例编写
随机推荐
C managed and unmanaged
matlab中的AR模型短时预测交通流
简单行人重识别代码到88%准确率 郑哲东 准备工作
大二上第三周学习笔记
C# 托管与非托管
malloc分配空间失败,并且不返回null
[Online deadlock analysis] by index_ Deadlock event caused by merge
What is asynchronous operation
Thread Join 和Object wait 的区别
V-permission add permission
I'm faded
添加dll
Neural network and deep learning-6-support vector machine 1-pytorch
Elastic APM installation and use
asp.net 使用redis缓存
服务器环境配置全过程
TabbarController的封装
吴恩达机器学习之线性回归
[untitled]
Smart gourmet C language