ll a[N],n;
vector<ll> fac;
set<ll> st;
void get_fac(ll x)
{
for (ll i = 2;i*i <= x;i++)
{
if (x%i == 0)
{
fac.emplace_back(i);
while (x%i == 0)x /= i;
}
}
if (x > 1)fac.emplace_back(x);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
cin >> n;
f(i, 1, n)scanf("%lld", &a[i]);
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<> dis(1, n);
int num = 0, top = min((ll)30, n);
while (num < top)
{
int now = dis(gen);
st.insert(a[now]);
st.insert(a[now] + 1);
if (a[now] > 2)st.insert(a[now] - 1);
num++;
}
for (auto i : st)get_fac(i);
sort(fac.begin(), fac.end());
auto it=unique(fac.begin(), fac.end());
fac.erase(it, fac.end());
ll mi = n;
for (auto I : fac)
{
ll num = 0;
f(i, 1, n)
{
if (a[i] < I)num += I - a[i] % I;
else num += min(I - a[i] % I, a[i] % I);
if (num > mi)break;
}
mi = min(num, mi);
}
cout << mi << endl;
return 0;
}
F. Kuroni and the Punishment(随机化)
最新推荐文章于 2021-05-10 22:21:32 发布