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

#include <iostream>
#include <set>
#include <vector>
using namespace std;

vector<int> pl;
int primes[1000005] = {};
typedef long long ll;

void init() {
  primes[0] = primes[1] = 1;
  for (int i = 4; i < 1000005; i += 2)
    primes[i] = 1;
  for (int i = 3; i * i < 1000005; i += 2)
    if (primes[i] == 0)
      for (int j = 2; i * j < 1000005; ++j)
        primes[i * j] = 1;

  pl.push_back(2);
  for (int i = 3; i < 1000005; i += 2)
    if (primes[i] == 0)
      pl.push_back(i);
  return;
}

void factorize(vector<ll> &p, vector<ll> &cnt, ll x) {
  p.clear();
  cnt.clear();

  ll pp = pl[0], pi = 0;
  while (true) {
    pp = pl[pi];
    if (x <= 1)
      break;
    if (x % pp == 0) {
      p.push_back(pp);
      cnt.push_back(0);
      while (x % pp == 0) {
        cnt[cnt.size() - 1]++;
        x /= pp;
      }
    }
    pi++;
    if (pi == pl.size())
      break;
  }
  if (x > 1) {
    p.push_back(x);
    cnt.push_back(1);
  }
  return;
}

int main() {
  pl.clear();
  init();
  ll l;
  while (cin >> l, l) {
    vector<ll> fact, cnt;
    factorize(fact, cnt, l);

    ll ans = 1;

    for (int i = 0; i < cnt.size(); ++i) {
      ans *= (cnt[i] + 1) * 2 - 1;
    }
    cout << (ans + 1) / 2 << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <set>
#include <vector>
using namespace std;

vector<int> pl;
int primes[100010] = {};
typedef long long ll;

void init() {
  primes[0] = primes[1] = 1;
  for (int i = 4; i < 100010; i += 2)
    primes[i] = 1;
  for (int i = 3; i * i < 100010; i += 2)
    if (primes[i] == 0)
      for (int j = 2; i * j < 100010; ++j)
        primes[i * j] = 1;

  pl.push_back(2);
  for (int i = 3; i < 100010; i += 2)
    if (primes[i] == 0)
      pl.push_back(i);
  return;
}

void factorize(vector<ll> &p, vector<ll> &cnt, ll x) {
  p.clear();
  cnt.clear();
  if (x == 1) {
    p.push_back(1);
    cnt.push_back(1);
    return;
  }

  ll pp = pl[0], pi = 0;
  while (true) {
    pp = pl[pi];
    if (x <= 1)
      break;
    if (x % pp == 0) {
      p.push_back(pp);
      cnt.push_back(0);
      while (x % pp == 0) {
        cnt[cnt.size() - 1]++;
        x /= pp;
      }
    }
    pi++;
    if (pi == pl.size())
      break;
  }
  return;
}

int main() {
  pl.clear();
  init();
  ll l;
  while (cin >> l, l) {
    vector<ll> fact, cnt;
    factorize(fact, cnt, l);
    // cout<<l<<endl;

    // for(int i=0; i<fact.size(); ++i) {
    //     cout<<fact[i]<<" "<<cnt[i]<<endl;
    // }

    // set<ll> factors;
    // factors.insert(1);
    // for(int i=0; i<fact.size(); ++i) {
    //     for(int j=0; j<cnt[i]; ++j) {
    //         vector<ll> tmp;
    //         for(set<ll>::iterator it = factors.begin();
    //             it != factors.end(); ++it) {
    //             tmp.push_back((*it)*fact[i]);
    //         }
    //         for(int k=0; k<tmp.size(); ++k) factors.insert(tmp[k]);
    //     }

    // }

    ll ans = cnt[0] * 2 + 1;
    for (int i = 1; i < cnt.size(); ++i) {
      // cout<<ans<<endl;
      ans *= cnt[i] * 2 + 1;
    }
    if (l == 1)
      ans = 1;
    cout << (ans + 1) / 2 << endl;

    // for(set<ll>::iterator it = factors.begin(); it != factors.end(); ++it)
    //     cout<<(*it)<<endl;
  }
}