(高斯消元)HDU2827 The Evaluation of Determinant

The determinant is quite important in Linear Algebras, but I think that almost everyone who has ever learnt Linear Algebras is tired of the complicated and tedious calculations of determinant. Actually, it’s not the job we should do, isn’t it? As an outstanding Geek, why don’t we just ask computers to do these? 


Give you a determinant D (it’s ensured that the result of it is an integer) and m, try to get the result of this determinant mod m, and m = p1 * p2 …… pn, all the pi are different. You can assume 1000 < pi < 10000, aij < 1000, and m can be fit in 32-bit signed integer. 

InputInput two integers n and m in the first line, n represents the scale of the determinant. (n <= 100) 
 Then comes an n * n matrix, the determinant’s component aij means the one in row i and column j. 
OutputOutput the result of the determinant D mod m.Sample Input

2 1009
1 2
3 4

Sample Output

1007

用高斯消元法化为对角线形式即可。

纯暴力进行过多swap的高斯消元会TLE

#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <vector>
#include <stack>
#define mp make_pair
//#define P make_pair
#define MIN(a,b) (a>b?b:a)
//#define MAX(a,b) (a>b?a:b)
typedef long long ll;
typedef unsigned long long ull;
const int MAX=1e3+5;
const int MAX_V=1e3+5;
const int INF=1e9+5;
const ll INF2=4e18+5;
const double M=4e18;
using namespace std;
const int MOD=1e9+7;
typedef pair<ll,int> pii;
const double eps=0.000000001;
#define rank rankk
ll n,m;
ll a[MAX][MAX];
ll Gause()
{
    ll re=1;
    int st=0;
    for(ll i=1;i<=n;i++)
    {
        for(ll j=i+1;j<=n;j++)
        {
            ll x=i,y=j;
            while(a[y][i])
            {
                ll ex=a[x][i]/a[y][i];
                for(ll s=i;s<=n;s++)
                {
                    a[x][s]=((a[x][s]-ex*a[y][s]%m)%m+m)%m;
                }
                swap(x,y);
            }
            if(x!=i)
            {
                for(ll s=i;s<=n;s++)
                    swap(a[i][s],a[x][s]);
                st^=1;
            }
        }
        if(!a[i][i])
            return 0;
    }
    for(ll i=1;i<=n;i++)
        re=(re*a[i][i]%m+m)%m;
    if(st)
        re*=-1;
    if(re<0)
        re+=m;
    return re;
}
int main()
{
    while(~scanf("%I64d%I64d",&n,&m))
    {
        for(ll i=1;i<=n;i++)
            for(ll j=1;j<=n;j++)
            {
                scanf("%I64d",&a[i][j]);
                a[i][j]=(a[i][j]%m+m)%m;
            }
        printf("%I64d\n",Gause());
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/quintessence/p/6940843.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值