SPOJ DETER3 Find The Determinant III(行列式求值取模)

2019-01-11 13:12:43 浏览数 (1)

题目链接:https://www.spoj.com/problems/DETER3/en/

       行列式求值取模,高斯消元法


AC代码:

代码语言:javascript复制
#include <bits/stdc  .h>
#define ll long long
#define maxn 205
#define inf 0x3f3f3f3f
using namespace std;
ll B[maxn][maxn];
ll n, mod;

ll det()
{
	ll ans = 1;
	for (int i=1;i<=n;i  ){
		for (int j=i 1;j<=n;j  ){
			while (B[j][i]){
				ll t = B[i][i] / B[j][i];
				for (int k=i;k<=n;k  ){
					B[i][k] = (B[i][k] - B[j][k] * t % mod   mod) % mod;
					swap(B[i][k], B[j][k]);
				}
				ans = -ans;
			}
		}
		if (B[i][i] == 0) return 0;
		ans = (ans * B[i][i]) % mod;
	}
	return (ans   mod) % mod;
}

int main()
{
  while(~scanf("%lld%lld",&n,&mod)){
    for(int i=1;i<=n;i  ){
			for(int j=1;j<=n;j  ){
				scanf("%lld",&B[i][j]);
				B[i][j] %= mod;
			}
		}
    ll ans = det();
    printf("%lldn", ans);
  }
  return 0;
}

0 人点赞