#include <bits/stdc++.h>
using namespace std;
int subtract[100001];
bool check(int n, vector<int>& a, int d)
{
for (int i = 0; i <= n; ++i) subtract[i] = 0;
int current = 0;
for (int i = 0; i < n; ++i)
{
current -= subtract[i];
if (a[i] - current < 0) return false;
if (a[i] - current > 0)
{
if (i + d > n) return false;
subtract[i + d] += a[i] - current;
current += a[i] - current;
}
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
long long sum = 0;
vector<int> a(n);
for (int i = 0; i < n; ++i)
{
cin >> a[i];
sum += a[i];
}
int best = 1;
for (int d = 1; d * d <= sum && d <= n; ++d)
{
if (sum % d) continue;
if (check(n, a, d)) best = max(best, d);
if (sum / d <= n && check(n, a, sum / d)) best = max(best, (int) (sum / d));
}
cout << best << '\n';
}
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 | #include <bits/stdc++.h> using namespace std; int subtract[100001]; bool check(int n, vector<int>& a, int d) { for (int i = 0; i <= n; ++i) subtract[i] = 0; int current = 0; for (int i = 0; i < n; ++i) { current -= subtract[i]; if (a[i] - current < 0) return false; if (a[i] - current > 0) { if (i + d > n) return false; subtract[i + d] += a[i] - current; current += a[i] - current; } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; long long sum = 0; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; sum += a[i]; } int best = 1; for (int d = 1; d * d <= sum && d <= n; ++d) { if (sum % d) continue; if (check(n, a, d)) best = max(best, d); if (sum / d <= n && check(n, a, sum / d)) best = max(best, (int) (sum / d)); } cout << best << '\n'; } |
English