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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
typedef long long ll;

int h, w, k;
ll field[1001][1001];
ll loop[1001][1001];

typedef pair<ll, ll> P;

const ll dy[] = {0, -1, 1, 0};
const ll dx[] = {-1, 0, 0, 1};

void dp() {
  memset(loop, 0, sizeof(loop));
  loop[0][0] = k - 1;
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      ll prvy = i - 1;
      ll prvx = j;
      if (prvy >= 0 && prvx >= 0 && prvy < h && prvx < w &&
          loop[prvy][prvx] != 0) {
        ll p = field[prvy][prvx];
        if (loop[prvy][prvx] % 2 == 0)
          p = 1;
        loop[i][j] += loop[prvy][prvx] / 2 + !p;
      }
      prvy = i;
      prvx = j - 1;
      if (prvy >= 0 && prvx >= 0 && prvy < h && prvx < w &&
          loop[prvy][prvx] != 0) {
        ll p = field[prvy][prvx];
        if (loop[prvy][prvx] % 2 == 0)
          p = 0;
        loop[i][j] += loop[prvy][prvx] / 2 + p;
      }
    }
  }
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {

  while (cin >> h >> w >> k && !(h == 0 && w == 0 && k == 0)) {
    for (ll i = 0; i < h; i++) {
      for (ll j = 0; j < w; j++) {
        ll t;
        cin >> field[i][j];
      }
    }
    dp();

    for (ll i = 0; i < 1; i++) {
      ll cx, cy;
      cx = cy = 0;
      while (1) {
        if (cx >= w || cy >= h)
          break;
        if (loop[cy][cx] % 2 == 0) {
          if (field[cy][cx]) {
            field[cy][cx] = 0;
            cx++;
          } else {
            field[cy][cx] = 1;
            cy++;
          }
        } else {
          if (!field[cy][cx]) {
            field[cy][cx] = 0;
            cx++;
          } else {
            field[cy][cx] = 1;
            cy++;
          }
        }
      }
      cout << cy + 1 << " " << cx + 1 << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int h, w, k;
int field[1001][1001];
int loop[1001][1001];

typedef pair<int, int> P;

const int dy[] = {0, -1, 1, 0};
const int dx[] = {-1, 0, 0, 1};

typedef long long ll;

void dp() {
  memset(loop, 0, sizeof(loop));
  loop[0][0] = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int prvy = i - 1;
      int prvx = j;
      if (prvy >= 0 && prvx >= 0 && prvy < h && prvx < w) {
        loop[i][j] += loop[prvy][prvx];
      }
      prvy = i;
      prvx = j - 1;
      if (prvy >= 0 && prvx >= 0 && prvy < h && prvx < w) {
        loop[i][j] += loop[prvy][prvx];
      }
    }
  }
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {

  while (cin >> h >> w >> k && !(h == 0 && w == 0 && k == 0)) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int t;
        cin >> field[i][j];
      }
    }
    dp();
    // ½ñÀsµ½çSÌÆµÄ[v·é©
    ll ln = 1;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        ln = lcm(ln, loop[i][j]);
      }
    }
    ll num = k % ln;
    for (int i = 0; i < num; i++) {
      int cx, cy;
      cx = cy = 0;
      while (1) {
        if (cx >= w || cy >= h)
          break;
        if (field[cy][cx]) {
          field[cy][cx] = 0;
          cx++;
        } else {
          field[cy][cx] = 1;
          cy++;
        }
      }
      if (i == k - 1) {
        cout << cy + 1 << " " << cx + 1 << endl;
      }
    }
  }

  return 0;
}