This is the harder version of the problem. In this version, 1≤𝑛≤106 and 0≤𝑎𝑖≤106. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems

Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare 𝑛 boxes of chocolate, numbered from 1 to 𝑛. Initially, the 𝑖-th box contains 𝑎𝑖 chocolate pieces.

Since Bob is a typical nice guy, he will not send Alice 𝑛 empty boxes. In other words, at least one of 𝑎1,𝑎2,…,𝑎𝑛 is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer 𝑘>1 such that the number of pieces in each box is divisible by 𝑘. Note that Alice won't mind if there exists some empty boxes.

Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box 𝑖 and put it into either box 𝑖−1 or box 𝑖+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.

链接🔗

「CodeForces 1254B」Send Boxes to Alice

题解

堆数肯定是sumsum的因数,如果 asum,bsum,aba|sum , b |sum , a | b,那么肯定选aa堆数
然后枚nn,因为只有可能将该堆数往后移,或者将后面的堆数往前移这两种可能,所以贪心一下就好

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxd = 1e6+10;
int a[maxd];
long long sum,ans = 1e18;
vector<long long> b;
int main()
{
//    freopen("a.in","r",stdin);
//    freopen("k.out","w",stdout);
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) sum+=a[i];
    if(sum == 1) {printf("-1\n");return 0;}
    for(long long i=2;i*i<=sum;i++)
    {
        if(sum%i==0)
        {
            b.push_back(i);
            sum /= i;
            i--;
        }
    }
    b.push_back(sum);
    for(auto x : b)
    {
        //printf("%d\n",x);
        long long res = 0; sum = 0; // sum 表示当前还未匹配到的数量
        for(int i=1;i<=n;i++)
        {
            sum += a[i];
            sum %= x;
            res += min(sum,x - sum);
            // 可以选择将当前剩下的带到后面一格,或者将还缺多少的部分从后面带回来,因为总数是一定够除以x的
        }
        ans = min(res,ans);
    }
    printf("%lld\n",ans);
    return 0;
}