简化版https://blog.csdn.net/qq_33831360/article/details/103170669
就是数据变大了
优化:k不是sqrt枚举,因为因子nk(个一组)结果可能不比因子k(个一组)好,所以分解质因数枚举质因子k
枚举的方法:移动到点i后,积累了had
#include
#include
#include
#include
using namespace std;
#define int long long
int n;
typedef long long LL;
LL a[1000003];
LL calc(int x) {
LL ans = 0;
LL had = 0;
for (int i = 1; i <= n; i++) {
had = had+a[i];
if (had > x) had %= x;
ans += min(had,x-had);
}
return ans;
}
main() {
LL sum = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%I64d",&a[i]);
sum += a[i];
}
if (sum <= 1) {
puts("-1");
return 0;
}
LL ans = (LL)1e18;
for (LL i = 2; i <= sqrt(sum); i++)
if (sum % i == 0) {
ans = min(ans,calc(i));
while (sum%i == 0) sum /= i;
}
if (sum > 1) ans = min(ans,calc(sum));
cout << ans;
return 0;
}