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 to2·3 + 2 = 8 coins.

 #include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
int a[maxn];
long long A,B;
int n;
long long ans=1e16;
vector<int>p;
long long g1[maxn];
long long g2[maxn];
long long g3[maxn];
void f(int x)
{
for(int i=;i*i<=x;i++)
{
if(x%i==)
{
p.push_back(i);
while(x%i==)
x/=i;
}
}
if(x!=) p.push_back(x);
}
void solve (int x)
{
memset(g1,,sizeof(g1));
memset(g2,,sizeof(g2));
memset(g3,,sizeof(g3));
for(int i=;i<=n;i++)
{
if(a[i]%x==) g1[i]=g1[i-];
else if((a[i]+)%x==||(a[i]-)%x==) g1[i]=g1[i-]+B;
else g1[i]=1e16; }
for(int i=n;i>=;i--)
{
if(a[i]%x==)g2[i]=g2[i+];
else if((a[i]-)%x==||(a[i]+)%x==) g2[i]=g2[i+]+B;
else g2[i]=1e16;
}
g3[]=1e16;
for(int i=;i<=n;i++)
{
g3[i]=g1[i]-(i+)*A;
g3[i]=min(g3[i],g3[i-]);
}
for(int i=;i<=n;i++)
{
ans=min(ans,g2[i]+(i-)*A);
ans=min(ans,g1[i]+(n-i)*A);
}
for(int i=;i<=n+;i++)
{
ans=min(ans,g3[i-]+g2[i]+A*i);
}
}
int main()
{
scanf("%d%ld%ld",&n,&A,&B);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=-;i<=;i++)
f(a[]+i),f(a[n]+i);
sort(p.begin(),p.end());
p.erase(unique(p.begin(),p.end()),p.end());
for(int i=;i<p.size();i++)
solve(p[i]);
printf("%I64d\n",ans);
return ;
}

D. Array GCD的更多相关文章

  1. AIM Tech Round (Div. 2) D. Array GCD dp

    D. Array GCD 题目连接: http://codeforces.com/contest/624/problem/D Description You are given array ai of ...

  2. Codeforces 623B Array GCD

    Array GCD 最后的序列里肯定有a[1], a[1]-1, a[1]+1, a[n], a[n]-1, a[n]+1中的一个,枚举质因子, dp去check #include<bits/s ...

  3. 【CodeForces 624D】Array GCD

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

  4. 【CodeForces 624D/623B】Array GCD

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

  5. Array GCD CodeForces - 624D (dp,gcd)

    大意: 给定序列, 给定常数a,b, 两种操作, (1)任选一个长为$t$的子区间删除(不能全部删除), 花费t*a. (2)任选$t$个元素+1/-1, 花费t*b. 求使整个序列gcd>1的 ...

  6. Codeforces AIM Tech Round (Div. 2)

    这是我第一次完整地参加codeforces的比赛! 成绩 news standings中第50. 我觉这个成绩不太好.我前半小时就过了前三题,但后面的两题不难,却乱搞了1.5h都没有什么结果,然后在等 ...

  7. “《编程珠玑》(第2版)第2章”:B题(向量旋转)

    B题是这样子的: 将一个n元一维向量向左旋转(即循环移位)i个位置.例如,当n=8且i=3时,向量abcdefgh旋转为defghabc.简单的代码使用一个n元的中间向量在n步内完成该工作.你能否仅使 ...

  8. 914. X of a Kind in a Deck of Cards

    In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...

  9. [bzoj2301]Problem b莫比乌斯反演+分块优化

    题意: $\sum\limits_{\begin{array}{*{20}{c}}{a < = x < = b}\\{c < = y < = d}\end{array}} {\ ...

随机推荐

  1. vim设置语法高亮

    在vim安装目录中的_vimrc修改,加上以下的代码. set nu! colorscheme desert      syntax enable      syntax on

  2. javascript中parentNode,childNodes,children的应用详解

    本篇文章是对javascript中parentNode,childNodes,children的应用进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助   "parentNode&qu ...

  3. unity3d 截屏

    原地址:http://www.cnblogs.com/88999660/archive/2013/01/21/2869747.html void OnGUI(){ if(GUI.Button(new ...

  4. NSArray和NSMutableArray

    //1. NSArray EOItems *eOItems = [[EOItems alloc] init]; eOItems.ID = [NSNumber numberWithInt:]; NSAr ...

  5. [POJ1338]Ugly Numbers

    [POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...

  6. [ZJOI3527][Zjoi2014]力

    [ZJOI3527][Zjoi2014]力 试题描述 给出n个数qi,给出Fj的定义如下: 令Ei=Fi/qi.试求Ei. 输入 包含一个整数n,接下来n行每行输入一个数,第i行表示qi. 输出 有n ...

  7. Dmaven.multiModuleProjectDirectory system propery is not set.

    eclipse中使用maven插件的时候,运行run as maven build的时候报错-Dmaven.multiModuleProjectDirectory system propery is ...

  8. ASP.NET MVC 的URL路由介绍

    在这个教程中,向你介绍每个ASP.NET MVC一个重要的特点叫做URL路由.URL路由模块是负责映射从浏览器请求到特定的控制器动作. 在教程的第一部分,你将学习标准路由表如何映射到控制器的动作.在教 ...

  9. iOS7 中的新加入的下载类NSURLSession(随ios版本更新而更新)

    想详细的了解网络下载的相关知识,要仔细阅读URL Loading System Programming Guide 这里有篇好文章(http://www.shinobicontrols.com/blo ...

  10. 用php计算行列式

    因为有课程设计要计算多元一次方程组,所以想编个程序实现,多元一次方程组的计算最系统的方法就是利用克拉默法则求解方程组,所以只需要编写一个类或者方法求出多元一次方程组系数行列式的值和和其他几个行列式,如 ...