F. Kuroni and the Punishment
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 aa consisting of nn positive integers. An operation consists of choosing an element and either adding 11 to it or subtracting 11 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 11. 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 nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of elements in the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an. (1≤ai≤10121≤ai≤1012) — the elements of the array.
Output
Print a single integer — the minimum number of operations required to make the array good.
Examples
input
Copy
3 6 2 4
output
Copy
0
input
Copy
5 9 8 7 3 1
output
Copy
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 22.
In the second example, we may apply the following operations:
- Add 11 to the second element, making it equal to 99.
- Subtract 11 from the third element, making it equal to 66.
- Add 11 to the fifth element, making it equal to 22.
- Add 11 to the fifth element again, making it equal to 33.
The greatest common divisor of all elements will then be equal to 33, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
题意:
给你n(<=2e5)个数a[i](<=1e12),在一次操作中,你可以任意选择其中一个数,进行+1或者-1(同一个数可以操作多次)。
问最少操作多少次,使得这些数的gcd>1。
思路:首先很容易发现答案不超过n(使所有数变成偶数)。那么就有(n+1)/2个数操作次数不大于1(+1/-1)
那么gcd就在权值x,x+1,x-1的质因子中。
因 此 我 们 随 机 100 个 位 置
把它们和它们+1/-1进行质因子分解
最后用分解出的每个质因子计算答案即可。
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
const int maxn=1e6+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,p;
ll ans,tmp,cnt,sum,ct;
ll a[maxn],pri[maxn];
vector<int>vc;
vector<ll>c;
inline int read() {
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getchar(); }
return x * f;
}
inline ll red() {
ll x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getchar(); }
return x * f;
}
void init()
{
rep(i,2,maxn-1)
{
if(!pri[i])
{
for(int j=i*2;j<maxn;j+=i)
pri[j]=1;
pri[++cnt]=i;
}
}
}
void div(ll x)
{
//cout<<"debug: "<<x<<" "<<endl;
for(int i=1;i<=cnt;i++)
{
if(pri[i]>x) break;
if(x%pri[i]) continue;
c.push_back(pri[i]);
while(x%pri[i]==0) x/=pri[i];
}
if(x>1) c.push_back(x);
}
void solve(ll k)
{
ll tmp=0;
rep(i,1,n)
{
if(a[i]<=k) tmp+=k-a[i];
else tmp+=min(a[i]%k,k-(a[i]%k));
if(tmp>=ans) return ;
}
if(tmp<ans) ans=tmp;
}
int main()
{
init();
n=read();
rep(i,1,n)
{
a[i]=red();
if(a[i]&1) ans++;
vc.push_back(i);
}
unsigned seed = std::chrono::system_clock::now ().time_since_epoch ().count ();
std::shuffle (vc.begin (), vc.end (), std::default_random_engine (seed));
for(int i=0;i<vc.size();i++)
{
if(i>112) break;
int id=vc[i];
div(a[id]);
div(a[id]+1);
if(a[id]>1) div(a[id]-1);
}
sort(c.begin(),c.end());
c.erase(unique(c.begin(),c.end()),c.end());
for(int j=0;j<c.size();j++) solve(c[j]);
printf("%lld\n",ans);
return 0;
}