zoukankan      html  css  js  c++  java
  • (高斯消元)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
    ",Gause());
        }
        return 0;
    }
  • 相关阅读:
    RDay2-Problem 2 B
    杭电 1862 EXCEL排序(sort+结构体)
    杭电 2803 The MAX(sort)
    杭电 5053 the Sum of Cube(求区间内的立方和)打表法
    杭电 2089 不要62
    杭电 4548 美素数(素数打表)
    杭电2098 分拆素数和
    杭电1722 Cake (分蛋糕)
    素数判定 (素数打表)
    最小公倍数
  • 原文地址:https://www.cnblogs.com/quintessence/p/6940843.html
Copyright © 2011-2022 走看看