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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

const int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1}, dy2[] = {1, -1, -1, 1},
          dx2[] = {1, 1, -1, -1};
int m, n, d, s[25][25];
int main() {
  while (scanf("%d%d%d", &n, &m, &d), m) {
    int N = m * n;
    vector<vi> a(N, vi(N + 1, 0));
    rep(i, m) rep(j, n) scanf("%d", s[i] + j), a[i * n + j][N] = s[i][j];
    rep(i, N)(a[i][i] += 1) %= 2;
    rep(i, m) rep(j, n) rep(dir, 4) rep(k, d) {
      int y = i + dy[dir] * d + dy2[dir] * k,
          x = j + dx[dir] * d + dx2[dir] * k;
      if (0 <= y && y < m && 0 <= x && x < n) {
        (a[y * n + x][i * n + j] += 1) %= 2;
      }
    }
    int j = 0;
    rep(i, N) {
      int mi = i;
      if (!a[mi][j])
        for (int k = i + 1; k < N; k++)
          if (a[mi] < a[k]) {
            mi = k;
            if (a[mi][j])
              break;
          }
      swap(a[i], a[mi]);
      for (; j < N && a[i][j] == 0; j++)
        ;
      if (j >= N)
        break;
      assert(a[i][j]);
      rep(k, N) if (i < k && a[k][j]) for (int l = j; l <= N;
                                           l++)(a[k][l] += a[i][l]) %= 2;
      j++;
    }
    bool ok = 1;
    j = 0;
    rep(i, N) if (ok && a[i][N]) {
      for (; j < N && a[i][j] == 0; j++)
        ;
      if (j >= N)
        ok = 0;
    }
    printf("%d\\n", (int)ok);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

const int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1}, dy2[] = {1, -1, -1, 1},
          dx2[] = {1, 1, -1, -1};
int m, n, d, s[25][25];
int main() {
  while (cin >> m >> n >> d, m) {
    int N = m * n;
    vector<vi> a(N, vi(N + 1, 0));
    rep(i, m) rep(j, n) cin >> s[i][j], a[i * n + j][N] = s[i][j];

    // rep(i,N)rep(j,N+1)cerr<<a[i][j]<<(j==N?"\\n":" ");

    rep(i, N)(a[i][i] += 1) %= 2;
    rep(i, m) rep(j, n) rep(dir, 4) rep(k, d) {
      int y = i + dy[dir] * d + dy2[dir] * k,
          x = j + dx[dir] * d + dx2[dir] * k;
      if (0 <= y && y < m && 0 <= x && x < n) {
        (a[y * n + x][i * n + j] += 1) %= 2;
      }
    }
    // rep(i,N)rep(j,N+1)cerr<<a[i][j]<<(j==N?"\\n":" ");

    rep(i, N) {
      sort(a.begin() + i, a.end(), greater<vi>());
      if (a[i][i]) {
        rep(j, N) if (i < j && a[j][i]) rep(k, N + 1)(a[j][k] += a[i][k]) %= 2;
      }
    }
    // rep(i,N)rep(j,N+1)cerr<<a[i][j]<<(j==N?"\\n":" ");

    bool ok = 1;
    rep(i, N) if (ok && a[i][N]) {
      bool exist = 0;
      rep(j, N) if (a[i][j]) exist = 1;
      if (!exist)
        ok = 0;
    }
    cout << (int)ok << endl;
  }
  return 0;
}