#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.cpp"
#else
#define debug(x...)
#endif
using ll = long long;
int main() {
ll n;
cin >> n;
vector <ll> v(n);
for (auto &x : v) cin >> x;
auto check = [&](const ll x) -> bool {
vector <ll> pref(n + 1, 0);
vector <ll> tab = v;
ll cur = 0;
for (int i = 0; i < n; i++) {
cur += pref[i];
tab[i] -= cur;
if (tab[i] > 0 && i + x <= n) {
cur += tab[i];
pref[i + x] -= tab[i];
tab[i] = 0;
}
if (tab[i] != 0) return false;
}
return (*max_element(tab.begin(), tab.end()) == 0 && *min_element(tab.begin(), tab.end()) == 0);
};
set <ll> st;
ll s = accumulate(v.begin(), v.end(), 0LL);
for (ll i = 1; i * i <= s; i++) {
if (s % i == 0) {
if (i <= n) st.insert(i);
if (s / i <= n) st.insert(s / i);
}
}
st.erase(1);
vector <ll> to_try(st.rbegin(), st.rend());
for (auto x : to_try) {
if (check(x)) {
cout << x << '\n';
return 0;
}
}
cout << 1 << '\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 | #include "bits/stdc++.h" using namespace std; #ifdef LOCAL #include "debug.cpp" #else #define debug(x...) #endif using ll = long long; int main() { ll n; cin >> n; vector <ll> v(n); for (auto &x : v) cin >> x; auto check = [&](const ll x) -> bool { vector <ll> pref(n + 1, 0); vector <ll> tab = v; ll cur = 0; for (int i = 0; i < n; i++) { cur += pref[i]; tab[i] -= cur; if (tab[i] > 0 && i + x <= n) { cur += tab[i]; pref[i + x] -= tab[i]; tab[i] = 0; } if (tab[i] != 0) return false; } return (*max_element(tab.begin(), tab.end()) == 0 && *min_element(tab.begin(), tab.end()) == 0); }; set <ll> st; ll s = accumulate(v.begin(), v.end(), 0LL); for (ll i = 1; i * i <= s; i++) { if (s % i == 0) { if (i <= n) st.insert(i); if (s / i <= n) st.insert(s / i); } } st.erase(1); vector <ll> to_try(st.rbegin(), st.rend()); for (auto x : to_try) { if (check(x)) { cout << x << '\n'; return 0; } } cout << 1 << '\n'; } |
English