行列式值计算

	int x, y;
	
	int exGCD(int a, int b) {
		if (0 == b) {
			x = 1; y = 0;
			return a;
		}
		int r = exGCD(b, a % b);
		int t = x; 
		x = y;
		y = t - a / b * y;
		return r;
	}

int det(int n) { // n: n行n列 int ans = 1; boolean flag = true; // 行交换次数奇偶 for (int i = 0, j, k; i < n; ++i) { if (0 == C[i][i]) { for (j = i + 1; j < n; ++j) if (C[j][i] > 0) break; if (j >= n) return 0; // 整个行列式的值为0 flag = !flag; for (k = i; k < n; ++k) { int temp = C[i][k]; C[i][k] = C[j][k]; C[j][k] = temp; } } ans = ans * C[i][i] % MOD; exGCD(C[i][i], MOD); // 求C[i][j]的乘法逆元 x = (x + MOD) % MOD; for (k = i + 1; k < n; ++k) C[i][k] = C[i][k] * x % MOD; for (j = i + 1; j < n; ++j) { if (0 != C[j][i]) { for (k = i + 1; k < n; ++k) C[j][k] = ((C[j][k] - C[i][k] * C[j][i] % MOD + MOD) % MOD); } } } if (flag) return ans; return MOD - ans; }

posted on 2013-04-01 18:56  Sure_Yi  阅读(302)  评论(0编辑  收藏  举报

导航