Problem p00646 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (i = 0; i < (n); i++)
#define REPONE(i, n) for (i = 1; i <= (n); i++)
#define LOOP(n) for (int loopCount = 1; loopCount <= (n); loopCount++)
#define ITER(c) __typeof((c).begin())
#define EACH(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define SZ(c) ((int)(c).size())
#define ALL(c) c.begin(), c.end()
#define SUM(c) accumulate(ALL(c), 0)
#define EXIST(c, v) (find(ALL(c), (v)) != (c).end())
#define PB push_back
#define MP make_pair

using namespace std;
static const double EPS = 1e-9;
static const double PI = 3.141592653589793238462643383279;
typedef long long ll;

vector<int> primeTable(int n) {
  vector<int> ps;
  vector<int> table(n + 1, 0);
  for (int i = 2; i <= n; i++) {
    if (!table[i]) {
      ps.push_back(i);
      if (i <= sqrt(n))
        table[i * i] = i;
    } else {
      int j = i, step = table[i];
      while (j + step <= n) {
        if (!table[j + step]) {
          table[j + step] = step;
          break;
        } else {
          int tmp = step;
          step = table[j + step];
          table[j + tmp] = tmp;
          j += tmp;
        }
      }
    }
  }
  return ps;
}

int main() {
  ll n;
  vector<int> ps = primeTable(1100000);
  int i, s = SZ(ps);
  while (cin >> n) {
    if (n == 0)
      break;
    vector<int> es;
    for (i = 0; n != 1 && i < s; i++) {
      if (n % ps[i] == 0) {
        int cnt = 1;
        n /= ps[i];
        while (n % ps[i] == 0) {
          n /= ps[i];
          cnt++;
        }
        es.PB(cnt);
      }
    }
    if (i == s && n != 1)
      es.PB(1);
    ll ans = 1;
    for (i = 0; i < SZ(es); i++) {
      ans *= (2 * es[i] + 1);
    }
    cout << ans / 2 + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (i = 0; i < (n); i++)
#define REPONE(i, n) for (i = 1; i <= (n); i++)
#define LOOP(n) for (int loopCount = 1; loopCount <= (n); loopCount++)
#define ITER(c) __typeof((c).begin())
#define EACH(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define SZ(c) ((int)(c).size())
#define ALL(c) c.begin(), c.end()
#define SUM(c) accumulate(ALL(c), 0)
#define EXIST(c, v) (find(ALL(c), (v)) != (c).end())
#define PB push_back
#define MP make_pair

using namespace std;
static const double EPS = 1e-9;
static const double PI = 3.141592653589793238462643383279;
typedef long long ll;

vector<int> primeTable(int n) {
  vector<int> ps;
  vector<int> table(n + 1, 0);
  for (int i = 2; i <= n; i++) {
    if (!table[i]) {
      ps.push_back(i);
      if (i <= sqrt(n))
        table[i * i] = i;
    } else {
      int j = i, step = table[i];
      while (j + step <= n) {
        if (!table[j + step]) {
          table[j + step] = step;
          break;
        } else {
          int tmp = step;
          step = table[j + step];
          table[j + tmp] = tmp;
          j += tmp;
        }
      }
    }
  }
  return ps;
}

int main() {
  ll n;
  vector<int> ps = primeTable(1100000);
  while (cin >> n) {
    if (n == 0)
      break;
    vector<int> es;
    for (int i = 0; n != 1; i++) {
      if (n % ps[i] == 0) {
        int cnt = 1;
        n /= ps[i];
        while (n % ps[i] == 0) {
          n /= ps[i];
          cnt++;
        }
        es.PB(cnt);
      }
    }
    ll ans = 1;
    for (int i = 0; i < SZ(es); i++) {
      ans *= (2 * es[i] + 1);
    }
    cout << ans / 2 + 1 << endl;
  }
  return 0;
}