码迷,mamicode.com
首页 > 其他好文 > 详细

【CodeForces 624D】Array GCD

时间:2016-02-12 00:59:54      阅读:464      评论:0      收藏:0      [点我收藏+]

标签:

You are given array ai of length n. You may consecutively apply two operations to this array:

  • remove some subsegment (continuous subsequence) of length m < n and pay for it m·a coins;
  • change some elements of the array by at most 1, and pay b coins for each change.

Please note that each of operations may be applied at most once (and may be not applied at all) so you can remove only one segment and each number may be changed (increased or decreased) by at most 1. Also note, that you are not allowed to delete the whole array.

Your goal is to calculate the minimum number of coins that you need to spend in order to make the greatest common divisor of the elements of the resulting array be greater than 1.

Input

The first line of the input contains integers na and b (1 ≤ n ≤ 1 000 000, 0 ≤ a, b ≤ 109) — the length of the array, the cost of removing a single element in the first operation and the cost of changing an element, respectively.

The second line contains n integers ai (2 ≤ ai ≤ 109) — elements of the array.

Output

Print a single number — the minimum cost of changes needed to obtain an array, such that the greatest common divisor of all its elements is greater than 1.

Sample test(s)
input
3 1 4
4 2 3
output
1
input
5 3 2
5 17 13 5 6
output
8
input
8 3 4
3 7 5 4 3 12 9 4
output
13
Note

In the first sample the optimal way is to remove number 3 and pay 1 coin for it.

In the second sample you need to remove a segment [17, 13] and then decrease number 6. The cost of these changes is equal to 2·3 + 2 = 8 coins.

题意

给你n个数,可以执行两种操作最多各一次,一是移除一个连续的序列不可移动整个数列,代价是序列长度*a,二是选一些数改变1,即可以有的加一,有的减一,代价是数字个数*b

最终使得剩下所有数字的最大公约数大于1,求最少代价

分析

因为不能移除所有的数,所以a[1]和a[n]至少会有一个留下来,所以要在a[1]-1、a[1]、a[1]+1、a[n]-1、a[n]、a[n]+1六个数的所有质因数里找最大公约数。

 

找一个数t的最大公约数的过程是j从2到根号t,如果t能整除j,j就是一个质因数,存起来,然后t一直除以j,除到不能整除为止,最后判断一下剩下的数如果大于1,那应该就是它本身了,那说明他就是一个素数,也要存起来。存的时候要注意不要重复。这样才不会超时,如果做素数表然后一个个素数去判断是不是它的因数就会超时。

 

找好后,对每个质因数,进行尺扫(还有种方法是DP(待补充)),左边扫到右边,预处理一下前i个数里有几个需要改变(bnum[i]),有几个必须移除的(anum[i])。

扫的时候,移除L到R这段区间的代价为aCost,不移除的代价为bCost,当bCost<=aCost时说明不用移除更划算,于是L更新为R+1,并且存下修改前面的数需要的代价oldbCost,否则计算一下只移除这段区间,其他区间通过修改达到目标,需要多少代价(oldbCost + aCost + ( bnum[n] - bnum[R] ) * b),然后更新答案。算代价的时候如果一个数必须移除,那修改的代价设为无穷。

代码

#include<stdio.h>
#include<algorithm>
#define ll long long
#define N 1000005
using namespace std;
ll n,a,b,m[N],p[N],bnum[N],anum[N],primeFactorNum,minc=1e18;

void savePrime(ll a)
{
    int noSaved=1;
    for(int i=0; i<primeFactorNum && noSaved; i++)
        if(a==p[i])
            noSaved=0;
    if(noSaved)
        p[primeFactorNum++]=a;
}

void FindPrimeFactor(ll a)
{
    for(int i=-1; i<=1; i++)
    {
        int j,t=a+i;
        for(j=2; j*j<=t; j++)
        {
            if(t%j==0)
            {
                savePrime(j);
                while(t%j==0)t/=j;
            }
        }
        if(t>1)
            savePrime(t);
    }
}

ll cost(ll num,ll factor)
{
    if(num%factor==0)
        return 0;
    if( (num+1) % factor && (num-1) % factor )
        return 1e17;//+1或-1都不能整除factor
    return b;
}

void solve(ll factor)
{
    ll L=1,R,bCost=0,aCost=0,oldbCost=0;

    while( cost(m[L],factor) == 0 && L <= n) L++;//左边过滤掉不需要改动的连续序列
    if(L == n+1)
    {
        minc=0;
        return;
    }
    R = L-1;

    bnum[0] = anum[0] = 0;

    for(int i=1; i<=n; i++)
    {
        ll tmp=cost(m[i],factor);

        if(tmp == b)
            bnum[i] = bnum[i-1]+1;
        else
            bnum[i] = bnum[i-1];

        if(tmp == 1e17)
            anum[i] = anum[i-1]+1;
        else
            anum[i] = anum[i-1];
    }

    if(anum[n] == 0)
        minc = min( minc, bnum[n] * b );

    while(R<n)
    {
        aCost+=a;
        R++;
        if(bCost<1e17)
            bCost+=cost(m[R],factor);
        if(bCost<=aCost)
        {
            L=R+1;
            oldbCost+=bCost;
            bCost=aCost=0;
        }
        else
        {
            if(anum[n]-anum[R]==0)
                minc = min( minc , oldbCost + aCost + ( bnum[n] - bnum[R] ) * b );
        }
    }
}
int main()
{
    scanf("%I64d%I64d%I64d",&n,&a,&b);
    for(int i=1; i<=n; i++)
        scanf("%I64d",&m[i]);

    FindPrimeFactor(m[1]);
    FindPrimeFactor(m[n]);

    for(int i=0; i<primeFactorNum && minc; i++)
        solve(p[i]);

    printf("%I64d",minc);
    return 0;
} 

【CodeForces 624D】Array GCD

标签:

原文地址:http://www.cnblogs.com/flipped/p/5186786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!