#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
#define all(a) begin(a), end(a)
int rand(int l, int r) { return rand() % (r - l + 1) + l; }
void solve() {
int n = rand(1, 10);
cin >> n;
vector<ll> a(n);
for (auto &i : a) {
// i = rand(1, 10);
cin >> i;
}
a.push_back(0);
vector<ll> d(n + 1);
ll prev = 0;
for (int i = 0; i < n + 1; i++) {
d[i] = a[i] - prev;
// cerr << d[i] << " ";
prev = a[i];
}
// cerr << "\n";
auto check = [&](int sz) {
auto t = d;
for (int j = 0; sz + j <= n; j++) {
ll f = t[j];
if (f < 0) {
return false;
}
t[sz + j] += f;
t[j] = 0;
}
for (int j = n - sz + 1; j <= n; j++) {
if (t[j] != 0) {
return false;
}
}
return true;
};
vector<int> pr;
for (int i = 2; i <= n; i++) {
bool p = 1;
for (int j = 2; j * j <= i; j++) {
p &= i % j != 0;
}
if (!p) {
continue;
}
if (check(i)) {
pr.push_back(i);
}
}
for (int i = n; i >= 1; i--) {
int t = i;
for (auto j : pr) {
while (t % j == 0) {
t /= j;
}
}
if (t != 1) {
continue;
}
if (check(i)) {
cout << i << "\n";
return;
}
}
assert(false);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int q = 1;
// cin >> q;
while (q--) {
solve();
}
}
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 | #include <cassert> #include <iostream> #include <vector> using namespace std; using ll = long long; #define all(a) begin(a), end(a) int rand(int l, int r) { return rand() % (r - l + 1) + l; } void solve() { int n = rand(1, 10); cin >> n; vector<ll> a(n); for (auto &i : a) { // i = rand(1, 10); cin >> i; } a.push_back(0); vector<ll> d(n + 1); ll prev = 0; for (int i = 0; i < n + 1; i++) { d[i] = a[i] - prev; // cerr << d[i] << " "; prev = a[i]; } // cerr << "\n"; auto check = [&](int sz) { auto t = d; for (int j = 0; sz + j <= n; j++) { ll f = t[j]; if (f < 0) { return false; } t[sz + j] += f; t[j] = 0; } for (int j = n - sz + 1; j <= n; j++) { if (t[j] != 0) { return false; } } return true; }; vector<int> pr; for (int i = 2; i <= n; i++) { bool p = 1; for (int j = 2; j * j <= i; j++) { p &= i % j != 0; } if (!p) { continue; } if (check(i)) { pr.push_back(i); } } for (int i = n; i >= 1; i--) { int t = i; for (auto j : pr) { while (t % j == 0) { t /= j; } } if (t != 1) { continue; } if (check(i)) { cout << i << "\n"; return; } } assert(false); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int q = 1; // cin >> q; while (q--) { solve(); } } |
English