当前位置:网站首页>Gauss elimination solves the inverse of matrix (Gauss)
Gauss elimination solves the inverse of matrix (Gauss)
2022-07-26 09:33:00 【Run away】
The basic theory is in linear algebra :
if A It's a reversible matrix ,(A,E) ~ (E,B), that B Namely A The inverse matrix .
Template title :P4783 【 Templates 】 Matrix inversion 

#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;
}
边栏推荐
猜你喜欢
![[MySQL] understand the important architecture of MySQL (I)](/img/89/5fb595b0112fac987626857b76f9a4.png)
[MySQL] understand the important architecture of MySQL (I)

小白搞一波深拷贝 浅拷贝
![[Online deadlock analysis] by index_ Deadlock event caused by merge](/img/67/0a02ad248c3ab21d3240e12aa23313.png)
[Online deadlock analysis] by index_ Deadlock event caused by merge

CSV data file settings of JMeter configuration components

Node 内存溢出及V8垃圾回收机制

Fiddler下载安装

Exception handling mechanism II

服务器、客户端双认证(2)

Fiddler packet capturing tool for mobile packet capturing

【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)
随机推荐
asp.net 使用redis缓存(二)
The problem of the sum of leetcode three numbers
Solve "note: one or more layouts are missing the layout_width or layout_height attributes."
[Online deadlock analysis] by index_ Deadlock event caused by merge
opencv图像处理
nodejs服务后台执行(forever)
Calling DLL to start thread
设置视图动态图片
IIS网站配置
微信小程序学习笔记1
Global variables in DLL
微信小程序图片无法显示时显示默认图片
I'm faded
配置ADCS后访问certsrv的问题
吴恩达机器学习之线性回归
Neural network and deep learning-6-support vector machine 1-pytorch
ZXing简化版,转载
asp. Net using redis cache (2)
malloc分配空间失败,并且不返回null
JS one line code to obtain the maximum and minimum values of the array