#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define sn second
typedef long long ll;
typedef vector<int> VI;
typedef vector<char> VC;
typedef pair<int, int> PI;
struct Factor {
ll f;
int cnt;
};
struct Wave {
int width;
int end;
};
void factorize(ll x, vector<Factor>& F) {
Factor f;
f.f = 2;
f.cnt = 0;
while ((x & 1) == 0) {
x >>= 1;
f.cnt++;
}
if (f.cnt > 0) {
F.pb(f);
}
f.f = 3;
f.cnt = 0;
while (f.f * f.f <= x) {
while (x % f.f == 0) {
f.cnt++;
x /= f.f;
}
if (f.cnt > 0) {
F.pb(f);
}
f.f += 2;
f.cnt = 0;
}
f.f = x;
f.cnt = 1;
if (f.cnt > 0) {
F.pb(f);
}
}
bool verify(int k, int n, VI& A) {
queue<Wave> Q;
int mod = 0;
for (int i = 0; i < n; i++) {
while (!Q.empty() && Q.front().end < i) {
mod -= Q.front().width;
Q.pop();
}
if (A[i] - mod > 0) {
Wave w;
w.width = A[i] - mod;
w.end = i + k - 1;
mod += w.width;
Q.push(w);
}
else if (A[i] - mod < 0) {
return false;
}
}
while (!Q.empty() && Q.front().end < n) {
mod -= Q.front().width;
Q.pop();
}
return mod == 0;
}
int verify_over_all_divs(int k, int f, int n, VI& A, vector<Factor>& F) {
if (k > n) {
return 0;
}
if (f == F.size()) {
return verify(k, n, A) ? k : 0;
}
int res = verify_over_all_divs(k, f + 1, n, A, F);
int mul = 1;
for (int i = 0; i < F[f].cnt; i++) {
mul *= F[f].f;
res = max(res, verify_over_all_divs(k * mul, f + 1, n, A, F));
}
return res;
}
int main() {
int n;
cin >> n;
VI A(n);
ll sum = 0; //99999992526;
for (int i = 0; i < n; i++) {
cin >> A[i];
sum += A[i];
}
vector<Factor> factors;
factorize(sum, factors);
int res = max(1, verify_over_all_divs(1, 0, n, A, factors));
cout << res << endl;
#ifdef DEBUG
cout << sum << endl;
for (int i = 0; i < factors.size(); i++) {
cout << factors[i].f << " " << factors[i].cnt << endl;
}
#endif
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define sn second typedef long long ll; typedef vector<int> VI; typedef vector<char> VC; typedef pair<int, int> PI; struct Factor { ll f; int cnt; }; struct Wave { int width; int end; }; void factorize(ll x, vector<Factor>& F) { Factor f; f.f = 2; f.cnt = 0; while ((x & 1) == 0) { x >>= 1; f.cnt++; } if (f.cnt > 0) { F.pb(f); } f.f = 3; f.cnt = 0; while (f.f * f.f <= x) { while (x % f.f == 0) { f.cnt++; x /= f.f; } if (f.cnt > 0) { F.pb(f); } f.f += 2; f.cnt = 0; } f.f = x; f.cnt = 1; if (f.cnt > 0) { F.pb(f); } } bool verify(int k, int n, VI& A) { queue<Wave> Q; int mod = 0; for (int i = 0; i < n; i++) { while (!Q.empty() && Q.front().end < i) { mod -= Q.front().width; Q.pop(); } if (A[i] - mod > 0) { Wave w; w.width = A[i] - mod; w.end = i + k - 1; mod += w.width; Q.push(w); } else if (A[i] - mod < 0) { return false; } } while (!Q.empty() && Q.front().end < n) { mod -= Q.front().width; Q.pop(); } return mod == 0; } int verify_over_all_divs(int k, int f, int n, VI& A, vector<Factor>& F) { if (k > n) { return 0; } if (f == F.size()) { return verify(k, n, A) ? k : 0; } int res = verify_over_all_divs(k, f + 1, n, A, F); int mul = 1; for (int i = 0; i < F[f].cnt; i++) { mul *= F[f].f; res = max(res, verify_over_all_divs(k * mul, f + 1, n, A, F)); } return res; } int main() { int n; cin >> n; VI A(n); ll sum = 0; //99999992526; for (int i = 0; i < n; i++) { cin >> A[i]; sum += A[i]; } vector<Factor> factors; factorize(sum, factors); int res = max(1, verify_over_all_divs(1, 0, n, A, factors)); cout << res << endl; #ifdef DEBUG cout << sum << endl; for (int i = 0; i < factors.size(); i++) { cout << factors[i].f << " " << factors[i].cnt << endl; } #endif return 0; } |
English