标签:style bit 优化 printf cto its def include ORC
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5+10; int a[N]; int n; bool prime(int x) {//判断是否为质数 for(int i = 2; i*i <= x; i++) { if(x%i == 0) return false; } return true; } ll solve(int x) { vector<int>b; ll ans = 0; for(int i = 1; i <= n; i++) { if(a[i] == 1 && b.size() < x) b.push_back(i);//存放位置 if(b.size() == x) { for(int j = 0; j < b.size(); j++) { ans += (ll)abs(b[j]-b[x/2]); } b.clear(); } } return ans; } int main() { int sum = 0; scanf("%d",&n); for(int i = 1; i <= n; i++) { scanf("%d",a+i); sum += a[i]; } if(sum == 1) { printf("-1\n"); return 0; } ll ans = 0x3f3f3f3f3f; for(int i = 2; i <= sum; i++) { if(sum%i==0&&prime(i)) {//此处用质因子优化,不加优化也行。 ans = min(ans,solve(i)); } } printf("%lld\n",ans); return 0; }
Codeforces Round #601 (Div. 2) E1 Send Boxes to Alice (Easy Version)
标签:style bit 优化 printf cto its def include ORC
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11946511.html