Codeforces Round #601 (Div. 2) E1 Send Boxes to Alice (Easy Version)
2024-10-08 09:59:26
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+;
int a[N];
int n;
bool prime(int x) {//判断是否为质数
for(int i = ; i*i <= x; i++) {
if(x%i == ) return false;
}
return true;
}
ll solve(int x) {
vector<int>b;
ll ans = ;
for(int i = ; i <= n; i++) {
if(a[i] == && b.size() < x) b.push_back(i);//存放位置
if(b.size() == x) {
for(int j = ; j < b.size(); j++) {
ans += (ll)abs(b[j]-b[x/]);
}
b.clear();
}
}
return ans;
}
int main() {
int sum = ;
scanf("%d",&n);
for(int i = ; i <= n; i++) {
scanf("%d",a+i);
sum += a[i];
}
if(sum == ) {
printf("-1\n");
return ;
}
ll ans = 0x3f3f3f3f3f;
for(int i = ; i <= sum; i++) {
if(sum%i==&&prime(i)) {//此处用质因子优化,不加优化也行。
ans = min(ans,solve(i));
}
}
printf("%lld\n",ans);
return ;
}
最新文章
- USACO翻译:USACO 2012 JAN三题(1)
- C#预编译指令之#region
- Spring IOC容器中注入bean
- atitit查询表修改表字段没反应--解锁锁定的表
- 让Tomcat支持中文文件名
- Oracle自动增长的序列号
- Web页面布局方式小结
- centos7.2中文乱码解决办法
- UE4实现描边效果
- CentOS 7 常用命令大全
- C# 视频多人脸识别的实现
- Leetcode 题解 First Missing Positive
- Docker 指定容量
- python正则表达式贪婪与非贪婪模式
- mybatis 于 hibernate区别
- node.js学习之post文件上传 (multer中间件)
- maven的部署安装
- 使用Django完成CRM管理系统
- setsockopt 详解
- 作业派NABCD的特点分析