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. 安装lnmp后,忘记phpmyadmin的root密码,怎么办

    如果忘记MySQL root密码,如何重设密码?执行如下命令:wget http://soft.vpser.net/lnmp/ext/reset_mysql_root_password.sh;sh r ...

  2. VS的工程链接优化的问题

    打算在项目中试试 CATCH 这个测试框架.请同事在工程中进行了试验,结果却出现了一点问题. CATCH 和 GTest 之类的框架一样,可以直接在 C++ 文件中定义测试函数,就能自动地注册到测试列 ...

  3. 用JSON-server模拟REST API(一) 安装运行

    用JSON-server模拟REST API(一) 安装运行 在开发过程中,前后端不论是否分离,接口多半是滞后于页面开发的.所以建立一个REST风格的API接口,给前端页面提供虚拟的数据,是非常有必要 ...

  4. 记Flume-NG一些注意事项(不定时更新,欢迎提供信息)

    这里只考虑flume本身的一些东西,对于JVM.HDFS.HBase等得暂不涉及.... 一.关于Source: 1.spool-source:适合静态文件,即文件本身不是动态变化的: 2.avro ...

  5. [Effective JavaScript 笔记]第48条:避免在枚举期间修改对象

    注册列表示例 一个社交网络有一组成员,每个成员有一个存储其朋友信息的注册列表. function Member(name){ this.name=name; this.friends=[]; } va ...

  6. 裸设备和Oracle问答20例

    导读裸设备,也叫裸分区(原始分区),是一种没有经过格式化,不被Unix通过文件系统来读取的特殊字符设备.裸设备可以绑定一个分区,也可以绑定一个磁盘.本文收集裸设备和Oracle问答20例. 1.什么叫 ...

  7. 默认hosts后面为files dns

    售后工程师 : 您好,问题已经解决,问题原因是您修改了/etc/nsswitch.conf配置文件中的hosts:这项导致的,默认hosts后面为files dns,但是后面去掉了DNS导致直接使用本 ...

  8. 【Python】Django Model 怎么使用 UUID 作为主键?

    >>> import uuidprint uuid.uuid3(uuid.uuid1(), 'python.org') >>> # make a UUID base ...

  9. 【leetcode】4Sum

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  10. 现在, Delphi 的多线程已经非常易用了!

    先看一个非多线程的例子, 代码执行时不能进行其它操作(譬如拖动窗体): {自定义方法: 在窗体上绘制...} procedure MyMethod; var   i: Integer; begin   ...