1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
    o << "{";
    for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
    return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
    return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) {}
#endif

#define x first
#define y second
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)((x).size()))

using ll = long long;
using pii = pair<ll, ll>;

vector<pii> trial_division1(long long n) {
  vector<pii> factorization;
  for (long long d = 2; d * d <= n; d++) {
    int ct = 0;
    while (n % d == 0) {
      ct++;
      n /= d;
    }
    if (ct)
      factorization.push_back({d, ct});
  }
  if (n > 1)
    factorization.push_back({n, 1});
  return factorization;
}


using u64 = uint64_t;
using i64 = int64_t;
typedef unsigned long long ull;

ull modmul(ull a, ull b, ull M) {
	ll ret = a * b - M * ull(1.L / M * a * b);
	return ret + M * (ret < 0) - M * (ret >= (ll)M);
}

ull modpow(ull b, ull e, ull mod) {
	ull ans = 1;
	for (; e; b = modmul(b, b, mod), e /= 2)
		if (e & 1) ans = modmul(ans, b, mod);
	return ans;
}

bool isPrime(ull n) {
	if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;
	ull A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022},
	    s = __builtin_ctzll(n-1), d = n >> s;
	for (ull a : A) {   // ^ count trailing zeroes
		ull p = modpow(a%n, d, n), i = s;
		while (p != 1 && p != n - 1 && a % n && i--)
			p = modmul(p, p, n);
		if (p != n-1 && i != s) return 0;
	}
	return 1;
}

ull pollard(ull n) {
	ull x = 0, y = 0, t = 30, prd = 2, i = 1, q;
	auto f = [&](ull x) { return modmul(x, x, n) + i; };
	while (t++ % 40 || __gcd(prd, n) == 1) {
		if (x == y) x = ++i, y = f(x);
		if ((q = modmul(prd, max(x,y) - min(x,y), n))) prd = q;
		x = f(x), y = f(f(y));
	}
	return __gcd(prd, n);
}
vector<ull> factor(ull n) {
	if (n == 1) return {};
	if (isPrime(n)) return {n};
	ull x = pollard(n);
	auto l = factor(x), r = factor(n / x);
	l.insert(l.end(), all(r));
	return l;
}

ll oN;
vec<pii> ps;
ll mx = 1;
void bt(int i, ll val, const function<bool(ll)>& check) {
  if (oN < val) return;
  // if (!check(val)) return;
  // cerr << i << " " << val << endl;
  // cerr << check(1) << endl;
  // if (!check(val)) {
  //   // cerr << "bail" << val << endl;
  //   return; 
  // }
  if (i == ps.size()) {
    if (check(val)) {
      mx = max(mx, val);
    }
    return;
  }
  if (i == ps.size()) return;
  int start = 0;
  // if (i == ps.size()-1) start = 1, val *= ps[i].x;
  rep (k, start, ps[i].y +1) {
    bt(i+1, val, check);
    val *= ps[i].x;
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int N; cin >> N;
  oN = N;
  vec<ll> a(N);
  ll s = 0;
  rep (n, 0, N) {
    cin >> a[n];
    s += a[n];
  }
  if (s == 1) {
    cout << "1\n";
    return 0;
  }
  vec<ull> l = factor(s);
  sort(all(l));
  ps.push_back({l[0], 1});
  rep (i, 1, l.size()) {
    if (ps.back().x == l[i]) {
      ps.back().y++;
    } else {
      ps.push_back({l[i], 1});
    }
  }
  debug(ps);
  debug(s);
  
  vec<ll> put(N);

  auto check = [&](ll len) {
    // put.assign(N, 0);
    // pref.assign(N+1, 0);
    put[0] = 0;
    debug(len);
    ll ls = 0;
    ll val = 0;
    rep (n, 0, N) {
      val += n-1 >= 0 ? put[n-1] : 0;
      val -= n-len >= 0 ? put[n-len] : 0;
      put[n] = a[n] - val;

      ls += put[n];
      // debug(n, put[n]);
      if (put[n] < 0) return false;
      if (n + len > N && put[n] != 0) return false;
    }
    debug(put);
    // debug(a, s);
    debug(ls, len, s);
    return ls * len == s;
  };

  bt(0, 1, check);
  cout << mx << "\n";
  return 0;
}