当前位置:网站首页>高斯消元求解矩阵的逆(gauss)
高斯消元求解矩阵的逆(gauss)
2022-07-26 09:29:00 【逃夭丶】
基本理论就是线性代数中的:
若A是可逆矩阵,(A,E) ~ (E,B),那么B就是A的逆矩阵。
模板题目:P4783 【模板】矩阵求逆
#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;
}
边栏推荐
猜你喜欢
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
arc-gis基础操作3
After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
Fiddler下载安装
js中树与数组的相互转化(树的子节点若为空隐藏children字段)
antUI中a-modal 拖拽功能制作
[MySQL] detailed explanation of redo log, undo log and binlog (4)
Drawing shadow error diagram with MATLAB
Cat installation and use
Selection and practice of distributed tracking system
随机推荐
Arc GIS basic operation 3
Process32First返回false,错误x信息24
uni-app学习总结
大二上第一周学习笔记
大二上第五周学习笔记
PHP一次请求生命周期
phpexcel导出emoji符号报错
Calling DLL to start thread
Process32first returns false, error x message 24
Audio and video knowledge
The problem of the sum of leetcode three numbers
Neural network and deep learning-6-support vector machine 1-pytorch
Sliding window, double pointer, monotone queue, monotone stack
The provincial government held a teleconference on safety precautions against high temperature weather across the province
附加到进程之后,断点显示“当前不会命中断点 还没有为该文档加载任何符号”
Force button list question
Fiddler抓包工具之移动端抓包
OnTap 9 file system limitations
Object 的Wait Notify NotifyAll 源码解析
Force deduction brush questions, sum of three numbers