D. Array GCD
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.
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.
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.
3 1 4
4 2 3
1
5 3 2
5 17 13 5 6
8
8 3 4
3 7 5 4 3 12 9 4
13
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的更多相关文章
- 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 ...
- 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 ...
- 【CodeForces 624D】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...
- 【CodeForces 624D/623B】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...
- Array GCD CodeForces - 624D (dp,gcd)
大意: 给定序列, 给定常数a,b, 两种操作, (1)任选一个长为$t$的子区间删除(不能全部删除), 花费t*a. (2)任选$t$个元素+1/-1, 花费t*b. 求使整个序列gcd>1的 ...
- Codeforces AIM Tech Round (Div. 2)
这是我第一次完整地参加codeforces的比赛! 成绩 news standings中第50. 我觉这个成绩不太好.我前半小时就过了前三题,但后面的两题不难,却乱搞了1.5h都没有什么结果,然后在等 ...
- “《编程珠玑》(第2版)第2章”:B题(向量旋转)
B题是这样子的: 将一个n元一维向量向左旋转(即循环移位)i个位置.例如,当n=8且i=3时,向量abcdefgh旋转为defghabc.简单的代码使用一个n元的中间向量在n步内完成该工作.你能否仅使 ...
- 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 ...
- [bzoj2301]Problem b莫比乌斯反演+分块优化
题意: $\sum\limits_{\begin{array}{*{20}{c}}{a < = x < = b}\\{c < = y < = d}\end{array}} {\ ...
随机推荐
- c/c++指针
指针主要分: 指向单一的某个对象/变量, 用于多态或函数指针, 这个不难 - 一级指针 其次是指向数组, 用来操作/遍历数组元素 - 一级/二级指针 指向数组的一级指针很简单了: p指向的是数组的元素 ...
- 搭建openfire Android 客户端学习和开发【二】spark源码导入eclipse
首先声明下 这是我在eoe上转载的 写的很好就摘抄了... 第一步 下载源码 svn下载,下载地址:spark:http://svn.igniterealtime.org/svn/repos/spar ...
- [Effective JavaScript 笔记] 第7条:视字符串为16位的代码单元序列
Unicode编码,基础:它为世界上所有的文字系统的每个字符单位分配一个唯一的整数,该整数介于0~1114111之间,在Unicode术语中称为代码点(code point). 和其它字符编码几乎没有 ...
- 部署细节回忆录(包括了nginx重启)
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) $cap -T $cap deploy:setup $cap deploy (遇 ...
- Tushare的安装
TuShare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采集.清洗加工到数据存储的过程,能够为金融分析人员提供快速.整洁.和多样的便于分析的数据. 考虑到python ...
- asp.net 网站 或者web Api 发布
asp.net 发布iis时可能遇到的内部服务错误常见的有两种: 1.如下图,500.19 Internal Server Error(内部服务错误) 这种错误可能是由于本机的注册表中的asp.net ...
- 解决ntp的错误 no server suitable for synchronization found
当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个: 错误1.Server dropped: ...
- C/C++程序终止时执行的函数——atexit()函数详解
很多时候我们需要在程序退出的时候做一些诸如释放资源的操作,但程序退出的方式有很多种,比如main()函数运行结束.在程序的某个地方用exit()结束程序.用户通过Ctrl+C或Ctrl+break操作 ...
- linux下复制一个文件的内容到另一个文件
cat path/to/file/filename1 >> path/to/file/filename2 例如: cat id_rsa.pub >> ~/.ssh/author ...
- 【转】基于LDA的Topic Model变形
转载自wentingtu 基于LDA的Topic Model变形最近几年来,随着LDA的产生和发展,涌现出了一批搞Topic Model的牛人.我主要关注了下面这位大牛和他的学生:David M. B ...