#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<long long> a(n + 1);
a[0] = 0;
long long S = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
S += a[i];
}
vector<long long> d(n + 1);
for (int i = 1; i <= n; i++)
d[i] = a[i] - a[i - 1];
vector<int> candidates;
for (long long div = 1; div * div <= S; div++) {
if (S % div == 0) {
if (div <= n) candidates.push_back((int)div);
long long other = S / div;
if (other != div && other <= n) candidates.push_back((int)other);
}
}
sort(candidates.rbegin(), candidates.rend());
vector<long long> x(n + 1);
auto check = [&](int k) -> bool {
for (int i = 1; i <= n; i++) {
x[i] = d[i] + (i > k ? x[i - k] : 0LL);
if (i <= n - k + 1) {
if (x[i] < 0) return false;
} else {
if (x[i] != 0) return false;
}
}
return true;
};
for (int k : candidates) {
if (check(k)) {
cout << k << "\n";
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<long long> a(n + 1); a[0] = 0; long long S = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; S += a[i]; } vector<long long> d(n + 1); for (int i = 1; i <= n; i++) d[i] = a[i] - a[i - 1]; vector<int> candidates; for (long long div = 1; div * div <= S; div++) { if (S % div == 0) { if (div <= n) candidates.push_back((int)div); long long other = S / div; if (other != div && other <= n) candidates.push_back((int)other); } } sort(candidates.rbegin(), candidates.rend()); vector<long long> x(n + 1); auto check = [&](int k) -> bool { for (int i = 1; i <= n; i++) { x[i] = d[i] + (i > k ? x[i - k] : 0LL); if (i <= n - k + 1) { if (x[i] < 0) return false; } else { if (x[i] != 0) return false; } } return true; }; for (int k : candidates) { if (check(k)) { cout << k << "\n"; return 0; } } return 0; } |
English