Problem p03065 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

bool isPrime(long long p) {
  if (p < 0) {
    p *= -1;
  }

  if (p == 0 || p == 1) {
    return false;
  } else if (p == 2) {
    return true;
  } else if (p % 2 == 0) {
    return false;
  }
  long long p_sqrt = sqrt(p);
  for (long long i = 3; i <= p_sqrt; i++) {
    if (p % i == 0) {
      return false;
    }
  }
  return true;
}

int gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  int N;
  cin >> N;
  vector<long long> a(N + 1);
  set<long long> ans;
  long long coef_gcd, tmp;
  coef_gcd = 0;

  for (int i = N; i >= 0; i--) {
    cin >> a[i];
    tmp = a[i];
    if (a[i] != 0) {
      if (tmp < 0) {
        tmp *= (-1);
      }
      if (coef_gcd == 0) {
        coef_gcd = tmp;
      } else {
        coef_gcd = gcd(tmp, coef_gcd);
      }
    }
  }

  for (long long i = 2; i * i <= coef_gcd; i++) {
    while (coef_gcd % i == 0) {
      ans.insert(i);
      coef_gcd /= i;
    }
  }
  if (coef_gcd != 1) {
    ans.insert(coef_gcd);
  }

  for (long long p = 2; p <= N; p++) {
    if (isPrime(p)) {
      vector<long long> coeff(p, 0);
      coeff[0] = a[0];
      for (int i = 1; i <= N; i++) {
        long long idx = i % (p - 1) == 0 ? p - 1 : i;
        coeff[idx] += a[i];
      }
      int flag = 0;
      for (int i = 0; i < p; i++) {
        if (coeff[i] % p != 0) {
          flag = 1;
          break;
        }
      }
      if (flag == 0) {
        ans.insert(p);
      }
    }
  }

  for (auto it : ans) {
    cout << it << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

bool isPrime(long long p) {
  if (p < 0) {
    p *= -1;
  }

  if (p == 0 || p == 1) {
    return false;
  } else if (p == 2) {
    return true;
  } else if (p % 2 == 0) {
    return false;
  }
  long long p_sqrt = sqrt(p);
  for (long long i = 3; i <= p_sqrt; i++) {
    if (p % i == 0) {
      return false;
    }
  }
  return true;
}

int gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  int N;
  cin >> N;
  vector<long long> a(N + 1);
  set<long long> ans;
  long long coef_gcd, tmp;
  coef_gcd = 0;

  for (int i = N; i >= 0; i--) {
    cin >> a[i];
    tmp = a[i];
    if (a[i] != 0) {
      if (tmp < 0) {
        tmp *= (-1);
      }
      if (coef_gcd == 0) {
        coef_gcd = tmp;
      } else {
        coef_gcd = gcd(tmp, coef_gcd);
      }
    }
  }

  for (long long i = 2; i * i <= coef_gcd; i++) {
    while (coef_gcd % i == 0) {
      ans.insert(i);
      coef_gcd /= i;
    }
  }
  if (coef_gcd != 1) {
    ans.insert(coef_gcd);
  }

  for (long long p = 2; p <= N; p++) {
    if (isPrime(p)) {
      vector<long long> coeff(p, 0);
      coeff[0] = a[0];
      for (int i = 1; i <= N; i++) {
        long long idx = i % (p - 1) == 0 ? p - 1 : i % (p - 1);
        coeff[idx] += a[i];
      }
      int flag = 0;
      for (int i = 0; i < p; i++) {
        if (coeff[i] % p != 0) {
          flag = 1;
          break;
        }
      }
      if (flag == 0) {
        ans.insert(p);
      }
    }
  }

  for (auto it : ans) {
    cout << it << endl;
  }
  return 0;
}