Problem p03065 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int MAX = 10000;
vector<ll> prime;
bool isprime[MAX];
void sieve() {
  for (ll i = 3; i < MAX; i += 2) {
    isprime[i] = 1;
  }
  isprime[2] = 1;
  prime.push_back(2);
  for (ll i = 3; i < MAX; i++) {
    if (isprime[i]) {
      prime.push_back(i);
      for (ll j = 2 * i; j < MAX; j += i) {
        isprime[j] = 0;
      }
    }
  }
  return;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void div(vector<ll> &v, ll p) {
  while (!v.empty() && v.back() == 0)
    v.pop_back();
  if (v.size() <= p - 1)
    return;
  int n = v.size();
  ll a = v.back();
  v.pop_back();
  v[n - p] = (v[n - p] + a) % p;
  div(v, p);
}
int main() {
  int n;
  cin >> n;
  sieve();
  ll a[10001];
  ll g = 0;
  for (int i = n; i >= 0; i--) {
    cin >> a[i];
    g = gcd(abs(a[i]), g);
  }
  for (int i = 0; i <= n; i++)
    a[i] /= g;
  vector<ll> ans;
  for (ll i = 2; i * i <= g; i++) {
    if (g % i == 0) {
      while (g % i == 0)
        g /= i;
      ans.push_back(i);
    }
  }
  if (g > 1)
    ans.push_back(g);
  for (int i = 0; prime[i] <= n; i++) {
    ll p = prime[i];
    if (a[0] % p != 0)
      continue;
    vector<ll> v;
    for (int i = 0; i <= n; i++) {
      v.push_back((a[i] % p + p) % p);
    }
    div(v, p);
    if (v.empty())
      ans.push_back(p);
  }
  if (ans.empty()) {
    cout << endl;
    return 0;
  }
  sort(ans.begin(), ans.end());
  ans.erase(unique(ans.begin(), ans.end()), ans.end());
  for (auto p : ans)
    cout << p << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int MAX = 11000;
vector<ll> prime;
bool isprime[MAX];
void sieve() {
  for (ll i = 3; i < MAX; i += 2) {
    isprime[i] = 1;
  }
  isprime[2] = 1;
  prime.push_back(2);
  for (ll i = 3; i < MAX; i++) {
    if (isprime[i]) {
      prime.push_back(i);
      for (ll j = 2 * i; j < MAX; j += i) {
        isprime[j] = 0;
      }
    }
  }
  return;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void div(vector<ll> &v, ll p) {
  while (!v.empty() && v.back() == 0)
    v.pop_back();
  if (v.size() <= p - 1)
    return;
  int n = v.size();
  ll a = v.back();
  v.pop_back();
  v[n - p] = (v[n - p] + a) % p;
  div(v, p);
}
int main() {
  int n;
  cin >> n;
  sieve();
  ll a[10001];
  ll g = 0;
  for (int i = n; i >= 0; i--) {
    cin >> a[i];
    g = gcd(abs(a[i]), g);
  }
  for (int i = 0; i <= n; i++)
    a[i] /= g;
  vector<ll> ans;
  for (ll i = 2; i * i <= g; i++) {
    if (g % i == 0) {
      while (g % i == 0)
        g /= i;
      ans.push_back(i);
    }
  }
  if (g > 1)
    ans.push_back(g);
  for (int i = 0; prime[i] <= n; i++) {
    ll p = prime[i];
    if (a[0] % p != 0)
      continue;
    vector<ll> v;
    for (int i = 0; i <= n; i++) {
      v.push_back((a[i] % p + p) % p);
    }
    div(v, p);
    if (v.empty())
      ans.push_back(p);
  }
  if (ans.empty()) {
    cout << endl;
    return 0;
  }
  sort(ans.begin(), ans.end());
  ans.erase(unique(ans.begin(), ans.end()), ans.end());
  for (auto p : ans)
    cout << p << endl;
  return 0;
}