当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Drawing shadow error diagram with MATLAB

Arc GIS basic operation 3

arc-gis的基本使用2

Fiddler packet capturing tool for mobile packet capturing

mysql5.7.25主从复制(单向)

CSV data file settings of JMeter configuration components

uni-app学习总结
![[MySQL] understand the important architecture of MySQL (I)](/img/89/5fb595b0112fac987626857b76f9a4.png)
[MySQL] understand the important architecture of MySQL (I)

Basic use of ArcGIS 1

【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)
随机推荐
网络流学习笔记
asp.net 使用redis缓存
cocoapods的安装和使用
Global variables in DLL
Network flow learning notes
Registration module use case writing
asp. Net using redis cache (2)
RMQ学习笔记
nodejs中mysql的使用
ZXing简化版,转载
Calling DLL to start thread
V-permission add permission
Audio and video knowledge
The problem of the sum of leetcode three numbers
E. Two Small Strings
opencv图像处理
How to add a PDB
tabbarController的使用
MySql5.7.25源码安装记录
el-table实现增加/删除行,某参数跟着变