Gambler Bo (Gauss elimination element for special solution)

Source: Internet
Author: User
Tags gcd

For each point in the diagram, assume that you click XI * m + j and then each point has so that for each point you can enumerate an equation, n*m a point solution n*m an unknown. It can be solved by using Gauss elimination element.

The problem is that there may be more than one special, so we need to solve the kindergarten solution. And then that one solved I can't read.

#include <bits/stdc++.h>using namespacestd;Const intMAXN = ** *;intN, M, CNT;intid[ *][ *], data[ *][ *], A[MAXN][MAXN], X[MAXN];intgcdintAintb) {    returnB?GCD (b, a%b): A;}intLcmintAintb) {    returnA/GCD (A, b) *b;}voidinit () {memset (x,0,sizeof(x)); Memset (A,0,sizeof(a));  for(inti =1; I <= N; i + +){         for(intj =1; J <= M; J + +) {a[id[i][j]][cnt]= (3-DATA[I][J])%3; A[ID[I][J]][ID[I][J]]=2; if(I >1) A[id[i][j]][id[i-1][J]] =1; if(J >1) A[id[i][j]][id[i][j-1]] =1; if(I < n) a[id[i][j]][id[i +1][J]] =1; if(J < m) A[id[i][j]][id[i][j +1]] =1; }    }}voidGaussi () { for(inti =1; I < CNT; i + +){        inttop =i;  for(intj = i +1; J < CNT; J + +) Top= ABS (A[j][i]) > abs (A[top][i])?J:top; if(A[top][i]) { for(intj = i; J <= CNT; J + +) Swap (A[top][j], a[i][j]);  for(intj = i +1; J < CNT; J + +)            if(A[j][i]) {intD =LCM (A[j][i], a[i][i]); intX1 = D/a[j][i], x2 = d/A[i][i];  for(intK = i; K <= CNT; K + +) A[j][k]= ((a[j][k] * x1-a[i][k] * x2)%3+3) %3; }        }    }    intAns =0;  for(inti = cnt-1; i >0; I--) {X[i]=a[i][cnt];  for(intj = i +1; J < CNT; J + +) X[i]= ((X[i]-a[i][j] * x[j])%3+3)%3; X[i]= a[i][i] * X[i]%3; Ans+=X[i]; } printf ("%d\n", ans);  for(inti =1; I < CNT; i + +){         while(X[i]) {printf ("%d%d\n", (I-1)/M +1, (I-1)%m +1); X[i]--; }    }}intMain () {intT; scanf ("%d",&T);  while(T--) {CNT=1; scanf ("%d%d",&n,&m);  for(inti =1; I <= N; i + +)             for(intj =1; J <= M; J + +) scanf ("%d", &data[i][j]), id[i][j] = cnt++;        Init ();    Gaussi (); }    return 0 ;}

Gambler Bo (Gauss elimination element for special solution)

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.