#include <iostream>
#include <vector>
using namespace std;
bool can_reduce_to_zero(const vector<long long>& arr, int k) {
int n = (int)arr.size();
if (n == 0) {
return true;
}
vector<long long> diff(n + 1, 0);
long long active_sub = 0;
for (int i = 0; i < n; i++) {
active_sub -= diff[i];
long long current = arr[i] - active_sub;
if (current < 0) {
return false;
}
if (i <= n - k) {
long long x = current;
active_sub += x;
diff[i + k] += x;
} else {
if (current != 0) {
return false;
}
}
}
return true;
}
int main() {
int n;
cin >> n;
long long s=0;
vector<long long> ll(n);
for (int i = 0; i < n; i++) {
cin >> ll[i];
s += ll[i];
}
for (int x = 0; x < n; x++) {
int k = n - x;
if (s % k != 0) { continue; }
if (can_reduce_to_zero(ll, k)) {
cout << k << '\n';
break;
}
}
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 | #include <iostream> #include <vector> using namespace std; bool can_reduce_to_zero(const vector<long long>& arr, int k) { int n = (int)arr.size(); if (n == 0) { return true; } vector<long long> diff(n + 1, 0); long long active_sub = 0; for (int i = 0; i < n; i++) { active_sub -= diff[i]; long long current = arr[i] - active_sub; if (current < 0) { return false; } if (i <= n - k) { long long x = current; active_sub += x; diff[i + k] += x; } else { if (current != 0) { return false; } } } return true; } int main() { int n; cin >> n; long long s=0; vector<long long> ll(n); for (int i = 0; i < n; i++) { cin >> ll[i]; s += ll[i]; } for (int x = 0; x < n; x++) { int k = n - x; if (s % k != 0) { continue; } if (can_reduce_to_zero(ll, k)) { cout << k << '\n'; break; } } return 0; } |
English