#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
bool check(int k, int n, const vector<int>& a) {
if (k <= 0) return false;
vector<long long> d(n + 2, 0);
d[1] = a[0];
for (int i = 1; i < n; ++i) {
d[i + 1] = (long long)a[i] - a[i - 1];
}
d[n + 1] = -(long long)a[n - 1];
for (int i = 1; i <= n - k + 1; ++i) {
if (d[i] < 0) return false;
long long num_waves = d[i];
d[i] -= num_waves;
d[i + k] += num_waves;
}
for (int i = 1; i <= n + 1; ++i) {
if (d[i] != 0) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
if (!(cin >> n)) return 0;
vector<int> a(n);
long long sum_a = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
sum_a += a[i];
}
vector<int> candidates;
for (long long i = 1; i * i <= sum_a; ++i) {
if (sum_a % i == 0) {
if (i <= n) candidates.push_back(i);
if (sum_a / i <= n) candidates.push_back(sum_a / i);
}
}
sort(candidates.rbegin(), candidates.rend());
for (int k : candidates) {
if (check(k, n, a)) {
cout << k << endl;
return 0;
}
}
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 64 65 66 | #include <iostream> #include <vector> #include <numeric> #include <algorithm> using namespace std; bool check(int k, int n, const vector<int>& a) { if (k <= 0) return false; vector<long long> d(n + 2, 0); d[1] = a[0]; for (int i = 1; i < n; ++i) { d[i + 1] = (long long)a[i] - a[i - 1]; } d[n + 1] = -(long long)a[n - 1]; for (int i = 1; i <= n - k + 1; ++i) { if (d[i] < 0) return false; long long num_waves = d[i]; d[i] -= num_waves; d[i + k] += num_waves; } for (int i = 1; i <= n + 1; ++i) { if (d[i] != 0) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; if (!(cin >> n)) return 0; vector<int> a(n); long long sum_a = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; sum_a += a[i]; } vector<int> candidates; for (long long i = 1; i * i <= sum_a; ++i) { if (sum_a % i == 0) { if (i <= n) candidates.push_back(i); if (sum_a / i <= n) candidates.push_back(sum_a / i); } } sort(candidates.rbegin(), candidates.rend()); for (int k : candidates) { if (check(k, n, a)) { cout << k << endl; return 0; } } return 0; } |
English