Kuroni and the Punishment

Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:

You have an array $a$ consisting of $n$ positive integers. An operation consists of choosing an element and either adding $1$ to it or subtracting $1$ from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not $1$. Find the minimum number of operations needed to make the array good.

Unable to match Kuroni’s intellect, the setters failed to solve the problem. Help them escape from Kuroni’s punishment!Input

The first line contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$)  — the number of elements in the array.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$. ($1 \le a_i \le 10^{12}$)  — the elements of the array.Output

Print a single integer  — the minimum number of operations required to make the array good.Examplesinput

3
6 2 4

output

0

input

5
9 8 7 3 1

output

4

Note

In the first example, the first array is already good, since the greatest common divisor of all the elements is $2$.

In the second example, we may apply the following operations:

  1. Add $1$ to the second element, making it equal to $9$.
  2. Subtract $1$ from the third element, making it equal to $6$.
  3. Add $1$ to the fifth element, making it equal to $2$.
  4. Add $1$ to the fifth element again, making it equal to $3$.

The greatest common divisor of all elements will then be equal to $3$, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.

Solution:

#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
return '"' + s + '"';
}

string to_string(const char* s) {
return to_string((string) s);
}

string to_string(bool b) {
return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}

template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
}
return res;
}

template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  auto start = clock();
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
    cin >> a[i];
}
const long long inf = (long long) 1e18;
set<long long> tested;
long long ans = inf;
auto Test = [&](long long x) {
if (tested.find(x) != tested.end()) {
return inf;
}
tested.insert(x);
long long cur = 0;
for (int i = 0; i < n; i++) {
      if (a[i] < x) {
        cur += x - a[i];
      } else {
        long long r = a[i] % x;
        cur += min(r, x - r);
      }
    }
    ans = min(ans, cur);
    return cur;
  };
  Test(2);
  const int MAX = (int) 1e6 + 10;
  vector<bool> prime(MAX, true);
for (int i = 2; i * i < MAX; i++) {
    if (prime[i]) {
      for (int j = i * i; j < MAX; j += i) {
        prime[j] = false;
      }
    }
  }
  vector<int> p;
for (int i = 2; i < MAX; i++) {
    if (prime[i]) {
      p.push_back(i);
    }
  }
  auto TestDiv = [&](long long x) {
    for (int i = 0; i < (int) p.size() && (long long) p[i] * p[i] <= x; i++) {
      if (x % p[i] == 0) {
        while (x % p[i] == 0) {
          x /= p[i];
        }
        Test(p[i]);
      }
    }
    if (x > 1) {
Test(x);
}
};
double q = 1.0 / CLOCKS_PER_SEC;
const double TL = 1.5;
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
while ((clock() - start) * q < TL) {
    int i, j;
    do {
      i = rng() % n;
      j = rng() % n;
    } while (i == j);
    for (int di = -1; di <= 1; di++) {
      if (a[i] + di == 0) {
        continue;
      }
      for (int dj = -1; dj <= 1; dj++) {
        if (a[j] + dj == 0) {
          continue;
        }
        long long g = __gcd(a[i] + di, a[j] + dj);
        TestDiv(g);
      }
    }
  }
  cout << ans << '\n';
  return 0;
}