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