CF1305F. Kuroni and the Punishment

给定一个序列,每次可以使$a_i+1,a_i-1$

求最小操作数使得$gcd(a_1,a_2…a_n)>1$

显然$gcd=2$,$ans<n$

假设有$k$个数,$+2,-2$,$2k<n,k<n/2$

所以可以知道一定有$n/2$个数$+1,-1,0$
随取选取一个数枚举他的质因数$n\log a_i$
如果实验次数为$m$,失败概率=$1-\frac{1}{2^m}$

代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>

#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
const int M = 70;
const int mod = 1e9 + 7;
int n;
ll a[N];
ll ans = N;
ll calc(ll x)
{
ll res = 0;
for (int i = 1; i <= n; i++)
{
if (a[i] < x)
res += x - a[i] % x;
else
res += min(a[i] % x, x - a[i] % x);
if (res > ans)
break;
}
return res;
}
void solve(ll x)
{

for (ll i = 2; i * i <= x; i++)
{
if (x % i == 0)
{
while (x % i == 0)
x /= i;
ans = min(calc(i), ans);
}
}
if (x > 1)
ans = min(calc(x), ans);
}
int main()
{
scanf("%d", &n);
srand(time(0));
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);

for (int i = 1; i <= M; i++)
{
ll x = a[1ll * rand() * rand() % n + 1];
//cout << rand() % n + 1 << endl;
solve(x + 1);
solve(x);
if (x > 1)
solve(x - 1);
}
printf("%lld\n", ans);
}