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. 【社招】来杭州吧,阿里国际UED招前端~~

    来杭州吧,阿里国际UED招前端~~ 依稀记得,几年前在北京的日子,两点一线的生活方式,似乎冲淡模糊了身边的一切,印象最深刻的莫过于北京的地铁站了吧(因为只有等地铁,搭地铁的时候,才能够停下脚步,静静地 ...

  2. Sqlserver日期函数应用

    1.获取当前时间 SELECT  GETDATE() AS '当前日期' ,         DATENAME(year, GETDATE()) AS '年' ,         DATENAME(m ...

  3. POJ 2442 Sequence

    Pro. 1 给定k个有序表,取其中前n小的数字.组成一个新表,求该表? 算法: 由于  a1[1] < a1[2] < a1[3] ... <a1[n] a2[1] < a2 ...

  4. 虚拟机里面安装Openfiler 2.99

    简介 Openfiler 由rPath Linux驱动,它是一个基于浏览器的免费网络存储管理实用程序,可以在单一框架中提供基于文件的网络连接存储 (NAS) 和基于块的存储区域网 (SAN).Open ...

  5. Xcode 5.0.2 下载地址

    下载地址:http://adcdownload.apple.com/Developer_Tools/xcode_5.0.2/xcode_5.0.2.dmg command_line_tools_os_ ...

  6. IOS8修改状态栏颜色

    IOS8修改状态栏颜色 http://blog.csdn.net/suelu/article/details/43701913 使用了storyboard,直接view controller里面设置s ...

  7. [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链

    [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...

  8. 消息通信库ZeroMQ 4.0.4安装指南

    一.ZeroMQ介绍 ZeroMQ是一个开源的消息队列系统,按照官方的定义,它是一个消息通信库,帮助开发者设计分布式和并行的应用程序. 首先,我们需要明白,ZeroMQ不是传统的消息队列系统(比如Ac ...

  9. shell脚本检测局域网内存活主机

    <1> d211 admin # for i in {3..254} ; do ping -c 1 192.168.1.$i &>/dev/null && e ...

  10. Linux: xclip,pbcopy,xsel用法 terminal 复制粘帖 (mac , ubuntu)

    ubuntu下的用户可以只用apt-get来安装: sudo apt-get install xclip echo "Hello, world" | xclip mac下使用pbc ...