Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#define MIN_INT -2147483648
#define MAX_INT 2147483647
#define MIN_LONG -9223372036854775808L
#define MAX_LONG 9223372036854775807L
#define PI 3.141592653589793238462643383279502884L

#define long long long int

using std::cerr;
using std::cin;
using std::cout;
using std::map;
using std::pair;
using std::set;
using std::string;
using std::vector;

// @author: pashka

vector<vector<bool>> go(int n, int d) {
  vector<vector<bool>> a(2 * n, vector<bool>(2 * n));
  if (d % 2 == 1) {
    for (int i = 0; i < 2 * n; i++) {
      for (int j = 0; j < 2 * n; j++) {
        if ((i + j) % 2) {
          a[i][j] = true;
        }
      }
    }
  } else {
    if (d % 4 != 0) {
      for (int i = 0; i < 2 * n; i++) {
        for (int j = 0; j < 2 * n; j++) {
          if (i % 2) {
            a[i][j] = true;
          }
        }
      }
    } else {
      auto aa = go(n / 2, d / 4);
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (aa[i][j]) {
            for (int ii = 0; ii < 2; ii++) {
              for (int jj = 0; jj < 2; jj++) {
                a[2 * i + ii][2 * j + jj] = true;
              }
            }
          }
        }
      }
    }
  }
  return a;
}

int main() {
  std::ios::sync_with_stdio(false);

  int n, d1, d2;
  cin >> n >> d1 >> d2;
  auto a1 = go(n, d1);
  auto a2 = go(n, d2);

  int s = n * n;
  for (int i = 0; i < 2 * n; i++) {
    for (int j = 0; j < 2 * n; j++) {
      if (!a1[i][j] && !a2[i][j] && s > 0) {
        s--;
        cout << i << " " << j << "\n";
      }
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#define MIN_INT -2147483648
#define MAX_INT 2147483647
#define MIN_LONG -9223372036854775808L
#define MAX_LONG 9223372036854775807L
#define PI 3.141592653589793238462643383279502884L

#define long long long int

using std::cerr;
using std::cin;
using std::cout;
using std::map;
using std::pair;
using std::set;
using std::string;
using std::vector;

// @author: pashka

vector<vector<bool>> go(int n, int d) {
  vector<vector<bool>> a(2 * n, vector<bool>(2 * n));
  if (d % 2 == 1) {
    for (int i = 0; i < 2 * n; i++) {
      for (int j = 0; j < 2 * n; j++) {
        if ((i + j) % 2) {
          a[i][j] = true;
        }
      }
    }
  } else {
    if (d % 4 != 0) {
      for (int i = 0; i < 2 * n; i++) {
        for (int j = 0; j < 2 * n; j++) {
          if (i % 2) {
            a[i][j] = true;
          }
        }
      }
    } else {
      auto aa = go((n + 1) / 2, d / 4);
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (aa[i][j]) {
            for (int ii = 0; ii < 2; ii++) {
              for (int jj = 0; jj < 2; jj++) {
                a[2 * i + ii][2 * j + jj] = true;
              }
            }
          }
        }
      }
    }
  }
  return a;
}

int main() {
  std::ios::sync_with_stdio(false);

  int n, d1, d2;
  cin >> n >> d1 >> d2;
  auto a1 = go(n, d1);
  auto a2 = go(n, d2);

  int s = n * n;
  for (int i = 0; i < 2 * n; i++) {
    for (int j = 0; j < 2 * n; j++) {
      if (!a1[i][j] && !a2[i][j] && s > 0) {
        s--;
        cout << i << " " << j << "\n";
      }
    }
  }

  return 0;
}