D. Array GCD
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 n, a 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.

Examples
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.


【题意】有两种操作,每种只能用一次,第一种对于一段连续区间进行移除(不能全删完),代价是长度*a,第二种是对于一些数进行+1或者-1,使得最后的剩余的最大公约数大于1。

【分析】关键要发现一个关键点,删除的序列是连续的并且不能全部删除。那么显然a[1]和a[n]肯定会留下一个。所以最终的gcd是整除a1,a1+1,a1-1或者an,an+1,an-1的。素因子就很少了,接下来就可以暴力dp或者two pointers了。我用的dp。

              dp[i][3]代表要删除的连续的状态。dp[i][0]代表没有开始,dp[i][1]代表删除的过程中,dp[i][2]代表删除结束。转移就比较简单了,具体看代码的转移吧。

             复杂度是log10^9*n的。

【AC 代码】

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1000003;
LL dp[maxn][3];
int n,a[maxn];
LL cost1,cost2;
int cnt,vis[maxn],prime[maxn],fac[maxn];
void init()
{
    cnt=0;
    for(int i=2; i<=maxn; i++){
        if(!vis[i]) prime[cnt++]=i;
        for(int j=0; j<cnt&&prime[j]*i<maxn; j++){
            vis[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int getfactor(int x)
{
    int t=0;
    for(int j=0; j<cnt&&x>=prime[j]; j++){
        if(x%prime[j]==0){
            fac[t++]=prime[j];
            while(x%prime[j]==0) x/=prime[j];
        }
    }
    if(x!=1) fac[t++]=x;
    return t;
}
LL solve(int l,int r,int x)
{
    memset(dp,0x3f,sizeof(dp));
    dp[l-1][0]=0;
    for(int i=l; i<=r; i++){
        dp[i][1]=min(dp[i-1][0],dp[i-1][1])+cost1;
        if(a[i]%x==0){
            dp[i][0]=dp[i-1][0];
            dp[i][2]=min(dp[i-1][1],dp[i-1][2]);
        }else{
            if((a[i]+1)%x==0||(a[i]-1)%x==0){
                dp[i][0]=dp[i-1][0]+cost2;
                dp[i][2]=min(dp[i-1][1],dp[i-1][2])+cost2;
            }
        }
    }
    return min(min(dp[r][0],dp[r][1]),dp[r][2]);
}
int main()
{
    init();
    scanf("%d%I64d%I64d",&n,&cost1,&cost2);
    for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    LL ans=(1LL)*n*cost1;
    for(int i=-1; i<=1; i++){
        LL cost=i==0?0:cost2;
        int cap=getfactor(a[n]+i);
        for(int j=0; j<cap; j++){
            ans = min(ans,solve(1,n-1,fac[j])+cost);
        }
        cap=getfactor(a[1]+i);
        for(int j=0; j<cap; j++){
            ans = min(ans,solve(2,n,fac[j])+cost);
        }
    }
    printf("%I64d\n",ans);
    return 0;
}