#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define vc vector
#define st first
#define nd second
#define all(a) a.begin(), a.end()
#define sz(a) (ll)a.size()
#define pub push_back
#define pob pop_back
ll n;
vc<ll> a;
void input() {
cin >> n;
a.resize(n);
for (ll &ai : a)
cin >> ai;
}
bool check(ll k) {
vc<ll> b = a;
vc<ll> d(n + 1, 0);
ll c = 0;
for (ll i = 0; i < n; i++) {
c += d[i];
b[i] += c;
if (b[i] < 0)
return false;
if (b[i] > 0 and i + k > n)
return false;
c -= b[i];
if (i + k <= n)
d[i + k] += b[i];
}
return true;
}
void solve() {
ll s = 0;
for (ll &ai : a)
s += ai;
for (ll k = n; k >= 1; k--)
if (s % k == 0 and check(k)) {
cout << k << "\n";
return;
}
}
void program() {
input();
solve();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
program();
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; #define vc vector #define st first #define nd second #define all(a) a.begin(), a.end() #define sz(a) (ll)a.size() #define pub push_back #define pob pop_back ll n; vc<ll> a; void input() { cin >> n; a.resize(n); for (ll &ai : a) cin >> ai; } bool check(ll k) { vc<ll> b = a; vc<ll> d(n + 1, 0); ll c = 0; for (ll i = 0; i < n; i++) { c += d[i]; b[i] += c; if (b[i] < 0) return false; if (b[i] > 0 and i + k > n) return false; c -= b[i]; if (i + k <= n) d[i + k] += b[i]; } return true; } void solve() { ll s = 0; for (ll &ai : a) s += ai; for (ll k = n; k >= 1; k--) if (s % k == 0 and check(k)) { cout << k << "\n"; return; } } void program() { input(); solve(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); program(); return 0; } |
English