SPOJ - DETER3 Find The Determinant III 行列式求值取余

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tawn0000/article/details/82987369

                                     DETER3 - Find The Determinant III

Given a NxN matrix A, find the Determinant of A % P.

Input

Multiple test cases (the size of input file is about 3MB, all numbers in each matrix are generated randomly).

The first line of every test case contains two integers , representing N (0 < N < 201) and P (0 < P < 1,000,000,001). The following N lines each contain N integers, the j-th number in i-th line represents A[i][j] (- 1,000,000,001 < A[i][j] < 1,000,000,001).

Output

For each test case, print a single line contains the answer.

Example

Input:
1 10
-528261590
2 2
595698392 -398355861
603279964 -232703411
3 4
-840419217 -895520213 -303215897
537496093 181887787 -957451145
-305184545 584351123 -257712188

Output:
0
0
2
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 200 + 10;
const int maxm = 1010025;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
#define type long long

int n,m;

int d[maxn];
int g[maxn][maxn];
type c[maxn][maxn];

type det(type a[][maxn],int n)
{
  type ret=1;
  for(int i=1;i<=n;i++)
  {
    for(int j=i+1;j<=n;j++)
    {
      while(a[j][i])
      {
        type t=a[i][i]/a[j][i];
        for(int k=i;k<=n;k++)
            a[i][k]=(((a[i][k]-a[j][k]*t)%m)+m)%m;

        for(int k=i;k<=n;k++)
            swap(a[i][k],a[j][k]);
        ret=-ret;
      }
    }
    if(a[i][i]==0)  return 0;
    ret=(((ret*a[i][i])%m)+m)%m;
  }
  if(ret<0)   ret=-ret;
  return ret;
}

int main()
{
  while(~scanf("%d%d",&n,&m))
  {
    for(int i = 1; i <= n; i++)
      for(int j = 1; j <= n; j++)
         {
           scanf("%lld",&c[i][j]);
           c[i][j] = ((c[i][j] % m)+m)%m;
         }
    type ans = ((det(c,n)%m)+m)%m;
    printf("%lld\n",ans);
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/Tawn0000/article/details/82987369
今日推荐