Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 320;
int n, D1, D2;
int t1[N][N], t2[N][N];
void Solve(int D, int t[N][N]) {
  int i, j, k, K, len, x, y;
  K = 0;
  while (D % 2 == 0)
    K++, D >>= 1;
  if (K & 1) {
    len = 1 << ((K + 1) >> 1);
    for (i = 0; i < 2 * n; i += len) {
      for (j = 0; j < (len >> 1) && i + j < 2 * n; j++) {
        for (k = 0; k < n * 2; k++) {
          t[i + j][k] = 1;
        }
      }
    }
  } else {
    len = (1 << ((K + 2) >> 1));
    for (i = 0; i < 2 * n; i++) {
      for (j = 0; j < 2 * n; j++) {
        x = ((i % len) >= (len >> 1));
        y = ((j % len) >= (len >> 1));
        if ((x + y) & 1)
          t[i][j] = 1;
      }
    }
  }
}
int main() {
  int a, b, i, j, num;
  scanf("%d%d%d", &n, &D1, &D2);
  Solve(D1, t1);
  Solve(D2, t2);
  /*
  for(i=0;i<2*n;i++)
  {
          for(j=0;j<2*n;j++)
          {
                  printf("%d ",t2[i][j]);
          }
          putchar('\n');
  }
  return 0;
  */
  for (a = 0; a < 2; a++) {
    for (b = 0; b < 2; b++) {
      num = 0;
      for (i = 0; i < 2 * n; i++) {
        for (j = 0; j < 2 * n; j++) {
          if (t1[i][j] == a && t2[i][j] == b)
            num++;
        }
      }
      if (num >= n * n) {
        num = 0;
        for (i = 0; i < 2 * n; i++) {
          for (j = 0; j < 2 * n; j++) {
            if (t1[i][j] == a && t2[i][j] == b) {
              num++;
              if (num <= n * n)
                printf("%d %d\n", i, j);
            }
          }
        }
        return 0;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 620;
int n, D1, D2;
int t1[N][N], t2[N][N];
void Solve(int D, int t[N][N]) {
  int i, j, k, K, len, x, y;
  K = 0;
  while (D % 2 == 0)
    K++, D >>= 1;
  if (K & 1) {
    len = 1 << ((K + 1) >> 1);
    for (i = 0; i < 2 * n; i += len) {
      for (j = 0; j < (len >> 1) && i + j < 2 * n; j++) {
        for (k = 0; k < n * 2; k++) {
          t[i + j][k] = 1;
        }
      }
    }
  } else {
    len = (1 << ((K + 2) >> 1));
    for (i = 0; i < 2 * n; i++) {
      for (j = 0; j < 2 * n; j++) {
        x = ((i % len) >= (len >> 1));
        y = ((j % len) >= (len >> 1));
        if ((x + y) & 1)
          t[i][j] = 1;
      }
    }
  }
}
int main() {
  int a, b, i, j, num;
  scanf("%d%d%d", &n, &D1, &D2);
  Solve(D1, t1);
  Solve(D2, t2);
  /*
  for(i=0;i<2*n;i++)
  {
          for(j=0;j<2*n;j++)
          {
                  printf("%d ",t2[i][j]);
          }
          putchar('\n');
  }
  return 0;
  */
  for (a = 0; a < 2; a++) {
    for (b = 0; b < 2; b++) {
      num = 0;
      for (i = 0; i < 2 * n; i++) {
        for (j = 0; j < 2 * n; j++) {
          if (t1[i][j] == a && t2[i][j] == b)
            num++;
        }
      }
      if (num >= n * n) {
        num = 0;
        for (i = 0; i < 2 * n; i++) {
          for (j = 0; j < 2 * n; j++) {
            if (t1[i][j] == a && t2[i][j] == b) {
              num++;
              if (num <= n * n)
                printf("%d %d\n", i, j);
            }
          }
        }
        return 0;
      }
    }
  }
  return 0;
}