#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
using int64 = long long int;
int n;
std::vector<int> a;
int64 sum;
int p;
std::vector<int> tree;
void update_tree(int node, int left, int right, int update_left, int update_right, int count)
{
if (update_left >= update_right)
return;
if (update_left == left && right == update_right)
{
tree[node] += count;
return;
}
const int mid = (left + right) / 2;
update_tree(node * 2, left, mid, update_left, std::min(update_right, mid), count);
update_tree(node * 2 + 1, mid, right, std::max(update_left, mid), update_right, count);
}
struct entry
{
int node, left, right, update_left, update_right;
};
entry stack[1 << 18];
int stack_size = 0;
void update_tree2(int node, int left, int right, int update_left, int update_right, int count)
{
stack_size = 0;
stack[stack_size++] = entry{ node, left, right, update_left, update_right };
while (stack_size)
{
entry e = stack[(stack_size--) - 1];
if (e.update_left == e.left && e.right == e.update_right)
{
tree[e.node] += count;
continue;
}
const int mid = (e.left + e.right) / 2;
{
const int r = std::min(e.update_right, mid);
if (e.update_left < r)
stack[stack_size++] = entry{ e.node << 1, e.left, mid, e.update_left, r };
}
{
const int l = std::max(mid, e.update_left);
if (l < e.update_right)
stack[stack_size++] = entry{ (e.node << 1) + 1, mid, e.right, l, e.update_right };
}
}
}
int get_count(int index)
{
int count = 0;
int l = p + index;
while (l > 0)
{
count += tree[l];
l /= 2;
}
return count;
}
bool check(int k)
{
for (int i = 0; i < n - k + 1; ++i)
{
const int count = get_count(i);
if (count > 0)
update_tree2(1, 0, p, i, i + k, -count);
else if (count < 0)
return false;
}
for (int i = n - k + 1; i < n; ++i)
{
const int count = get_count(i);
if (count != 0)
return false;
}
return true;
}
bool check2(int k)
{
std::vector<int> pop(n, 0);
int sub = 0;
for (int i = 0; i < n; ++i)
{
if (i > 0)
sub -= pop[i - 1];
if (a[i] > sub)
{
if (i + k - 1 >= n)
return false;
const int diff = a[i] - sub;
sub += diff;
pop[i + k - 1] += diff;
}
else if (a[i] < sub)
return false;
}
return true;
}
int main()
{
#if 1
std::cin >> n;
a.resize(n);
for (int i = 0; i < n; ++i)
{
std::cin >> a[i];
sum += a[i];
}
#else
n = 100000;
a.resize(n);
for (int i = 0; i < n; ++i)
{
a[i] = i;
sum += a[i];
}
#endif
#if 0
std::vector<int> reusable_tree;
p = 1;
while (p < n)
p *= 2;
reusable_tree.resize(p * 2, 0);
for (int i = 0; i < n; ++i)
reusable_tree[p + i] = a[i];
for (int i = p - 1; i > 0; --i)
{
int& left = reusable_tree[i * 2];
int& right = reusable_tree[i * 2 + 1];
const int shared = std::min(left, right);
left -= shared;
right -= shared;
reusable_tree[i] += shared;
}
#endif
int max_wave = 1;
std::unordered_set<int> nope;
for (int i = 2; i <= n; ++i)
{
if (sum % i == 0)
{
bool div_prev = false;
for (int j : nope)
if (i % j == 0)
{
div_prev = true;
break;
}
if (div_prev)
continue;
#if 0
tree = reusable_tree;
if (check(i))
{
max_wave = i;
continue;
}
#else
if (check2(i))
{
max_wave = i;
continue;
}
#endif
nope.insert(i);
}
}
std::cout << max_wave << std::endl;
return 0;
}
/*
5
1 1 0 1 1
5
1 0 0 1 1
8
1 2 3 4 5 5 3 1
4
1 1 0 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | #include <iostream> #include <vector> #include <unordered_set> #include <algorithm> using int64 = long long int; int n; std::vector<int> a; int64 sum; int p; std::vector<int> tree; void update_tree(int node, int left, int right, int update_left, int update_right, int count) { if (update_left >= update_right) return; if (update_left == left && right == update_right) { tree[node] += count; return; } const int mid = (left + right) / 2; update_tree(node * 2, left, mid, update_left, std::min(update_right, mid), count); update_tree(node * 2 + 1, mid, right, std::max(update_left, mid), update_right, count); } struct entry { int node, left, right, update_left, update_right; }; entry stack[1 << 18]; int stack_size = 0; void update_tree2(int node, int left, int right, int update_left, int update_right, int count) { stack_size = 0; stack[stack_size++] = entry{ node, left, right, update_left, update_right }; while (stack_size) { entry e = stack[(stack_size--) - 1]; if (e.update_left == e.left && e.right == e.update_right) { tree[e.node] += count; continue; } const int mid = (e.left + e.right) / 2; { const int r = std::min(e.update_right, mid); if (e.update_left < r) stack[stack_size++] = entry{ e.node << 1, e.left, mid, e.update_left, r }; } { const int l = std::max(mid, e.update_left); if (l < e.update_right) stack[stack_size++] = entry{ (e.node << 1) + 1, mid, e.right, l, e.update_right }; } } } int get_count(int index) { int count = 0; int l = p + index; while (l > 0) { count += tree[l]; l /= 2; } return count; } bool check(int k) { for (int i = 0; i < n - k + 1; ++i) { const int count = get_count(i); if (count > 0) update_tree2(1, 0, p, i, i + k, -count); else if (count < 0) return false; } for (int i = n - k + 1; i < n; ++i) { const int count = get_count(i); if (count != 0) return false; } return true; } bool check2(int k) { std::vector<int> pop(n, 0); int sub = 0; for (int i = 0; i < n; ++i) { if (i > 0) sub -= pop[i - 1]; if (a[i] > sub) { if (i + k - 1 >= n) return false; const int diff = a[i] - sub; sub += diff; pop[i + k - 1] += diff; } else if (a[i] < sub) return false; } return true; } int main() { #if 1 std::cin >> n; a.resize(n); for (int i = 0; i < n; ++i) { std::cin >> a[i]; sum += a[i]; } #else n = 100000; a.resize(n); for (int i = 0; i < n; ++i) { a[i] = i; sum += a[i]; } #endif #if 0 std::vector<int> reusable_tree; p = 1; while (p < n) p *= 2; reusable_tree.resize(p * 2, 0); for (int i = 0; i < n; ++i) reusable_tree[p + i] = a[i]; for (int i = p - 1; i > 0; --i) { int& left = reusable_tree[i * 2]; int& right = reusable_tree[i * 2 + 1]; const int shared = std::min(left, right); left -= shared; right -= shared; reusable_tree[i] += shared; } #endif int max_wave = 1; std::unordered_set<int> nope; for (int i = 2; i <= n; ++i) { if (sum % i == 0) { bool div_prev = false; for (int j : nope) if (i % j == 0) { div_prev = true; break; } if (div_prev) continue; #if 0 tree = reusable_tree; if (check(i)) { max_wave = i; continue; } #else if (check2(i)) { max_wave = i; continue; } #endif nope.insert(i); } } std::cout << max_wave << std::endl; return 0; } /* 5 1 1 0 1 1 5 1 0 0 1 1 8 1 2 3 4 5 5 3 1 4 1 1 0 0 */ |
English