Gaussian elimination method (transpose) Gaussian elimination method is an algorithm in linear algebra. It can be used to solve linear equations, obtain the rank of the matrix, and obtain the inverse matrix of the reversible matrix. The principle of Gaussian elimination method is: if the augmented matrix is converted into an Elementary Line Transformation, AX = B and CX = D are the same equations. Therefore, we can use the elementary row transformation to convert the Augmented Matrix into a row-step array, and then return to find the solution of the equation. The above is a review of linear algebra. Next we will talk about the application of Gaussian elimination method in programming. First, we will introduce the process of Gaussian elimination in the program: (we set the number of equations in the equations to equ, and the number of variable elements to var. Note: n equations are generally used, n yuan, but some questions intentionally make the number of equations different from the number of variables) 1. converts a equations to an augmented matrix. 2. Use Elementary Row Transformation to convert the Augmented Matrix into step arrays. Enumeration k ranges from 0 to equ-1. The column currently being processed is col (initially 0). Each time you find the column below the k (including the k row ), the column with the largest absolute value of the element in the col column is exchanged with row k. If all the elements in the col column are 0, the col + 1 column will be processed, and k will not change. 3. convert to a row-level array to determine the solution. ① When the equation appears (0, 0 ,..., 0, a) and! If it is set to 0, no solution is available. ② The only solution condition is k = equ, that is, the row step formed into a strict upper triangle array. Use the return generation to obtain the solution set one by one. ③ Infinite solution. The condition is k <equ, that is, a strict upper triangle cannot be formed, and the number of free yuan is equ-k. However, some questions require that you determine which variables are not missing. Here we will introduce this solution separately: first, there are at least var-k free variables, that is, there are at least var-k uncertain variables. We should consider all the changes as uncertain. Determine the number of uncertain variable elements in each equation. If there are more than one variable, the equation cannot be solved. If there is only one variable, the variable is determined. The above describes the method for solving integer linear equations. The complexity is O (n3 ). The Method for Solving floating-point linear equations is similar. However, when determining whether the value is 0, add the EPS to eliminate the Accuracy Problem. The following describes how to solve the linear equations with Gaussian elimination method on OJ: POJ 1222 extended lights OUThttp: // acm.pku.edu.cn/JudgeOnline/problem? Id = 1222POJ 1681 Painter's Problemhttp: // acm.pku.edu.cn/JudgeOnline/problem? Id = 1681POJ 1753 Flip Gamehttp: // acm.pku.edu.cn/JudgeOnline/problem? Id = 1753POJ 1830 switch problem http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1830POJ 3185 The Water Bowlshttp: // acm.pku.edu.cn/JudgeOnline/problem? Id = 3185 switch the window and switch the light. It is typical for solving linear equations. The number of equations and the number of variables are the number of rows * Number of columns. You can directly set the template to solve the problem. However, when an infinite solution occurs, it is necessary to enumerate the solution, because it is impossible to determine which solution is the most optimal. POJ 2947 Widget Factoryhttp: // acm.pku.edu.cn/JudgeOnline/problem? Id = 2947. Similar to the general solution to linear equations, you only need to add the remainder in the solution process. Note: When the system of equations has a unique solution, the solution must be between [3, 9. POJ 1166 The Clockshttp: // acm.pku .edu.cn/judgeonline/problem? Id = 1166 classic BFS problems, there are various solutions, you can also use the inverse matrix for matrix multiplication. However, it seems that some problems have been solved by Gaussian elimination method (which has plagued me for N days... being plagued ...), since period 4 is not a prime number, the remainder cannot be obtained during the solution process (because the remainder may increase the solution set). However, the remainder operation is still required for the final solution set, the final solution is not guaranteed to be correct... no one answered questions in discuss for several days... which of the following is your hope ~~ POJ 2065 SETIhttp: // acm.pku.edu.cn/JudgeOnline/problem? Id = 2065 is also a problem for solving the same equations. Since p in the question is a prime number, you can obtain the remainder directly when solving the problem. Just apply the template to solve the problem. (Although there are few AC users, it is still a relatively watery question.) POJ 1487 Single-Player Gameshttp: // acm.pku.edu.cn/JudgeOnline/problem? Id = 1487 a very troublesome question... the description in the question seems to have used the lexical definition in the compilation principle. (after reading it, I don't want to do it ...) the idea of solving the equations is still very good-looking (the premise is that you can read the questions no more than five times ...), however, it is difficult to convert the string expression of the tree into a equations. I use stack + recursion to break it down. First, use the stack idea to find the number of children at the node, and then recursively solve each child. This solution equations are also different... first, we need to solve the floating-point number equations, pay attention to the accuracy problem, and then ask about the uncertain variable, and solve it according to the method described above. After a hard fight, I wrote 6000 + B... in addition, compaction is about giant C ++ WA and G ++ AC. Maybe it's about accuracy... looking at this question and looking at this code, there is no desire to change... hdu OJ 2449 http://acm.hdu.edu.cn/showproblem.php? Pid = a pure Gaussian question in the 2449 Harbin field competition. At that time, he Niu knocked for more than an hour... it is mainly to write a score class and set a high-precision template (to be lazy, Java ...) done ~~ Pay attention to the output when the values are 0 and negative. Fze OJ 1704 http://acm.fzu.edu.cn/problem.php? Pid = a topic of the 1704 Fukui Grand Prix is still a classic switch problem, but the number of equations and the number of variations are different (it is time to test the template ~~), At last, the order of the augmented array is required, and high precision is required ~~ Here, we provide templates for solving the integer linear equations that are fairly satisfactory. (floating point numbers are similar and are not provided )~~ /* Used to obtain the equations of integers. */# include <iostream> # include <string> # include <cmath> using namespace std; const int maxn = 105; int equ, var; // There are equ equations, var variable. The number of rows in the augmented array is equ, ranging from 0 to equ-1, and the number of columns is var + 1, ranging from 0 to var.int a [maxn] [maxn]; int x [maxn]; // solution set. bool free_x [maxn]; // determines whether the variable is uncertain. int free_num; void Debug (void) {int I, j; for (I = 0; I <equ; I ++) {for (j = 0; j <var + 1; j ++) {cout <a [I] [j] <";}cout <endl ;} inline int gcd (int a, int B) {int t; while (B! = 0) {t = B; B = a % B; a = t;} return a;} inline int lcm (int a, int B) {return a * B/gcd (a, B);} // Gaussian elimination method (Gauss-Jordan elimination ). (-2 indicates a floating point solution, but no integer solution.-1 indicates no solution, 0 indicates a unique solution, and greater than 0 indicates an infinite solution, and returns the number of free variables) int Gauss (void) {int I, j, k; int max_r; // the row with the largest absolute value of the current column. int col; // The column currently being processed. int ta, tb; int LCM; int temp; int free_x_num; int free_index; // converts it to a step array. col = 0; // The column currently being processed. for (k = 0; k <equ & col <var; k ++, co L ++) {// enumerate the currently processed rows. // find the row with the largest absolute value of the col column element and swap it with row k. (to reduce the error in Division) max_r = k; for (I = k + 1; I <equ; I ++) {if (abs (a [I] [col])> abs (a [max_r] [col]) max_r = I;} if (max_r! = K) {// exchange with row k. for (j = k; j <var + 1; j ++) swap (a [k] [j], a [max_r] [j]);} if (a [k] [col] = 0) {// indicates that the column k contains all 0, and the next column of the current row is processed. k --; continue;} for (I = k + 1; I <equ; I ++) {// enumerate the rows to be deleted. if (a [I] [col]! = 0) {LCM = lcm (abs (a [I] [col]), abs (a [k] [col]); ta = LCM/abs (a [I] [col]), tb = LCM/abs (a [k] [col]); if (a [I] [col] * a [k] [col] <0) tb =-tb; // if the difference is that two numbers are added together. for (j = col; j <var + 1; j ++) {a [I] [j] = a [I] [j] * ta-a [k] [j] * tb ;}} Debug (); // 1. solution-free: the simplified augmented array exists (0, 0 ,..., a) such a row (! = 0 ). for (I = k; I <equ; I ++, record exchange. if (a [I] [col]! = 0) return-1;} // 2. infinite solution: In the augmented array of var * (var + 1), (0, 0 ,..., 0) This indicates that no strict upper triangle array is formed. // The number of rows displayed is the number of free yuan. if (k <var) {// first, there are var-k free variables, that is, there are at least var-k uncertain variables. for (I = k-1; I> = 0; I --) {// row I won't be (0, 0 ,..., 0), because such rows are from row k to row equ. // Similarly, row I will not be (0, 0 ,..., a),! If it is 0, there is no such solution. free_x_num = 0; // used to determine the number of uncertain variable elements in the row. If more than one variable exists, it cannot be solved and they are still uncertain variable elements. for (j = 0; j <var; j ++) {if (a [I] [j]! = 0 & free_x [j]) free_x_num ++, free_index = j;} if (free_x_num> 1) continue; // No fixed variable can be obtained. // there is only one uncertain variable free_index, so we can solve the variable and the variable is determined. temp = a [I] [var]; for (j = 0; j <var; j ++) {if (a [I] [j]! = 0 & j! = Free_index) temp-= a [I] [j] * x [j];} x [free_index] = temp/a [I] [free_index]; // obtain the variable. free_x [free_index] = 0; // The variable is determined .} return var-k; // The free variable has var-k .} // 3. unique Solution: a strict upper triangle array is formed in the augmented array of var * (var + 1. // calculate the Xn-1, Xn-2... x0.for (I = var-1; I> = 0; I --) {temp = a [I] [var]; for (j = I + 1; j <var; j ++) {if (a [I] [j]! = 0) temp-= a [I] [j] * x [j];} if (temp % a [I] [I]! = 0) return-2; // indicates that there is a floating point solution, but no integer solution. x [I] = temp/a [I] [I];} return 0;} int main (void) {freopen ("Input.txt", "r", stdin ); int I, j; while (scanf ("% d", & equ, & var )! = EOF) {memset (a, 0, sizeof (a); memset (x, 0, sizeof (x); memset (free_x, 1, sizeof (free_x )); // At the beginning, there were all uncertain yuan changes. for (I = 0; I <equ; I ++) {for (j = 0; j <var + 1; j ++) {scanf ("% d ", & a [I] [j]) ;}// Debug (); free_num = Gauss (); if (free_num =-1) printf ("no solution! \ N "); else if (free_num =-2) printf (" floating point solution, no integer solution! \ N "); else if (free_num> 0) {printf (" infinite solution! Free variable number: % d \ n ", free_num); for (I = 0; I <var; I ++) {if (free_x [I]) printf ("x % d is uncertain \ n", I + 1); else printf ("x % d: % d \ n", I + 1, x [I]) ;}} else {for (I = 0; I <var; I ++) {printf ("x % d: % d \ n ", I + 1, x [I]) ;}} printf ("\ n") ;}return 0 ;}