E2. Send Boxes to Alice (Hard Version)

Posted accepting

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E2. Send Boxes to Alice (Hard Version)相关的知识,希望对你有一定的参考价值。

秒的有点难以理解: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;
}

 

以上是关于E2. Send Boxes to Alice (Hard Version)的主要内容,如果未能解决你的问题,请参考以下文章

E1. Send Boxes to Alice (Easy Version)

Codeforces Round #601 (Div. 2) E1 Send Boxes to Alice (Easy Version)

Kattis - boxes (LCA)

Leetcode 1769. Minimum Number of Operations to Move All Balls to Each Box

UVALive - 7042 The Problem to Make You Happy 博弈

2018/11/12 send a letter to send to my apologies to one of my theacher beacuse I were late