当前位置:网站首页>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

Windows backs up the database locally by command

OFDM Lecture 16 - OFDM

MySQL transaction

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

docker配置mysql集群

配置ADCS后访问certsrv的问题

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

2019 ICPC Asia Yinchuan regional (water problem solution)

Fiddler download and installation
随机推荐
js 表格自动循环滚动,鼠标移入暂停
antd TreeSelect获取父节点的值
php执行shell脚本
Wechat applet avatarcropper avatar clipping
搜索模块用例编写
Force deduction brush questions, sum of three numbers
如何添加一个PDB
arc-gis基础操作3
Drawing shadow error diagram with MATLAB
uni-app学习总结
V-for dynamically sets the SRC of img
matlab simulink实现模糊pid对中央空调时延温度控制系统控制
The difference between thread join and object wait
Exception handling mechanism II
I'm faded
设置视图动态图片
Arc GIS basic operation 3
Add DLL
matlab中的AR模型短时预测交通流
2020-12-29