HDU 2827 高斯消元
模板的高斯消元....
/** @Date : 2017-09-26 18:05:03 * @FileName: HDU 2827 高斯消元.cpp * @Platform: Windows * @Author : Lweleth (SoungEarlf@gmail.com) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h> #define LL long long #define PII pair #define MP(x, y) make_pair((x),(y)) #define fi first #define se second #define PB(x) push_back((x)) #define MMG(x) memset((x), -1,sizeof(x)) #define MMF(x) memset((x),0,sizeof(x)) #define MMI(x) memset((x), INF, sizeof(x)) using namespace std; const int INF = 0x3f3f3f3f; const int N = 1e5+20; const double eps = 1e-8; LL a[110][110]; LL mod; int n; LL gauss(int n) { int last, row; LL ans = 1; int flag = 0; for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++)//从i+1行开始的所有行 { last = i; row = j; while(a[row][i])//到i列为止都为0 { LL t = a[last][i] / a[row][i]; for(int k = i; k < n; k++) { a[last][k] = (a[last][k] % mod - (a[row][k] * t % mod) + mod) % mod; while(a[last][k] < 0) a[last][k] += mod; } swap(last, row);//不断交换 } if(last != i)//保证上比下大 { flag++ ;//行基本操作 交换 正负号改变 for(int k = 0; k < n; k++) swap(a[i][k], a[last][k]); } } //cout << a[i][i] << endl; if(a[i][i] == 0) // return 0; else //对角线之积 ans = (ans * a[i][i] % mod + mod) % mod; } if(flag % 2) ans *= -1; while(ans < 0) ans += mod; return ans; } int main() { while(cin >> n >> mod) { MMF(a); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%lld", &a[i][j]); LL ans = gauss(n); printf("%lld\n", ans); } return 0; }