SPOJ - Find The Determinant III 计算矩阵的行列式答案 + 辗转相除法思想
SPOJ -Find The Determinant III
参考:https://blog.csdn.net/zhoufenqin/article/details/7779707
参考中还有几个关于行列式的性质。
题意:
计算矩阵的行列式答案
思路:
计算行列式的基本方法就是把矩阵化成上三角或下三角,然后观察对角线的元素,如果其中有一个元素为0则答案为0,否则行列式的值就是对角线上各个元素的乘积。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
// const int mod = 998244353;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = ;
ll a[maxn][maxn],mod;
int n;
void cal(){
ll ans = ;int sign = ;
for(int i=; i<=n; i++){ //当前行
for(int j=i+; j<=n; j++){
int x = i, y = j;
while(a[y][i]){ //利用gcd的方法,不停地进行辗转相除,达到消去其他行对应列元素的目的
ll t = a[x][i] / a[y][i];
for(int k=i; k<=n; k++)
a[x][k] = (a[x][k] - a[y][k]*t)%mod;
swap(x,y);
} if(x != i){ //奇数次交换,则D=-D'整行交换
for(int k = ; k<=n; k++){
swap(a[i][k], a[x][k]);
}
sign ^= ;
}
}
if(a[i][i] == ){ //斜对角中有一个0,则结果为0
puts("");
return;
}
else ans = ans * a[i][i] %mod;
}
if(sign) ans *= -;
if(ans < ) ans += mod;
printf("%lld\n", ans);
}
int main(){
while(~scanf("%d%lld", &n, &mod)){
for(int i=; i<=n; i++){
for(int j=; j<=n; j++)
scanf("%lld", &a[i][j]);
} cal();
}
return ;
}
SPOJ - Find The Determinant III 计算矩阵的行列式答案 + 辗转相除法思想的更多相关文章
- C++中计算矩阵的行列式
使用eigen库: 求行列式: #include <iostream> #include <Eigen/Dense> using namespace std; using na ...
- SPOJ - DETER3:Find The Determinant III (求解行列式)
Find The Determinant III 题目链接:https://vjudge.net/problem/SPOJ-DETER3 Description: Given a NxN matrix ...
- 【原创】开源Math.NET基础数学类库使用(15)C#计算矩阵行列式
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 上个月 ...
- 【原创】开源Math.NET基础数学类库使用(16)C#计算矩阵秩
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 上个月 ...
- bzoj 2107: Spoj2832 Find The Determinant III 辗转相除法
2107: Spoj2832 Find The Determinant III Time Limit: 1 Sec Memory Limit: 259 MBSubmit: 154 Solved: ...
- 开源Math.NET基础数学类库使用(16)C#计算矩阵秩
原文:[原创]开源Math.NET基础数学类库使用(16)C#计算矩阵秩 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4 ...
- 开源Math.NET基础数学类库使用(15)C#计算矩阵行列式
原文:[原创]开源Math.NET基础数学类库使用(15)C#计算矩阵行列式 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...
- 【原创】开源Math.NET基础数学类库使用(17)C#计算矩阵条件数
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 上个月 ...
- Openjudge计算概论-计算矩阵边缘元素之和
/*======================================================================== 计算矩阵边缘元素之和 总时间限制: 1000ms ...
随机推荐
- 最火的分布式 HTAP 数据库 TiDB - 入门实践教程
偶然在某篇博客看到了 TiDB,一个融合 OLTP 和 OLAP 的分布式开源数据库, GitHub 上 Star 很多,然后 watch 了,发现 commit 和 pull request 一直都 ...
- 戴尔PowerEdge T110 Ⅱ服务器U盘安装Windows Server 2019 DataCenter
一. 下载准备 准备工作——下载Microsoft Windows Server 2019 官方简体中文激活版 (MSDN)原版iso镜像 准备工作——安装刻录软件UltraISO,单文件绿色版就够用 ...
- 云计算网络基础笔记及VLAN交换机配置
- LDAP 服务搭建和后期管理
LDAP 服务 本文主要在debian配置,如果需要在CentOS上部署,需要修改大部分的路劲,这里需要自行修改. LDAP 服务按照个人理解,也可使理解为一个数据库,但是这个数据库的读写性能不像 M ...
- C#打开并选择特定类型文件并返回文件名
public string[] GetOpenFileDialogReturnFileFullName(bool multiSelect = false) { ...
- 5.Go-封装、继承、接口、多态和断言
面向对象 Go语言开发者认为:面向对象就是特定类型(结构体)有着自己的方法,利用这个方法完成面向对象编程, 并没有提封装.继承.多态.所以Go语言进行面向对象编程时,重点在于灵活使用方法. Go语言有 ...
- Spring Cloud 相关资料链接
Spring Cloud中文网:https://springcloud.cc/ Spring Cloud API:https://springcloud.cc/spring-cloud-dalston ...
- LeetCode——264. Ugly Number II
题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...
- Redis简单梳理及集群配置
**REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANSI C语言编写.遵 ...
- imageloader+图片压缩
public class MainActivity extends AppCompatActivity { private ImageView ivIcon; @Override protected ...