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

#include <bits/stdc++.h>
using namespace std;

#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (auto i = 0 * (n); i < (n); ++i)
#define FOR(i, s, n) for (size_t i = (s); i < (n); ++i)

typedef bitset<26 * 25> Vector;
typedef vector<Vector> Matrix;
struct GaussJordanBit {
  typedef size_t T;
  GaussJordanBit(Matrix mat, T col) : x(move(mat)) {
    T row = x.size(), r = 0;
    REP(c, col) {
      T p = r;
      FOR(i, r, row) if (x[i][c]) p = i;
      swap(x[r], x[p]);
      if (x[r][c]) {
        if (c == col - 1)
          possible = 0; // ??????????????????????????????????????????
        REP(i, row) if (r != i && x[i][c]) x[i] ^= x[r];
        if (++r == row)
          break;
      }
    }
    rank = r;
  }
  Matrix x;
  T rank;
  bool possible = 1; // ?????¢????¬?????¨????????§£??????????????´???????????????????§£?????????
                     // <-> true
};

bool ba[30][30];

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios::sync_with_stdio(0);
  int m, n, d;
  while (cin >> m >> n >> d, m || n || d) {
    Matrix A(n * m);
    REP(i, n) {
      REP(j, m) {
        cin >> ba[i][j];
        A[i * m + j][i * m + j] = 1;
        REP(k, n) {
          REP(l, m) {
            int dis = abs(i - k) + abs(j - l);
            if (dis == d) {
              A[k * m + l][i * m + j] = 1;
            }
          }
        }
        A[i * m + j][n * m] = ba[i][j];
      }
    }
    cout << (GaussJordanBit(move(A), n * m + 1).possible ? 1 : 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ALL(c) (c).begin(), (c).end()

typedef int64_t ll;
ll inverse(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    swap(a -= t * b, b);
    swap(u -= t * v, v);
  }
  return (u % m + m) % m;
}
#define REP(i, n) for (auto i = 0 * (n); i < (n); ++i)
#define FOR(i, s, n) for (size_t i = (s); i < (n); ++i)

typedef vector<ll> Vector;
typedef vector<Vector> Matrix;
struct GaussJordan {
  typedef size_t T;
  GaussJordan(Matrix mat, T m) : x(move(mat)) {
    T row = x.size(), col = x[0].size(), r = 0;
    REP(c, col) {
      T p = r;
      FOR(i, r, row) if (abs(x[i][c]) > abs(x[p][c])) p = i;
      swap(x[r], x[p]);
      if (x[r][c]) {
        if (c == col - 1)
          impossible = 1;        // ??????????????????????????????????????????
        det = det * x[r][c] % m; // ??????????????????????????????????????????
        // FOR(j, c + 1, col) x[r][j] = x[r][j] * inverse(x[r][c], m) % m;
        FOR(j, c + 1, col) x[r][j] = x[r][j] * x[r][c];
        x[r][c] = 1;
        REP(i, row) if (r != i) {
          FOR(j, c + 1, col)
          x[i][j] = (x[i][j] - x[i][c] * x[r][j] % m + m) % m;
          // FOR(j, c + 1, col) x[i][j] ^= x[i][c] * x[r][j];
          x[i][c] = 0;
        }
        if (++r == row)
          break;
      }
    }
    rank = r;
  }
  Matrix x;
  T rank;
  ll det = 1; // mat ?????£??????????????? full rank ?????¨???????????????
  bool impossible = 0; // ?????¢????¬?????¨????????§£??????????????´???????????????????§£?????????
                       // <-> true
};

GaussJordan linear_equations(Matrix A, const Vector &b, size_t m) {
  REP(i, A.size()) A[i].push_back(b[i]);
  return GaussJordan(move(A), m);
}

bool ba[30][30];

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0);
  ios::sync_with_stdio(0);
  int m, n, d;
  while (cin >> m >> n >> d, m || n || d) {
    Matrix A(n * m, Vector(n * m));
    Vector b(n * m);
    REP(i, n) {
      REP(j, m) {
        cin >> ba[i][j];
        A[i * m + j][i * m + j] = 1;
        REP(k, n) {
          REP(l, m) {
            int dis = abs(i - k) + abs(j - l);
            if (dis == d) {
              A[k * m + l][i * m + j] = 1;
            }
          }
        }
        b[i * m + j] = ba[i][j];
      }
    }
    cout << (linear_equations(move(A), b, 2).impossible ? 0 : 1) << endl;
  }
}