Ozon Tech Challenge 2020 (Div.1 + Div.2) F. Kuroni and the Punishment 随机化算法

time limit per test2.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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≤n≤2⋅1e5) — the number of elements in the array.

The second line contains n integers a1,a2,…,an. (1≤ai≤1e12) — the elements of the array.

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

Examples
inputCopy
3
6 2 4
outputCopy
0
inputCopy
5
9 8 7 3 1
outputCopy
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:

Add 1 to the second element, making it equal to 9.
Subtract 1 from the third element, making it equal to 6.
Add 1 to the fifth element, making it equal to 2.
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.

题意:给你n个数,每次可以加 1 或者减 1 ,但是必须保证每次操作后这些数都还是正整数,问最少操作多少次能使这n个数的gcd不为1。
也不算是什么正确的算法,一个随机化的算法,大概也可能是正解。
之前很少接触随机化算法,所以还是存一下。

主要学习思想。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<set>
#define ll long long
using namespace std;
const int maxn=1000100;
ll a[maxn],ans;
set<ll>se;
int prime[maxn],cnt;
bool ha[maxn];
int n;

void Prime(void)
{
    ha[1]=true;
    for(int i=2;i<maxn;i++)
    {
        if(!ha[i]) prime[cnt++]=i;
        for(int j=0;j<cnt&&prime[j]*i<maxn;j++)
        {
            ha[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }
}


void doit(ll now)
{
    ll res=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]<=now) res+=now-a[i];
        else
        {
            ll pr=a[i]%now;
            res+=min(pr,now-pr);
        }
    }
    ans=min(ans,res);
    //cout<<"now:  "<<now<<" res:  "<<res<<endl;
}

void check(ll now)
{
    if(now==0) return ;
    for(int i=0;i<cnt&&1ll*prime[i]*prime[i]<=now;i++)
    {
        if(now%prime[i]) continue;
        if(se.find(prime[i])==se.end())
            doit(prime[i]),se.insert(prime[i]);
        while(now%prime[i]==0)
            now/=prime[i];
    }
    if(now>1)
    {
        if(se.find(now)==se.end())
            doit(now),se.insert(now);
    }
}


int main(void)
{
    Prime();
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    ans=n;
    srand(19981031);
    for(int i=1;i<=15;i++)
    {
        int pos=((rand()<<15)|(rand()))%n+1;
        //cout<<"pos:  "<<pos<<"  a:  "<<a[pos]<<endl;
        check(a[pos]);

        //check  a[pos]+1 和 a[pos]-1 很有必要
        check(a[pos]+1);
        check(a[pos]-1);
    }
    printf("%lld\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值