Problem p02963 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) begin(x), end(x)
#define dump(x) cerr << #x " = " << x << endl
using ll = long long;
using namespace std;
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> inline void chmax(T &a, U const &b) {
  a = max<T>(a, b);
}
template <class T, class U> inline void chmin(T &a, U const &b) {
  a = min<T>(a, b);
}
template <typename X, typename T> auto make_vectors(X x, T a) {
  return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto make_vectors(X x, Y y, Z z, Zs... zs) {
  auto cont = make_vectors(y, z, zs...);
  return vector<decltype(cont)>(x, cont);
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &xs) {
  REP(i, (int)xs.size() - 1) out << xs[i] << ' ';
  if (not xs.empty())
    out << xs.back();
  return out;
}

/**
 * @note O(\sqrt{n})
 * @note about 1.0 sec for 10^5 queries with n < 10^10
 */
struct prepared_primes {
  int size;
  vector<int> sieve;
  vector<int> primes;

  prepared_primes(int size_) : size(size_) {

    sieve.resize(size);
    REP3(p, 2, size) if (sieve[p] == 0) {
      primes.push_back(p);
      for (int k = p; k < size; k += p) {
        if (sieve[k] == 0) {
          sieve[k] = p;
        }
      }
    }
  }

  vector<ll> prime_factorize(ll n) {
    // assert (1 <= n and n < (ll)size * size);
    vector<ll> result;

    // trial division for large part
    for (int p : primes) {
      if (n < size or n < (ll)p * p) {
        break;
      }
      while (n % p == 0) {
        n /= p;
        result.push_back(p);
      }
    }

    // small part
    if (n == 1) {
      // nop
    } else if (n < size) {
      while (n != 1) {
        result.push_back(sieve[n]);
        n /= sieve[n];
      }
    } else {
      result.push_back(n);
    }

    assert(is_sorted(ALL(result)));
    return result;
  }

  bool is_prime(ll n) { return prime_factorize(n).size() == 1; }

  vector<ll> list_all_factors(ll n) {
    auto p = prime_factorize(n);
    vector<ll> d;
    d.push_back(1);
    for (int l = 0; l < p.size();) {
      int r = l + 1;
      while (r < p.size() and p[r] == p[l])
        ++r;
      int n = d.size();
      REP(k1, r - l) {
        REP(k2, n) { d.push_back(d[d.size() - n] * p[l]); }
      }
      l = r;
    }
    return d;
  }
};

vector<ll> solve(ll s) {
  constexpr ll LIMIT = 1000000000;
  prepared_primes primes(1000000);
  REP3(h1, max<ll>(1, sqrt(s) - 1000), LIMIT + 1) {
    REP3(w2, max<ll>(0, s / h1 - 100), LIMIT + 1) {
      ll t = s - (ll)h1 * w2;
      if (t < 0)
        break;
      // cerr << h1 << ' ' << w2 << ' ' << t << endl;

      for (ll h2 : primes.list_all_factors(t)) {
        ll w1 = t / h2;
        if (h2 <= LIMIT and w1 + w2 <= LIMIT) {
          ll x1 = 0;
          ll y1 = h1;
          ll x2 = w1;
          ll y2 = 0;
          ll x3 = w1 + w2;
          ll y3 = h2;
          assert(0 <= x1 and x1 <= LIMIT);
          assert(0 <= y1 and y1 <= LIMIT);
          assert(0 <= x2 and x2 <= LIMIT);
          assert(0 <= y2 and y2 <= LIMIT);
          assert(0 <= x3 and x3 <= LIMIT);
          assert(0 <= y3 and y3 <= LIMIT);
          assert(s == h1 * w2 + h2 * w1);
          return vector<ll>({x1, y1, x2, y2, x3, y3});
        }
      }
    }
  }
  assert(false);
}

int main() {
  ll s;
  cin >> s;
  cout << solve(s) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) begin(x), end(x)
#define dump(x) cerr << #x " = " << x << endl
using ll = long long;
using namespace std;
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> inline void chmax(T &a, U const &b) {
  a = max<T>(a, b);
}
template <class T, class U> inline void chmin(T &a, U const &b) {
  a = min<T>(a, b);
}
template <typename X, typename T> auto make_vectors(X x, T a) {
  return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto make_vectors(X x, Y y, Z z, Zs... zs) {
  auto cont = make_vectors(y, z, zs...);
  return vector<decltype(cont)>(x, cont);
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &xs) {
  REP(i, (int)xs.size() - 1) out << xs[i] << ' ';
  if (not xs.empty())
    out << xs.back();
  return out;
}

/**
 * @note O(\sqrt{n})
 * @note about 1.0 sec for 10^5 queries with n < 10^10
 */
struct prepared_primes {
  int size;
  vector<int> sieve;
  vector<int> primes;

  prepared_primes(int size_) : size(size_) {

    sieve.resize(size);
    REP3(p, 2, size) if (sieve[p] == 0) {
      primes.push_back(p);
      for (int k = p; k < size; k += p) {
        if (sieve[k] == 0) {
          sieve[k] = p;
        }
      }
    }
  }

  vector<ll> prime_factorize(ll n) {
    // assert (1 <= n and n < (ll)size * size);
    vector<ll> result;

    // trial division for large part
    for (int p : primes) {
      if (n < size or n < (ll)p * p) {
        break;
      }
      while (n % p == 0) {
        n /= p;
        result.push_back(p);
      }
    }

    // small part
    if (n == 1) {
      // nop
    } else if (n < size) {
      while (n != 1) {
        result.push_back(sieve[n]);
        n /= sieve[n];
      }
    } else {
      result.push_back(n);
    }

    assert(is_sorted(ALL(result)));
    return result;
  }

  bool is_prime(ll n) { return prime_factorize(n).size() == 1; }

  vector<ll> list_all_factors(ll n) {
    auto p = prime_factorize(n);
    vector<ll> d;
    d.push_back(1);
    for (int l = 0; l < p.size();) {
      int r = l + 1;
      while (r < p.size() and p[r] == p[l])
        ++r;
      int n = d.size();
      REP(k1, r - l) {
        REP(k2, n) { d.push_back(d[d.size() - n] * p[l]); }
      }
      l = r;
    }
    return d;
  }
};

vector<ll> solve(ll s) {
  constexpr ll LIMIT = 1000000000;
  prepared_primes primes(1000000);
  REP3(h1, max<ll>(1, sqrt(s) - 1000), LIMIT + 1) {
    REP3(w2, max<ll>(0, s / h1 - 100), LIMIT + 1) {
      ll t = s - (ll)h1 * w2;
      if (t < 0)
        break;
      // cerr << h1 << ' ' << w2 << ' ' << t << endl;

      for (ll h2 : primes.list_all_factors(t)) {
        ll w1 = t / h2;
        if (h2 <= LIMIT and w1 + w2 <= LIMIT) {
          ll x1 = 0;
          ll y1 = h1;
          ll x2 = w1;
          ll y2 = 0;
          ll x3 = w1 + w2;
          ll y3 = h2;
          assert(0 <= x1 and x1 <= LIMIT);
          assert(0 <= y1 and y1 <= LIMIT);
          assert(0 <= x2 and x2 <= LIMIT);
          assert(0 <= y2 and y2 <= LIMIT);
          assert(0 <= x3 and x3 <= LIMIT);
          assert(0 <= y3 and y3 <= LIMIT);
          assert(s == (ll)h1 * w2 + (ll)h2 * w1);
          return vector<ll>({x1, y1, x2, y2, x3, y3});
        }
      }
    }
  }
  assert(false);
}

int main() {
  ll s;
  cin >> s;
  cout << solve(s) << endl;
  return 0;
}