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}} {\ ...
随机推荐
- (转)win32Application和win32ApplicationConsole
这几天在创建MFC项目时,常常遇到一下两个连接错误,例如: 1. LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _ma ...
- Telnet是什么?
Telnet协议是 用途一: 测试端口号 例子:测试本机的80端口是否打开 1.获得本机IP dos命令:ipconfig 可以看到本机的IP地址是192.168.0.102,当然这也是局域网IP,并 ...
- 2013 ACM/ICPC Asia Regional Chengdu Online 1004 Minimum palindrome
Minimum palindrome Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 微信新版支持读取iPhone M7/M8协处理器运动数据 与好友PK一下运动量吧
iPhone的创新是有目共睹的,Healthkit的推出预示着苹果进军健康领域,iPhone M7/M8协处理器可以收集和分析用户的健康数据,那么好的硬件自然不会被势在打造完整生态圈的微信给错过,这不 ...
- 二叉树建立,先序、中序、后序遍历(c实现)
建立树ABC##DE#G##F###,输出 #include <stdio.h> #include <stdlib.h> #define ElemType char //节点声 ...
- HDU1068 最大独立点集
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- OpenStack 的Nova组件详解
Open Stack Compute Infrastructure (Nova) Nova是OpenStack云中的计算组织控制器.支持OpenStack云中实例(instances)生命周期的所有活 ...
- Linux 怎么重命名多个文件
下面是重命名命令的基本语法. rename [-v -n -f] <pcre> <files> <pcre> 是Perl兼容正则表达式,它表示的是要重命名的文件和该 ...
- c++11 gcc4.8.x安装
c++11 gcc4.8.x安装 2014年12月11日默北 要安装PHP-CPP,需要c++11,否则就会报错,错误信息如下所示: g++ -Wall -c -g -std=c++11 -fpi ...
- ASP.NET MVC 入门系列教程
ASP.NET MVC 入门系列教程 博客园ASP.NET MVC 技术专题 http://kb.cnblogs.com/zt/mvc/ 一个居于ASP.NET MVC Beta的系列入门文章,有朋友 ...