Problem p03257 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
vector<ll> primes;
ll factor[505];
ll field[505][505];
set<ll> used;

int main() {
  // cout.precision(10);
  int N;
  cin >> N;
  for (ll i = 2; i <= 20000; i++) {
    bool ok = true;
    for (ll j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      primes.push_back(i);
  }
  int index = 1;
  for (int i = 2; i <= 2 * N; i += 2) {
    factor[i] = primes[index];
    index++;
  }
  for (int i = 1; i <= N + 1; i += 2) {
    field[i][1] = primes[index];
    index++;
    ll now1 = field[i][1];
    field[1][i] = primes[index];
    index++;
    ll now2 = field[1][i];
    // cerr << i << " " << index << endl;
    for (int j = 1; j + i - 1 <= N + 1; j++) {
      field[j][i + j - 1] = now1 * factor[i + 2 * j - 1];
      field[i + j - 1][j] = now2 * factor[i + 2 * j - 1];
      // cerr << i << " " << j << endl;
    }
  }
  vector<ll> val;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (field[i][j] == 0) {
        if (i > 1)
          field[i][j] = field[i + 1][j] * field[i - 1][j] + 1;
        else
          field[i][j] = field[i][j + 1] * field[i][j - 1] + 1;
      }
      cout << field[i][j] << " ";
      // if(field[i][j] > 1e15) val.push_back(field[i][j]);
      // if(used.find(field[i][j]) != used.end()) val.push_back(field[i][j]);
      used.insert(field[i][j]);
      if (i > 1) {
        if (max(field[i - 1][j], field[i][j]) %
                min(field[i][j], field[i - 1][j]) !=
            1)
          val.push_back(field[i][j]);
        // cerr << i << " " << j << " " << field[i][j] << " "<< field[i-1][j] <<
        // endl;
      }
    }
    cout << endl;
  }
  // cout << "out" << endl;
  // for(int i = 0; i < val.size(); i++) cout << val[i] << endl;
  // cout << primes.size() << " " << primes[2] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
vector<ll> primes;
ll factor[1005];
ll field[505][505];
set<ll> used;

int main() {
  // cout.precision(10);
  int N;
  cin >> N;
  for (ll i = 2; i <= 20000; i++) {
    bool ok = true;
    for (ll j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      primes.push_back(i);
  }
  int index = 1;
  for (int i = 2; i <= 2 * N; i += 2) {
    factor[i] = primes[index];
    index++;
  }
  for (int i = 1; i <= N + 1; i += 2) {
    field[i][1] = primes[index];
    index++;
    ll now1 = field[i][1];
    field[1][i] = primes[index];
    index++;
    ll now2 = field[1][i];
    // cerr << i << " " << index << endl;
    for (int j = 1; j + i - 1 <= N + 1; j++) {
      field[j][i + j - 1] = now1 * factor[i + 2 * j - 1];
      field[i + j - 1][j] = now2 * factor[i + 2 * j - 1];
      // cerr << i << " " << j << endl;
    }
  }
  vector<ll> val;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (field[i][j] == 0) {
        if (i > 1)
          field[i][j] = field[i + 1][j] * field[i - 1][j] + 1;
        else
          field[i][j] = field[i][j + 1] * field[i][j - 1] + 1;
      }
      cout << field[i][j] << " ";
      // if(field[i][j] > 1e15) val.push_back(field[i][j]);
      // if(used.find(field[i][j]) != used.end()) val.push_back(field[i][j]);
      used.insert(field[i][j]);
      if (i > 1) {
        if (max(field[i - 1][j], field[i][j]) %
                min(field[i][j], field[i - 1][j]) !=
            1)
          val.push_back(field[i][j]);
        // cerr << i << " " << j << " " << field[i][j] << " "<< field[i-1][j] <<
        // endl;
      }
    }
    cout << endl;
  }
  // cout << "out" << endl;
  // for(int i = 0; i < val.size(); i++) cout << val[i] << endl;
  // cout << primes.size() << " " << primes[2] << endl;
  return 0;
}