#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define st first
#define nd second
#define ld long double
bool sprawdz (ll k, vector<ll> &a, int n) {
vector<ll> s (n+k, 0);
ll x = 0;
for (int i = 0; i < n; i++) {
x += s[i];
if (x < a[i]) {
s[i+k] -= a[i]-x;
x = a[i];
}
else if (x > a[i]) {
return 0;
}
}
return x+s[n]==0;
}
void solve() {
int n;
cin >> n;
vector<ll> a (n);
ll s = 0;
for (ll &i : a) {
cin >> i;
s += i;
}
set<ll> d;
for (ll i = 1; i*i <= s; i++) {
if (s%i==0) {
if (i <= n) {
d.insert(i);
}
if (s/i <= n) {
d.insert(s/i);
}
}
}
vector<ll> ok, nok;
while (!d.empty()) {
int l = d.size();
int r = rand()%l;
auto pt = d.begin();
for (int i = 0; i < r; i++, pt++);
ll x = *pt;
d.erase(pt);
bool q = 0;
for (ll i : ok) {
if (i%x == 0) {
q = 1;
ok.push_back(x);
break;
}
}
for (ll i : nok) {
if (x%i == 0) {
q = 1;
nok.push_back(x);
break;
}
}
if (q) continue;
q = sprawdz(x, a, n);
if (q) ok.push_back(x);
else nok.push_back(x);
}
sort(ok.begin(), ok.end());
cout << ok[ok.size()-1] << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(67);
int t=1;
//cin >> t;
for (int i = 0; i < t; i++) {
solve();
}
}
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <bits/stdc++.h> using namespace std; #define ll long long #define st first #define nd second #define ld long double bool sprawdz (ll k, vector<ll> &a, int n) { vector<ll> s (n+k, 0); ll x = 0; for (int i = 0; i < n; i++) { x += s[i]; if (x < a[i]) { s[i+k] -= a[i]-x; x = a[i]; } else if (x > a[i]) { return 0; } } return x+s[n]==0; } void solve() { int n; cin >> n; vector<ll> a (n); ll s = 0; for (ll &i : a) { cin >> i; s += i; } set<ll> d; for (ll i = 1; i*i <= s; i++) { if (s%i==0) { if (i <= n) { d.insert(i); } if (s/i <= n) { d.insert(s/i); } } } vector<ll> ok, nok; while (!d.empty()) { int l = d.size(); int r = rand()%l; auto pt = d.begin(); for (int i = 0; i < r; i++, pt++); ll x = *pt; d.erase(pt); bool q = 0; for (ll i : ok) { if (i%x == 0) { q = 1; ok.push_back(x); break; } } for (ll i : nok) { if (x%i == 0) { q = 1; nok.push_back(x); break; } } if (q) continue; q = sprawdz(x, a, n); if (q) ok.push_back(x); else nok.push_back(x); } sort(ok.begin(), ok.end()); cout << ok[ok.size()-1] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); srand(67); int t=1; //cin >> t; for (int i = 0; i < t; i++) { solve(); } } |
English