#include <bits/stdc++.h>
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldbl;
typedef pair<int, int> pii;
typedef pair<uint, uint> puu;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<uint> vu;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pii> vpii;
typedef vector<puu> vpuu;
typedef vector<pll> vpll;
typedef vector<pull> vpull;
typedef vector<string> vstr;
typedef vector<double> vdbl;
typedef vector<ldbl> vldbl;
#define pb push_back
#define ppb pop_back
#define pfr push_front
#define ppfr pop_front
#define emp emplace
#define empb emplace_back
#define be begin
#define rbe rbegin
#define all(x) (x).be(), (x).end()
#define rall(x) (x).rbe(), (x).rend()
#define fir first
#define sec second
#define mkp make_pair
#define brif(cond) if (cond) break
#define ctif(cond) if (cond) continue
#define retif(cond) if (cond) return
void fastio() {ios_base::sync_with_stdio(false);cin.tie(nullptr);}
template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;}
template<typename T> T extgcd(T a, T b, T &x, T &y)
{
T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
while (b) {
T q = a/b; a %= b; swap(a, b);
x0 -= q*x1; swap(x0, x1);
y0 -= q*y1; swap(y0, y1);
}
x = x0; y = y0; return a;
}
int ctz(uint x) {return __builtin_ctz(x);}
int ctzll(ull x) {return __builtin_ctzll(x);}
int clz(uint x) {return __builtin_clz(x);}
int clzll(ull x) {return __builtin_clzll(x);}
int popcnt(uint x) {return __builtin_popcount(x);}
int popcntll(ull x) {return __builtin_popcountll(x);}
int bsr(uint x) {return 31^clz(x);}
int bsrll(ull x) {return 63^clzll(x);}
#define N 100016
vpii seg;
int a[N], d[N];
bool check(int n, int k)
{
copy_n(a, n+1, d);
for (pii p : seg) {
int l = p.fir, r = p.sec;
for (int i = l; i <= r-k; ++i) {
retif(d[i] < 0) false;
if (d[i] > 0) d[i+k] += d[i];
}
for (int i = r-k+1; i <= r; ++i) {
retif(d[i]) false;
}
}
return true;
}
int main()
{
fastio();
int n, ub;
ll m = 0;
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
ub = n;
for (int i = 0, j = 1; i < n; i = j++) {
ctif(a[i] == 0);
ll s = a[i];
while (j < n && a[j] > 0) s += a[j++];
seg.empb(i, j);
ub = min(ub, j-i);
m = gcd(s, m);
}
for (int i = n; i > 0; --i) a[i] -= a[i-1];
for (int k = ub; k > 1; --k) {
ctif(m%k);
retif(check(n, k)) cout << k << '\n', 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 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 | #include <bits/stdc++.h> using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef long double ldbl; typedef pair<int, int> pii; typedef pair<uint, uint> puu; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<uint> vu; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<puu> vpuu; typedef vector<pll> vpll; typedef vector<pull> vpull; typedef vector<string> vstr; typedef vector<double> vdbl; typedef vector<ldbl> vldbl; #define pb push_back #define ppb pop_back #define pfr push_front #define ppfr pop_front #define emp emplace #define empb emplace_back #define be begin #define rbe rbegin #define all(x) (x).be(), (x).end() #define rall(x) (x).rbe(), (x).rend() #define fir first #define sec second #define mkp make_pair #define brif(cond) if (cond) break #define ctif(cond) if (cond) continue #define retif(cond) if (cond) return void fastio() {ios_base::sync_with_stdio(false);cin.tie(nullptr);} template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;} template<typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a/b; a %= b; swap(a, b); x0 -= q*x1; swap(x0, x1); y0 -= q*y1; swap(y0, y1); } x = x0; y = y0; return a; } int ctz(uint x) {return __builtin_ctz(x);} int ctzll(ull x) {return __builtin_ctzll(x);} int clz(uint x) {return __builtin_clz(x);} int clzll(ull x) {return __builtin_clzll(x);} int popcnt(uint x) {return __builtin_popcount(x);} int popcntll(ull x) {return __builtin_popcountll(x);} int bsr(uint x) {return 31^clz(x);} int bsrll(ull x) {return 63^clzll(x);} #define N 100016 vpii seg; int a[N], d[N]; bool check(int n, int k) { copy_n(a, n+1, d); for (pii p : seg) { int l = p.fir, r = p.sec; for (int i = l; i <= r-k; ++i) { retif(d[i] < 0) false; if (d[i] > 0) d[i+k] += d[i]; } for (int i = r-k+1; i <= r; ++i) { retif(d[i]) false; } } return true; } int main() { fastio(); int n, ub; ll m = 0; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; ub = n; for (int i = 0, j = 1; i < n; i = j++) { ctif(a[i] == 0); ll s = a[i]; while (j < n && a[j] > 0) s += a[j++]; seg.empb(i, j); ub = min(ub, j-i); m = gcd(s, m); } for (int i = n; i > 0; --i) a[i] -= a[i-1]; for (int k = ub; k > 1; --k) { ctif(m%k); retif(check(n, k)) cout << k << '\n', 0; } cout << "1\n"; return 0; } |
English