当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
sublime 安装插件
2019 ICPC Asia Yinchuan Regional(水题题解)
配置ADCS后访问certsrv的问题
The difference between thread join and object wait
电机转速模糊pid控制
Smart gourmet C language
asp.net 使用redis缓存(二)
Mo team learning notes (I)
[untitled]
V-for dynamically sets the SRC of img
docker配置mysql集群
Drawing shadow error diagram with MATLAB
PHP一次请求生命周期
Qt随手笔记(三)在vs中使用QtCharts画折线图
Jmeter配置元件之CSV数据文件设置
M-ary number STR to n-ary number
解决IE7 & IE8 存储cookie问题
VS2019配置opencv
服务器环境配置全过程
Qt随手笔记(二)Edit控件及float,QString转化、







