传送门
文章目录
- 题意:
- 思路:
题意:
大体题意跟easyeasyeasy版本差不多,就是hardhardhard版本的aaa范围更大。见这里Codeforces Round #601 (Div. 2)
思路:
首先还是考虑质因子分解,因为一个数被分成以xyxyxy为一组一定不如分成xxx组或者yyy组,所以只需要以质因子分组就好啦。
让后贪心的考虑,假设当前质因子为basebasebase,那么我们肯定是把当前这个数的a[i]modbasea[i]\bmod basea[i]modbase给a[i+1]a[i+1]a[i+1]或者从a[i+1]a[i+1]a[i+1]获得base−a[i]modbasebase-a[i]\bmod basebase−a[i]modbase,这样就可以忽略前面的数,把a[i+1]a[i+1]a[i+1]当成第一个数。那么第一个式子列出来就是a[i+1]=(a[i+1]+a[i])modbasea[i+1]=(a[i+1]+a[i])\bmod basea[i+1]=(a[i+1]+a[i])modbase,第二个式子列出来就是a[i+1]=(a[i+1]−(base−a[i]modbase))modbasea[i+1]=(a[i+1]-(base-a[i]\bmod base))\bmod basea[i+1]=(a[i+1]−(base−a[i]modbase))modbase,让后化简一下就是a[i+1]=(a[i+1]+a[i])modbasea[i+1]=(a[i+1]+a[i])\bmod basea[i+1]=(a[i+1]+a[i])modbase,让后这样递推下去就好啦。对于每个数我们只需要两种情况取minminmin即可,也就是min(pre,base−pre)min(pre,base-pre)min(pre,base−pre),preprepre为模basebasebase情况下的前缀和。
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
int a[N];
LL sum,ans=1e18;void solve(LL base)
{LL as=0,pre=0;for(int i=1;i<=n;i++) pre=(pre+a[i])%base,as+=min(pre,base-pre);ans=min(ans,as);
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];if(sum==1) { puts("-1"); return 0; }for(LL i=2;i<=sum/i;i++)if(sum%i==0){solve(i);while(sum%i==0) sum/=i;}if(sum>1) solve(sum);cout<<ans<<endl;return 0;
}
/**/