博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AIM Tech Round (Div. 2) D. Array GCD dp
阅读量:7113 次
发布时间:2019-06-28

本文共 2868 字,大约阅读时间需要 9 分钟。

D. Array GCD

题目连接:

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
using namespace std;const int maxn = 1e6+7;int a[maxn];long long A,B;int n;long long ans = 1e16;vector
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

转载地址:http://bighl.baihongyu.com/

你可能感兴趣的文章
Oracle表空间维护总结
查看>>
12C -- ORA-01017
查看>>
约瑟夫环问题
查看>>
Compile、Make和Build的区别(as make, build, clean, run)
查看>>
介绍三款串口监控工具:Device Monitoring Studio,portmon,Comspy
查看>>
Bulk Load-HBase数据导入最佳实践
查看>>
sqlServer的主键只能自增不能手动增加
查看>>
maven常用命令介绍
查看>>
【树莓派】树莓派上刷android系统
查看>>
J2EE之Servlet初见
查看>>
elasticsearch best_fields most_fields cross_fields从内在实现看区别——本质就是前两者是以field为中心,后者是词条为中心...
查看>>
JPA(一):简介
查看>>
git 的安装和使用
查看>>
(转) OpenCV学习笔记大集锦 与 图像视觉博客资源2之MIT斯坦福CMU
查看>>
Controller 接口控制器详解
查看>>
【转】【MySQL】mysql 通过bin-log恢复数据方法详解
查看>>
linux上安装启动elasticsearch-5.5.1完整步骤
查看>>
Silverlight 4 MVVM开发方式(一)小黑端
查看>>
公告:CSDN博客频道新功能正式上线!
查看>>
Web服务的体系架构
查看>>