Problem p00887 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;

#define EPS (1e-9)

// KEXÌÁ@ÅA§®ðÆ­
bool gauss_jordan(const mat &A, const vec &b) {
  int n = A.size();
  mat B(n, vec(n + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j];
  for (int i = 0; i < n; i++)
    B[i][n] = b[i];
  // eÏÉÚ
  for (int i = 0; i < n; i++) {
    // ÅåÌWÌâÎlðàÂàÌðiÔÚÉÁÄ­é
    int piv = -1;
    int val = -1;
    for (int j = i; j < n; j++) {
      if (val < abs(B[j][i])) {
        piv = j;
        val = abs(B[j][i]);
      }
    }
    // EÓª0ÉÈé©Ç¤©²×é
    if (piv == -1 || val <= 0)
      continue;
    swap(B[piv], B[i]);
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      int mulnum = B[j][i];
      if (mulnum == 1) {
        for (int k = i; k <= n; k++)
          B[j][k] = (B[j][k] + B[i][k]) % 2;
      }
    }
  }
  vec res(n);
  for (int i = 0; i < n; i++) {
    res[i] = B[i][n];
    if (B[i][i] == 0) {
      if (B[i][n] == 1)
        return false;
    }
  }
  return true;
}
int M, N, D;
int field[101][101];

int main() {

  while (cin >> M >> N >> D && (M | N | D)) {
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        cin >> field[i][j];
    mat A(M * N, vec(M * N));
    vec B(M * N, 0);
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (field[i][j])
          B[i * M + j] = 1;
        for (int k = 0; k < N; k++) {
          for (int l = 0; l < M; l++) {
            // }nb^£
            int dist = abs(l - j) + abs(i - k);
            if (dist == D || dist == 0)
              A[i * M + j][k * M + l] = 1;
          }
        }
      }
    }
    bool res = gauss_jordan(A, B);
    if (res)
      cout << 1 << endl;
    else
      cout << 0 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;

#define EPS (1e-9)
#define EQ(a, b) (abs((a) - (b)) < EPS)

// KEXÌÁ@ÅA§®ðÆ­
void gauss_jordan(const mat &A, const vec &b, vector<int> &res) {
  int n = A.size();
  mat B(n, vec(n + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j];
  for (int i = 0; i < n; i++)
    B[i][n] = b[i];
  // eÏÉÚ
  for (int i = 0; i < n; i++) {
    // evfð2ÅÁ½]èÉu«·¦é
    //		for(int j=0;j<n;j++){
    //			for(int k=0;k<n;k++){
    //				if(abs(B[j][k])%2==1)B[j][k]=B[j][k]%2;
    //				else if(B[j][k]==0)B[j][k]=0;
    //				else if(B[j][k]>0)B[j][k]=2;
    //				else B[j][k]=-2;
    //			}
    //			B[j][n]=B[j][n]%2;
    //		}
    // ÅåÌWÌâÎlðàÂàÌðiÔÚÉÁÄ­é
    int piv = -1;
    int val = -1;
    for (int j = i; j < n; j++) {
      if (abs(B[j][i]) == 1) {
        piv = j;
        val = abs(B[j][i]);
      }
    }
    // ðª¡Ü½Íðª³¢
    if (piv == -1 || val < EPS) {
      if (B[i][n] != 0) {
        res = vec();
        return;
      } else {
        // ðª¡¶Ý·éÌÅA»Ì¤¿ÌêÂðßé
        B[i][n] = 0;
        continue;
      }
    }
    swap(B[piv], B[i]);
    // isÚð»ê¼êB[piv][i]ÌlÅé
    //		int divNum=B[i][i];
    //		for(int j = i; j <= n; j++)B[i][j]/=divNum;
    // »ê¼êÌs©çAisÚÌlðø­©ñ¶
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      int mulnum = B[j][i];
      for (int k = i; k <= n; k++) {
        B[j][k] -= mulnum * B[i][k];
        if (k != n) {
          if (abs(B[j][k]) % 2 == 1)
            B[j][k] %= 2;
          else if (B[j][k] == 0)
            B[j][k] = 0;
          else if (B[j][k] > 0)
            B[j][k] = 2;
          else
            B[j][k] = -2;
        } else
          B[j][n] %= 2;
      }
    }
  }
  for (int i = 0; i < n; i++)
    res[i] = B[i][n];
}

int main() {
  int m, n, d;
  int field[30][30];
  while (cin >> m >> n >> d && (m | n | d)) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        cin >> field[i][j];
    int mtSize = m * n;
    mat mt(mtSize, vec(mtSize));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        // (m*i+j)sÚÌsñÌeñÉÂ¢Äè
        for (int k = 0; k < n; k++) {
          for (int l = 0; l < m; l++) {
            int mhd = abs(k - i) + abs(l - j);
            mt[m * i + j][m * k + l] = (mhd == d || mhd == 0);
          }
        }
      }
    }
    vector<int> r;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        r.push_back(field[i][j]);
    vec res(mtSize);
    gauss_jordan(mt, r, res);
    if (res.size() == 0)
      cout << 0 << endl;
    else
      cout << 1 << endl;
  }
  return 0;
}