秒的有点难以理解:https://blog.csdn.net/weixin_42868863/article/details/103200132
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll INF=1e18+7; const ll N=1E6+7; ll arr[N]; ll n; ll cal(ll x){ ll sum=0,s=0; for(ll i=1;i<=n;i++){ s=(s+arr[i])%x; sum+=min(s,x-s); } return sum; } int main(){ ios::sync_with_stdio(0); cin>>n; ll cnt=0; for(ll i=1;i<=n;i++){ cin>>arr[i]; cnt+=arr[i]; } if(cnt==1){ cout<<-1<<endl; return 0; } if(n==1){ cout<<0<<endl; return 0; } ll m=sqrt(cnt); ll sum=INF; for(ll i=2;i<=m;i++){ if(cnt%i==0){ sum=min(sum,cal(i)); while(cnt%i==0) cnt/=i; } } if(cnt!=1) sum=min(sum,cal(cnt)); cout<<sum<<endl; return 0; }