Problem p02825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fst first
#define sec second
#define MAX_N (1 << 20)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rer(i, a, b) for (int i = a - 1; i >= b; i--)
#define sz(a) int(a.size())
using namespace std;

typedef vector<int> vi;
typedef long long int ll;
const ll linf = (1ll << 60);

int N;
char ans[1010][1010];

string three[3] = {"aac", "b.c", "bdd"};
string four[4] = {"aacd", "bbcd", "efgg", "efhh"};
string five[5] = {"abbcc", "add.e", "i..je", "i..jf", "hhggf"};
string seven[7] = {
    "aabbcc.", "ggh...d", "j.h...d", "jii...e", "...kkle", "...n.lf", "...nmmf",
};

int main() {
  cin >> N;
  rep(i, 0, N) {
    rep(j, 0, N) { ans[i][j] = '.'; }
  }
  if (N == 2)
    cout << -1 << "\n";
  else {
    if (N == 7 || N == 11) {
      ans[0][0] = 'z';
      ans[0][1] = 'z';
      ans[0][2] = 'y';
      ans[0][3] = 'y';
      ans[0][4] = 'x';
      ans[0][5] = 'x';
      ans[1][6] = 'z';
      ans[2][6] = 'z';
      ans[3][6] = 'y';
      ans[4][6] = 'y';
      ans[5][6] = 'x';
      ans[6][6] = 'x';
      rep(i, 0, 3) {
        rep(j, 0, 3) { ans[i + 1][j] = three[i][j]; }
      }
      rep(i, 0, 3) {
        rep(j, 0, 3) { ans[i + 4][j + 3] = three[i][j]; }
      }
      if (N == 11) {
        rep(i, 0, 4) {
          rep(j, 0, 4) { ans[i + 7][j + 7] = four[i][j]; }
        }
      }
    } else if (N % 3 == 0) {
      rep(k, 0, N / 3) {
        rep(i, 0, 3) {
          rep(j, 0, 3) { ans[k * 3 + i][k * 3 + j] = three[i][j]; }
        }
      }
    } else {
      int a = -1, b = -1;
      for (int i = 0; i <= N; i++) {
        if (N - 4 * i >= 0 && (N - 4 * i) % 5 == 0) {
          a = i;
          b = (N - 3 * i) / 5;
        }
      }
      if (a != -1) {
        int at = 0;
        while (a--) {
          rep(i, 0, 4) {
            rep(j, 0, 4) { ans[at + i][at + j] = four[i][j]; }
          }
          at += 4;
        }
        while (b--) {
          rep(i, 0, 5) {
            rep(j, 0, 5) { ans[at + i][at + j] = five[i][j]; }
          }
          at += 5;
        }
      }
    }
    rep(i, 0, N) {
      rep(j, 0, N) { cout << ans[i][j]; }
      cout << "\n";
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define fst first
#define sec second
#define MAX_N (1 << 20)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rer(i, a, b) for (int i = a - 1; i >= b; i--)
#define sz(a) int(a.size())
using namespace std;

typedef vector<int> vi;
typedef long long int ll;
const ll linf = (1ll << 60);

int N;
char ans[1010][1010];

string three[3] = {"aac", "b.c", "bdd"};
string four[4] = {"aacd", "bbcd", "efgg", "efhh"};
string five[5] = {"abbcc", "add.e", "i..je", "i..jf", "hhggf"};
string seven[7] = {
    "aabbcc.", "ggh...d", "j.h...d", "jii...e", "...kkle", "...n.lf", "...nmmf",
};

int main() {
  cin >> N;
  rep(i, 0, N) {
    rep(j, 0, N) { ans[i][j] = '.'; }
  }
  if (N == 2)
    cout << -1 << "\n";
  else {
    if (N == 7 || N == 11) {
      ans[0][0] = 'z';
      ans[0][1] = 'z';
      ans[0][2] = 'y';
      ans[0][3] = 'y';
      ans[0][4] = 'x';
      ans[0][5] = 'x';
      ans[1][6] = 'z';
      ans[2][6] = 'z';
      ans[3][6] = 'y';
      ans[4][6] = 'y';
      ans[5][6] = 'x';
      ans[6][6] = 'x';
      rep(i, 0, 3) {
        rep(j, 0, 3) { ans[i + 1][j] = three[i][j]; }
      }
      rep(i, 0, 3) {
        rep(j, 0, 3) { ans[i + 4][j + 3] = three[i][j]; }
      }
      if (N == 11) {
        rep(i, 0, 4) {
          rep(j, 0, 4) { ans[i + 7][j + 7] = four[i][j]; }
        }
      }
    } else if (N % 3 == 0) {
      rep(k, 0, N / 3) {
        rep(i, 0, 3) {
          rep(j, 0, 3) { ans[k * 3 + i][k * 3 + j] = three[i][j]; }
        }
      }
    } else {
      int a = -1, b = -1;
      for (int i = 0; i <= N; i++) {
        if (N - 4 * i >= 0 && (N - 4 * i) % 5 == 0) {
          a = i;
          b = (N - 4 * i) / 5;
        }
      }
      if (a != -1) {
        int at = 0;
        while (a--) {
          rep(i, 0, 4) {
            rep(j, 0, 4) { ans[at + i][at + j] = four[i][j]; }
          }
          at += 4;
        }
        while (b--) {
          rep(i, 0, 5) {
            rep(j, 0, 5) { ans[at + i][at + j] = five[i][j]; }
          }
          at += 5;
        }
      }
    }
    rep(i, 0, N) {
      rep(j, 0, N) { cout << ans[i][j]; }
      cout << "\n";
    }
  }
}