Problem p03065 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define lowbit(x) (x) & (-x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define maxd 1000000007
typedef long long ll;
const int N = 100000;
const double pi = acos(-1.0);
int n, tot = 0, pri[1001000], a[10100], tax[10100];
bool ispri[10100];
set<int> ans;

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

void sieve(int n) {
  rep(i, 2, n) {
    if (!ispri[i])
      pri[++tot] = i;
    int j;
    for (j = 1; j <= tot && i * pri[j] <= n; j++) {
      ispri[i * pri[j]] = 1;
      if (i % pri[j] == 0)
        break;
    }
  }
}

int gcd(int x, int y) {
  if (!y)
    return x;
  else
    return gcd(y, x % y);
}

bool chk(int p) {
  if (a[0] % p)
    return 0;
  rep(i, 0, p - 2) tax[i] = 0;
  rep(i, 0, n) tax[i % (p - 1)] = (tax[a[i] % (p - 1)] + a[i]) % p;
  rep(i, 0, p - 2) if (tax[i]) return 0;
  return 1;
}

int main() {
  n = read();
  sieve(n);
  per(i, n, 0) a[i] = read();
  int now = 0;
  rep(i, 0, n) {
    if (a[i]) {
      if (!now)
        now = abs(a[i]);
      else
        now = gcd(abs(a[i]), now);
    }
  }
  now = abs(now);

  int rest = now, i;
  for (i = 2; i * i <= now; i++) {
    if (rest % i)
      continue;
    while (rest % i == 0)
      rest /= i;
    ans.insert(i);
  }

  if (rest > 1)
    ans.insert(rest);
  rep(i, 1, tot) if (chk(pri[i])) ans.insert(pri[i]);
  set<int>::iterator it = ans.begin();
  while (it != ans.end()) {
    printf("%d\n", *it);
    it++;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define lowbit(x) (x) & (-x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define maxd 1000000007
typedef long long ll;
const int N = 100000;
const double pi = acos(-1.0);
int n, tot = 0, pri[1001000], a[10100], tax[10100];
bool ispri[10100];
set<int> ans;

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

void sieve(int n) {
  rep(i, 2, n) {
    if (!ispri[i])
      pri[++tot] = i;
    int j;
    for (j = 1; j <= tot && i * pri[j] <= n; j++) {
      ispri[i * pri[j]] = 1;
      if (i % pri[j] == 0)
        break;
    }
  }
}

int gcd(int x, int y) {
  if (!y)
    return x;
  else
    return gcd(y, x % y);
}

bool chk(int p) {
  if (a[0] % p)
    return 0;
  rep(i, 0, p - 2) tax[i] = 0;
  rep(i, 0, n) tax[i % (p - 1)] = (tax[i % (p - 1)] + a[i]) % p;
  rep(i, 0, p - 2) if (tax[i]) return 0;
  return 1;
}

int main() {
  n = read();
  sieve(n);
  per(i, n, 0) a[i] = read();
  int now = 0;
  rep(i, 0, n) {
    if (a[i]) {
      if (!now)
        now = abs(a[i]);
      else
        now = gcd(abs(a[i]), now);
    }
  }
  now = abs(now);

  int rest = now, i;
  for (i = 2; i * i <= now; i++) {
    if (rest % i)
      continue;
    while (rest % i == 0)
      rest /= i;
    ans.insert(i);
  }

  if (rest > 1)
    ans.insert(rest);
  rep(i, 1, tot) if (chk(pri[i])) ans.insert(pri[i]);
  set<int>::iterator it = ans.begin();
  while (it != ans.end()) {
    printf("%d\n", *it);
    it++;
  }
  return 0;
}