D. Array GCD

题目连接:

http://codeforces.com/contest/624/problem/D

Description

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 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.

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 Input

3 1 4

4 2 3

Sample Output

1

Hint

题意

有n个数,你可以花费i*a去删除长度i的线段,也可以花费B去让一个数+-1,但是删除操作只能进行一次,+-1对一个数也只能操作一次

并且删除操作不能删除所有的数

问你最小花费多少,可以使得剩下的数的gcd不等于1

题解:

很显然,因为不能删除完,所以必然第一个数和最后一个数会剩下来

所以我们暴力枚举第一个数和最后一个数的质因子就好了

然后开始跑dp

显然ans = f[i] + (j-i-1)*a + g[i],f[i]是前缀只修改b的最小值,g[i]是后缀只修改b的最小值

然后我们维护一下f[i]+(i-1)*a的前缀最小值,再暴力枚举g[i]就好了

代码

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

AIM Tech Round (Div. 2) D. Array GCD dp的更多相关文章

  1. Codeforces AIM Tech Round (Div. 2)

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

  2. AIM Tech Round (Div. 1) D. Birthday 数学 暴力

    D. Birthday 题目连接: http://www.codeforces.com/contest/623/problem/D Description A MIPT student named M ...

  3. AIM Tech Round (Div. 2)——ABCD

    http://codeforces.com/contest/624 A.python是用来写div2的AB题的... a, b, x, y = map(float, raw_input().split ...

  4. AIM Tech Round (Div. 2) C. Graph and String 二分图染色

    C. Graph and String 题目连接: http://codeforces.com/contest/624/problem/C Description One day student Va ...

  5. AIM Tech Round (Div. 2) B. Making a String 贪心

    B. Making a String 题目连接: http://codeforces.com/contest/624/problem/B Description You are given an al ...

  6. AIM Tech Round (Div. 2) A. Save Luke 水题

    A. Save Luke 题目连接: http://codeforces.com/contest/624/problem/A Description Luke Skywalker got locked ...

  7. AIM Tech Round (Div. 2) B

    B. Making a String time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. AIM Tech Round (Div. 2) A

    A. Save Luke time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. AIM Tech Round (Div. 1) C. Electric Charges 二分

    C. Electric Charges 题目连接: http://www.codeforces.com/contest/623/problem/C Description Programmer Sas ...

随机推荐

  1. Yii系列教程(二):功能简介

    1 MVC架构 1.1处理流程 一个Web请求在Yii内部的执行流程如下图所示: 1.2组件角色 组件名 角色与责任 index.php 入口脚本.创建Application的单例对象. applic ...

  2. 通过DDOS攻击流程图来浅谈如何预防Ddos攻击与防御

    DDOS攻击流程图 站长之家配图(来源:ppkj.net) 一 背景 在前几天,我们运营的某网站遭受了一次ddos攻击,我们的网站是一个公益性质的网站,为各个厂商和白帽子之间搭建一个平台以传递安全问题 ...

  3. mysql 用户名密码登陆不上

    问题1:刚安装完mysql,设置了用户名密码root,登陆OK的,后来再连怎么也连不上了 操作步骤: 输入:mysql -uroot -proot 提示:ERROR 1045 (28000): Acc ...

  4. HDU5812 Distance 构造,预处理

    分析:怎么看都是超时,但是可以先筛一遍1e6以内的每个数的最小素数 算出每个数由多少个素数组成,然后应用,c[1e6][20] 就是题解的那一套,参照题解,比赛的时候没有想到好的办法筛一个数的因子,醉 ...

  5. Selenium启动本地firefox的profile

    ProfilesIni pi = new ProfilesIni();FirefoxProfile profile = pi.getProfile("default");WebDr ...

  6. 【,net】发布网站问题

    今天解决了一个发布后网站访问不了的问题: 问题: 发布网站:http://172.168.1.102:2222/nologin.html,但是页面一直处于刷新状态,进行不了系统: 解决方法: 问开发他 ...

  7. 开发环境配置(netbeans+ant迁移到eclipse+maven)

    新公司入职,接手一个离职人员的项目,拿到的源码是以一个压缩包,用netbeans开发,ant管理:前端:jsp+extjs,后端:springmvc+hibernate+activiti+spring ...

  8. Javascript模块化开发-轻巧自制

    Javascript模块化开发-轻巧自制 一.前言现在javascript的流行,前端的代码越来越复杂,所以我们需要软件工程的思想来开发前端.模块化是必不可少的,这样不仅能够提高代码的可维护性.可扩展 ...

  9. 从四大音乐APP首页设计对比分析产品方向

    原帖:http://www.ui.cn/detail/63201.html 本文章中作者例举四个音乐APP应用:虾米.网易.百度.QQ首页 1. 推荐内容:作者将四个首页界面划分出官方推荐与个性化推荐 ...

  10. 50+ 响应式的Prestashop电商主题

    PrestaShop是一款针对web2.0设计的全功能.跨平台的免费开源电子商务解决方案,自08年1.0版本发布,短短两年时间,发展迅速,全球已超过四万家网店采用Prestashop进行部署.Pres ...