题目链接:
SPOJ DETER3 Find The Determinant III
题意:
给一个 n?n 的行列式 A 和数
分析;
注意求 det 时,需要返回 (res + mod) % mod
而不能直接返回 res 。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 210;ll mod;
ll C[MAX_N][MAX_N];ll det(ll mat[][MAX_N], int n)
{for(int i = 0; i < n; ++i) {for(int j = 0; j < n; ++j) {mat[i][j] = (mat[i][j] % mod + mod) % mod;}}ll res = 1;int cnt = 0;for(int i = 0; i < n; ++i) {for(int j = i + 1; j < n; ++j) {while(mat[j][i]) {ll t = mat[i][i] / mat[j][i];for(int k = i; k < n; ++k) {mat[i][k] = (mat[i][k] - mat[j][k] * t % mod) % mod;swap(mat[i][k], mat[j][k]);}cnt++;}}if(mat[i][i] == 0) return 0;res = res * mat[i][i] % mod;}if(cnt & 1) res = -res;return (res + mod) % mod;//return res; //WRONG ANSWER
}int main()
{int n;while(~scanf("%d%lld", &n, &mod)) {for(int i = 0; i < n; ++i) {for(int j = 0; j < n; ++j) {scanf("%lld", &C[i][j]);}}printf("%lld\n", det(C, n));}return 0;
}