当前位置:网站首页>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;
}
边栏推荐
- asp. Net using redis cache
- OFDM Lecture 16 - OFDM
- How to add a PDB
- 2019 ICPC Asia Yinchuan regional (water problem solution)
- Search module use case writing
- [untitled]
- 附加到进程之后,断点显示“当前不会命中断点 还没有为该文档加载任何符号”
- JS判断数据类型 Object.prototype.toString.call和typeof
- 小白搞一波深拷贝 浅拷贝
- Fiddler packet capturing tool for mobile packet capturing
猜你喜欢
V-permission add permission
Your login IP is not within the login mask configured by the administrator
Basic use of ArcGIS 1
2019 ICPC Asia Yinchuan Regional(水题题解)
Zxing simplified version, reprinted
Does volatile rely on the MESI protocol to solve the visibility problem? (top)
Personality test system V1.0
小白搞一波深拷贝 浅拷贝
Windows下Redis哨兵模式搭建
【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)
随机推荐
V-for dynamically sets the SRC of img
Qt随手笔记(二)Edit控件及float,QString转化、
Global variables in DLL
[shutter -- layout] detailed explanation of the use of align, center and padding
matlab中的AR模型短时预测交通流
Does volatile rely on the MESI protocol to solve the visibility problem? (top)
PHP一次请求生命周期
2020-12-29
Registration module use case writing
opencv图像处理
MySQL transaction
sublime 安装插件
微信小程序AvatarCropper 头像裁剪
Table extraction for opencv table recognition (2)
暑假末尾学习笔记
asp.net 使用redis缓存(二)
服务器、客户端双认证
[MySQL] understand the important architecture of MySQL (I)
莫队学习总结(二)
nodejs服务后台执行(forever)