Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define ZERO (1e-10)
#define INF int(1e9 + 1)
#define CL(A, I) (memset(A, I, sizeof(A)))
#define DEB printf("DEB!\n");
#define D(X) cout << "  " << #X ": " << X << endl;
#define EQ(A, B) (A + ZERO > B && A - ZERO < B)
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define IN(n)                                                                  \
  int n;                                                                       \
  scanf("%d", &n);
#define FOR(i, m, n) for (int i(m); i < n; i++)
#define F(n) FOR(i, 0, n)
#define FF(n) FOR(j, 0, n)
#define FT(m, n) FOR(k, m, n)
#define aa first
#define bb second
void ga(int N, int *A) { F(N) scanf("%d", A + i); }
#define MX (1 << 20)
#define IS(A) ((cn[A >> 6] >> (A & 63)) & 1)
#define ST(A) (cn[A >> 6] |= 1ULL << ((A & 63)))
ll P[MX >> 1] = {2}, L(1), cn[1 + (MX >> 6)];
void gen() {
  ST(0), ST(1);
  int Q(1 + sqrt(MX));
  for (ll i(4); i < MX; i += 2)
    ST(i);
  for (int k(3); k <= Q; k += 2)
    if (!IS(k))
      for (ll h(k << 1), j(k * k); j < MX; j += h)
        ST(j);
  for (int i(3); i < MX; i += 2)
    if (!IS(i))
      P[L++] = i;
}
int fc(int N, int *f, int *c) {
  int L(0);
  for (int i(0), h(0); N >= P[i] * P[i]; ++i, h = 0) {
    while (!(N % P[i]))
      ++h, N /= P[i];
    if (h)
      f[L] = P[i], c[L++] = h;
  }
  if (N > 1)
    f[L] = N, c[L++] = 1;
  return L;
}
void rec(int *f, int *c, int p, int L, vi &o, int S) {
  if (p == L)
    o.PB(S);
  else
    F(c[p] + 1) rec(f, c, p + 1, L, o, S), S *= f[p];
}
void dsr(int N, vi &o) {
  o.clear();
  static int f[64], c[64];
  int L(fc(N, f, c));
  rec(f, c, 0, L, o, 1);
  sort(o.begin(), o.end(), greater<int>());
}
int N, A[MX], K, T, V[MX];
bool ok(int M) {
  int I = 0, J = N - 1, q = 0;
  F(N) V[i] = A[i] % M;
  sort(V, V + N);
  while (I < J) {
    int m = min(V[I], M - V[J]);
    q += m, V[I] -= m, V[J] += m, V[J] %= M;
    if (q > K)
      return 0;
    I += !V[I];
    J -= !V[J];
  }
  return 1;
}
vi o;
int main(void) {
  gen(), scanf("%d%d", &N, &K), ga(N, A), T = accumulate(A, A + N, 0);
  dsr(T, o);
  for (auto &h : o)
    if (ok(h))
      return printf("%d\n", h);
  assert(0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define ZERO (1e-10)
#define INF int(1e9 + 1)
#define CL(A, I) (memset(A, I, sizeof(A)))
#define DEB printf("DEB!\n");
#define D(X) cout << "  " << #X ": " << X << endl;
#define EQ(A, B) (A + ZERO > B && A - ZERO < B)
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define IN(n)                                                                  \
  int n;                                                                       \
  scanf("%d", &n);
#define FOR(i, m, n) for (int i(m); i < n; i++)
#define F(n) FOR(i, 0, n)
#define FF(n) FOR(j, 0, n)
#define FT(m, n) FOR(k, m, n)
#define aa first
#define bb second
void ga(int N, int *A) { F(N) scanf("%d", A + i); }
#define MX (1 << 20)
#define IS(A) ((cn[A >> 6] >> (A & 63)) & 1)
#define ST(A) (cn[A >> 6] |= 1ULL << ((A & 63)))
ll P[MX >> 1] = {2}, L(1), cn[1 + (MX >> 6)];
void gen() {
  ST(0), ST(1);
  int Q(1 + sqrt(MX));
  for (ll i(4); i < MX; i += 2)
    ST(i);
  for (int k(3); k <= Q; k += 2)
    if (!IS(k))
      for (ll h(k << 1), j(k * k); j < MX; j += h)
        ST(j);
  for (int i(3); i < MX; i += 2)
    if (!IS(i))
      P[L++] = i;
}
int fc(int N, int *f, int *c) {
  int L(0);
  for (int i(0), h(0); N >= P[i] * P[i]; ++i, h = 0) {
    while (!(N % P[i]))
      ++h, N /= P[i];
    if (h)
      f[L] = P[i], c[L++] = h;
  }
  if (N > 1)
    f[L] = N, c[L++] = 1;
  return L;
}
void rec(int *f, int *c, int p, int L, vi &o, int S) {
  if (p == L)
    o.PB(S);
  else
    F(c[p] + 1) rec(f, c, p + 1, L, o, S), S *= f[p];
}
void dsr(int N, vi &o) {
  o.clear();
  static int f[64], c[64];
  int L(fc(N, f, c));
  rec(f, c, 0, L, o, 1);
  sort(o.begin(), o.end(), greater<int>());
}
int N, A[MX], K, T, V[MX];
bool ok(int M) {
  int I = 0, J = N - 1, q = 0;
  F(N) V[i] = A[i] % M;
  sort(V, V + N);
  while (I < J) {
    int m = min(V[I], M - V[J]);
    q += m, V[I] -= m, V[J] += m, V[J] %= M;
    if (q > K)
      return 0;
    I += !V[I];
    J -= !V[J];
  }
  return 1;
}
vi o;
int main(void) {
  gen(), scanf("%d%d", &N, &K), ga(N, A), T = accumulate(A, A + N, 0);
  dsr(T, o);
  for (auto &h : o)
    if (ok(h))
      return printf("%d\n", h), 0;
  assert(0);
  return 0;
}