The Evaluation of Determinant(求行列式mod一个数的值)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#define LL long long
using namespace std;
LL n,m,A[][],p[],pos,d[],r[],len,B[][];
bool vd[]={};
void prime()
{
pos=;
for(int i=;i<;i++)
{
if(!vd[i])
{
if(i>) p[pos++]=i;
for(int j=(i<<);j<;j+=i)
vd[i]=;
}
}
}
void deal(LL k)
{
len=;
for(int i=;i<pos&&k!=;i++)
{
if(k%p[i]==)
{
while(k%p[i]==)
{
d[len++]=p[i];
k/=p[i];
}
}
}
}
LL exp(LL a,LL b,LL mod)
{
LL ans=;
while(b)
{
if(b&)ans=ans*a%mod;
a=a*a%mod;b>>=;
}
return ans;
}
void ex_gcd(LL a,LL b,LL &dd,LL &x,LL &y)
{
if(b==)
x=,y=,dd=a;
else
{
ex_gcd(b,a%b,dd,y,x);
y-=x*(a/b);
}
}
LL gauss(LL mod)
{
bool flag=;
LL ans=;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
B[i][j]=A[i][j];
for(int k=;k<n-;k++)
{
LL max_b=B[k][k];int bin=k;
for(int i=k+;i<n;i++)
if(B[i][k]>max_b)
max_b=B[i][k],bin=i;
if(bin!=k)
{
for(int i=k;i<n;i++)
swap(B[bin][i],B[k][i]);
flag^=;
}
if(B[k][k]<)B[k][k]+=mod;
LL Ni,y,dd;
ex_gcd(B[k][k],mod,dd,Ni,y);
Ni%=mod;
if(Ni<)Ni+=mod;
for(int j=k+;j<n;j++)
{
B[k][j]=B[k][j]*Ni%mod;
if(B[k][j]<)B[k][j]+=mod;
for(int i=k+;i<n;i++)
{
B[i][j]=(B[i][j]-(B[k][j]*B[i][k])%mod)%mod;
if(B[i][j]<)B[i][j]+=mod;
}
}
ans*=B[k][k];
ans%=mod;
if(ans<)ans+=mod;
}
ans*=B[n-][n-];
ans%=mod;
if(flag)ans=-ans;
if(ans<)ans+=mod;
return ans;
} LL china_remain()
{
LL a,b,c,c1,c2,x,y,dd,N;
a=d[],c1=r[];
if(c1==)c1=d[];
for(int i=;i<len;i++)
{
b=d[i],c2=r[i];
ex_gcd(a,b,dd,x,y);
c=c2-c1;
LL b1=b/dd;
x=((c/dd*x)%b1+b1)%b1;
c1=a*x+c1;
a=a*b1;
}
return c1%m;
}
int main()
{
prime();
while(cin>>n>>m)
{
deal(m);
for(int i=;i<n;i++)
for(int j=;j<n;j++)
cin>>A[i][j];
if(m==)
{
cout<<<<endl;
continue;
}
for(int i=;i<len;i++)
{
r[i]=gauss(d[i]);
}
cout<<china_remain()<<endl;
}
return ;
}
The Evaluation of Determinant(求行列式mod一个数的值)的更多相关文章
- 用php实现斐波那契数列,如: 1, 1, 2, 3, 5, 8, 13, 21, 34。用数组求出第20个数的值。
<?php //用数组 function fib($n){ $array = array(); $array[0] = 1; $array[1] = 1; for($i=2;$i<$n;$ ...
- 用php实现斐波那契数列,如: 1, 1, 2, 3, 5, 8, 13, 21, 34。求出第20个数的值。
<?php function Fibonacci($n){ if ($n <= 0) { return 0; } elseif ($n == 1) { return 1; } else { ...
- SPOJ HIGH(生成树计数,高斯消元求行列式)
HIGH - Highways no tags In some countries building highways takes a lot of time... Maybe that's bec ...
- C. Learning Languages 求联通块的个数
C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring&g ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- 一种快速求fibonacci第n个数的算法
利用动态规则的思路,摒弃传统的递归做法,可以得到一种快速的求fibonacci第n个数的算法: ''' 求第n(从1开始)位fibonacci数 fibonacci数列前两位为0, 1. 后面每一位数 ...
- 【剑指offer】求逆序对的个数
2013-09-07 10:50:31 面试题36:在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字构成一个逆序对.输入一个数组,求出这个数组中逆序对的总数. 小结: 最直观的的方法是: ...
- 用递归方法求n阶勒让德多项式的值
/* Date: 07/03/19 15:40 Description: 用递归法求n阶勒让德多项式的值 { 1 n=0 Pn(x)= { x n=1 { ((2n-1) ...
- 求集合中选一个数与当前值进行位运算的max
求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...
随机推荐
- 擅长排列的小明II
先搜索 出来一点结果之后 看结果之间的 联系 得出 递推公式 . #include<stdio.h> #include<string.h> #include<mat ...
- 离散化+BFS HDOJ 4444 Walk
题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...
- Android 性能优化(9)网络优化( 5)Optimizing Server-Initiated Network Use
Optimizing Server-Initiated Network Use This lesson teaches you to Send Server Updates with GCM Netw ...
- 【转】MySQL常见的运算符及使用
转自:http://www.linuxidc.com/Linux/2016-03/129672.htm MySQL中有4类运算符,它们是: 算术运算符 比较运算符 逻辑运算符 位操作运算符 算术操作符 ...
- 聊聊MyBatis缓存机制
https://tech.meituan.com/mybatis_cache.html 前言 MyBatis是常见的Java数据库访问层框架.在日常工作中,开发人员多数情况下是使用MyBatis的默认 ...
- SQL数据库,增加查询修改以及防sql写入攻击
SQL添加信息 SQL查询信息 SQL修改信息 SQL语句写入攻击: 普通语句添加信息 sql写入语句攻击: 解决方法:分开传送语句与参数关键:@
- Mysql阿里数据源配置参考
maven pom.xml配置 <dependency> <groupId>com.alibaba</groupId> <artifactId>drui ...
- [ SDOI 2006 ] 保安站岗
\(\\\) Description 给出一棵 \(n\) 个节点以 \(1\) 为根的树,一个节点的覆盖半径是 \(1\) ,点有点权 \(val_x\) . 选择一些点,使得点权和最小,同时每个节 ...
- Dojo - 操作Dom的函数
DOM Manipulation You might be seeing a trend here if you have gotten this far in the tutorial, in th ...
- 全志A33平台编译linux(分色排版)sina33
全志A33平台编译linux 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/12 17:36 版本:V1.0 Xshell 5 (Buil ...