#include <bits/stdc++.h>
using namespace std;
const int MAX_N=100005;
int a[MAX_N];
long long x[MAX_N];
bool check(int k, int n, long long s)
{
if(s%k!=0)
{
return false;
}
for(int i=1; i<=n; i++)
{
long long diff=(long long)a[i]-a[i-1];
x[i]=diff+(i>k ? x[i-k] : 0);
if(x[i]<0)
{
return false;
}
if(i>n-k+1 && x[i]!=0)
{
return false;
}
}
return true;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
long long total=0;
a[0]=0;
for(int i=1; i<=n; i++)
{
cin>>a[i];
total+=a[i];
}
int l=1;
int r=1;
while(l<n && a[l+1]>=a[l])
{
l++;
}
while(r<n && a[n-r]>=a[n-r+1])
{
r++;
}
int possible=min({n,l,r});
vector<int> candidates;
for(long long d=1; d*d<=total; d++)
{
if(total%d==0)
{
if(d<=(long long)possible)
{
candidates.push_back((int)d);
}
if(total/d<=(long long)possible)
{
candidates.push_back((int)(total/d));
}
}
}
sort(candidates.rbegin(),candidates.rend());
candidates.erase(unique(candidates.begin(),candidates.end()),candidates.end());
for(int k : candidates)
{
if(check(k,n,total))
{
cout<<k<<endl;
return 0;
}
}
cout<<1<<endl;
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 | #include <bits/stdc++.h> using namespace std; const int MAX_N=100005; int a[MAX_N]; long long x[MAX_N]; bool check(int k, int n, long long s) { if(s%k!=0) { return false; } for(int i=1; i<=n; i++) { long long diff=(long long)a[i]-a[i-1]; x[i]=diff+(i>k ? x[i-k] : 0); if(x[i]<0) { return false; } if(i>n-k+1 && x[i]!=0) { return false; } } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; long long total=0; a[0]=0; for(int i=1; i<=n; i++) { cin>>a[i]; total+=a[i]; } int l=1; int r=1; while(l<n && a[l+1]>=a[l]) { l++; } while(r<n && a[n-r]>=a[n-r+1]) { r++; } int possible=min({n,l,r}); vector<int> candidates; for(long long d=1; d*d<=total; d++) { if(total%d==0) { if(d<=(long long)possible) { candidates.push_back((int)d); } if(total/d<=(long long)possible) { candidates.push_back((int)(total/d)); } } } sort(candidates.rbegin(),candidates.rend()); candidates.erase(unique(candidates.begin(),candidates.end()),candidates.end()); for(int k : candidates) { if(check(k,n,total)) { cout<<k<<endl; return 0; } } cout<<1<<endl; return 0; } |
English