#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define VAR(v,w) __typeof(w) v=(w)
#define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it)
#define PB push_back
#define ALL(c) (c).begin(),(c).end()
#define MP make_pair
#define FT first
#define SD second
#define INT(x) int x; scanf("%d", &x)
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<LL> VLL;
int a[100000];
VLL allFactors(LL x) {
VLL r1, r2;
for (int d = 1; LL(d) * d <= x; ++d) {
if (!(x % d)) {
r1.PB(d);
r2.PB(x / d);
}
}
if (r1.back() == r2.back()) r2.pop_back();
reverse(ALL(r2));
FORE(it,r2) r1.PB(*it);
return r1;
}
int main() {
INT(n);
REP(i,n) scanf("%d", &a[i]);
LL s = 0;
REP(i,n) s += a[i];
VLL d = allFactors(s);
reverse(ALL(d));
FORE(it,d) {
if (*it > n) continue;
int x = 0;
queue<PII> q;
bool ok = 1;
REP(i,n) {
while (!q.empty() && q.front().FT == i) {
x -= q.front().SD;
q.pop();
}
int j = i + *it;
if (a[i] < x || (j > n && a[i] > x)) {
ok = 0;
break;
}
q.push(MP(j, a[i] - x));
x = a[i];
}
if (ok) {
printf("%d\n", int(*it));
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 | #include <algorithm> #include <cstdio> #include <queue> #include <utility> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef long long LL; typedef pair<int,int> PII; typedef vector<LL> VLL; int a[100000]; VLL allFactors(LL x) { VLL r1, r2; for (int d = 1; LL(d) * d <= x; ++d) { if (!(x % d)) { r1.PB(d); r2.PB(x / d); } } if (r1.back() == r2.back()) r2.pop_back(); reverse(ALL(r2)); FORE(it,r2) r1.PB(*it); return r1; } int main() { INT(n); REP(i,n) scanf("%d", &a[i]); LL s = 0; REP(i,n) s += a[i]; VLL d = allFactors(s); reverse(ALL(d)); FORE(it,d) { if (*it > n) continue; int x = 0; queue<PII> q; bool ok = 1; REP(i,n) { while (!q.empty() && q.front().FT == i) { x -= q.front().SD; q.pop(); } int j = i + *it; if (a[i] < x || (j > n && a[i] > x)) { ok = 0; break; } q.push(MP(j, a[i] - x)); x = a[i]; } if (ok) { printf("%d\n", int(*it)); return 0; } } } |
English