#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <numeric>
#define LL long long
std::vector<LL> factorize(LL a) {
std::vector<LL> res;
for (LL i = 1; i * i <= a; i++) {
if (a % i == 0) {
res.push_back(i);
if (i * i != a) {
res.push_back(a / i);
}
}
}
std::sort(res.rbegin(), res.rend());
return res;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<LL> nums(n + 10, 0);
LL sum = 0;
for (int i = 0; i < n; i++) {
std::cin >> nums[i];
sum += nums[i];
}
std::vector<LL> factors = factorize(sum);
std::vector<LL> started(n + 10);
LL res = 0;
for (const LL factor : factors) {
bool okay = true;
LL curr_height = 0LL;
for (int i = 0; i <= n; i++) {
if (i >= factor) {
curr_height -= started[i - factor];
}
if (curr_height > nums[i]) {
okay = false;
break;
}
LL need = nums[i] - curr_height;
started[i] = need;
curr_height += need;
}
if (okay) {
res = factor;
break;
}
}
std::cout << res;
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 | #include <iostream> #include <vector> #include <algorithm> #include <queue> #include <stack> #include <deque> #include <set> #include <map> #include <string> #include <cmath> #include <numeric> #define LL long long std::vector<LL> factorize(LL a) { std::vector<LL> res; for (LL i = 1; i * i <= a; i++) { if (a % i == 0) { res.push_back(i); if (i * i != a) { res.push_back(a / i); } } } std::sort(res.rbegin(), res.rend()); return res; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; std::cin >> n; std::vector<LL> nums(n + 10, 0); LL sum = 0; for (int i = 0; i < n; i++) { std::cin >> nums[i]; sum += nums[i]; } std::vector<LL> factors = factorize(sum); std::vector<LL> started(n + 10); LL res = 0; for (const LL factor : factors) { bool okay = true; LL curr_height = 0LL; for (int i = 0; i <= n; i++) { if (i >= factor) { curr_height -= started[i - factor]; } if (curr_height > nums[i]) { okay = false; break; } LL need = nums[i] - curr_height; started[i] = need; curr_height += need; } if (okay) { res = factor; break; } } std::cout << res; return 0; } |
English