Poj1830-Use of Gaussian elimination method (good)

Source: Internet
Author: User

First, let's talk about Gaussian elimination: it refers to the process of converting the Augmented Matrix into an upper triangle matrix and then determining the solution. Assume that a * x = B. A is a matrix of N * m, X is the solution matrix of M * 1, and B is the coefficient matrix of N * 1. Then, the Augmented Matrix is a matrix of N * (m + 1.

There are three situations after Gaussian elimination in the Augmented Matrix: 1. The first M number of a row is all 0, but the last M number is not 0. In this way, the contradiction is exported, so there is no solution to the original equation. 2. When M = n and the number of M in the last row is not 0, there is a unique solution. 3. In other cases, there are infinite solutions. For example, if the number of M in the first row is not 0.

   The Gaussian elimination algorithm uses the elementary transformation of the matrix. The main idea is to convert the angle element into 1, and then solve the problem from the bottom to the bottom.

The following figure shows how 1830 is associated with Gaussian elimination:

  There are two States for each lamp: Moving and not moving, represented by 1, 0. Then we can construct the column vector x = [X1, x2 .... XN], xi = 0 or 1. coefficient matrix B [B1, B2... BN], transformation matrix A [n] [N]; suppose a * x = B, then biIt is equal to the multiplication of the elements in line I of A and X, so we can set a [I] [J] as follows: if the operation on the J lamp affects I, then a [I] [J] = 1.

If the initialization status of I is different from the target status, B [I] = 1. Because B [I]Equals to the multiplication of the I-th row element of a and X. That is to say, whether the final state of the lamp I changes depends on X, because B [I] = sum (A [I] [J] * X [J]), the sum here is regarded as the modulo 2 operation, that is, ^, so whether the final state and initial ratio of the lamp I have changed is determined by each lamp related to I, this is why J affects I set a [I] [J] = 1. That is why B [I] is the exclusive or between the initial and final states ...... The result is the number of solutions for finding the equation A * x = B.

# Include <cstdlib> # include <iostream> # include <cstdio> # include <cmath> # include <cstring> # include <algorithm> # include <set> # include <map> # define ll long # define INF 0x7fffffff # define linf 0x7fffffffffffff # define e 1e-9 # define M 100 # define N 32 using namespace STD; int N, K, M; bool X [N]; bool ma [N] [N]; int Gauss () // n * (n + 1) matrix, step ladder {int I = 0, j = 0; For (; I <n, j <n;) {int K; For (k = I; k <N; k ++) if (MA [k] [J]! = 0) break; If (k = N) {J ++; continue;} if (I! = K) {for (int l = J; L <= N; l ++) // swap (MA [I] [L], ma [k] [l]);} For (INT r = I + 1; r <n; r ++) if (MA [r] [J]) {for (int l = J; L <= N; l ++) Ma [r] [l] ^ = ma [I] [l]; // It must be an exception or // ma [r] [l]-= ma [I] [l];} I ++, J ++ ;} for (INT r = I; r <n; r ++) if (MA [R] [N]! = 0) return 0; // return (1 <(n-I); // because the solution can only be 0 and 1, how many Yuan are left at the end, returns the number of second power solutions} int main () {# ifndef online_judge freopen ("ex. in "," r ", stdin); # endif int t; scanf (" % d ", & T); While (t --) {memset (MA, 0, sizeof (MA); scanf ("% d", & N); For (INT I = 0; I <n; ++ I) scanf ("% d ", & X [I]); For (INT I = 0; I <n; ++ I) {scanf ("% d", & Ma [I] [N]); ma [I] [N] ^ = x [I]; MA [I] [I] = 1;} int A, B; while (scanf ("% d", & A, & B) & (! = 0 | B! = 0) {ma [b-1] [A-1] = 1; // direction} int num = Gauss (); If (Num) printf ("% d \ n ", num); else printf ("oh, it's impossible ~!! \ N ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.