任务二

A - Send Boxes to Alice (Easy Version)

https://vjudge.net/contest/345984#problem/A

This is the easier version of the problem. In this version,(1n105and0ai1)

(1n105and0ai1) . You can hack this problem only if you solve and lock 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 n boxes of chocolate, numbered from 11 to nn. Initially, the ithith box contains a1

a1 chocolate pieces.

 

Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a1a2...ana1a2...an is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k>1

k>1 such that the number of pieces in each box is divisible by k. 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 i and put it into either box i1i1 or box i+1

i+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.

Input

 

The first line contains a single integer n(1n105)

n(1n105) — the number of chocolate boxes.

 

The second line contains n integers a1a2...an(0ai1)

a1a2...an(0ai1) — the number of chocolate pieces in the i-th box.

 

It is guaranteed that at least one of a1a2...an

a1a2...an is positive.

Output

 

If there is no way for Charlie to make Alice happy, print 1

1.

 

Otherwise, print a single integer x

x — the minimum number of seconds for Charlie to help Bob make Alice happy.

Examples

input

3
1 0 1

output

2

input

1
1

output

1

题意:

n个盒子,有的有东西有的空,0或1,移动最小的次数,使最后不空的盒子,有相同公约数(质数),就是将a中的某一小段中的所有数字加到其中的一个点上

 

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+10;

int a[N];
int n;

ll solve(ll x){//记录当为x是需要的移动的个数
    ll ans = 0;
    ll sum = 0;
    for(int i = 1;i <= n;i++){
        sum = (sum+a[i])%x;
        ans += min(sum,x-sum);
    }
    return ans;
}
int main(){
    ll sum = 0;
    scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        scanf("%d",a+i);
        sum += a[i];
    }
    if(sum == 0){
        printf("0\n");
        return 0;
    }else if(sum == 1){
        printf("-1\n");
        return 0;
    }
    ll ans = 0x7f7f7f7f7f7f7f7f;
    //求解质因子
    for(ll i = 2;i*i <= sum;i++){
        if(sum%i == 0){
            ans = min(ans,solve(i));
            while(sum%i == 0) sum /= i;//相当于把i的因数全部去掉
        }
    }
    if(sum > 1) ans = min(ans,solve(sum));//防止最后i原来远大i*i>sum,但sum还是质数,因此这个不能掉
    printf("%lld\n",ans);
    return 0;
}
posted @ 2019-12-16 13:05  sqsq  阅读(166)  评论(0编辑  收藏  举报