# codeforces 624d 623b Array GCD ★ ★

169人阅读 评论(0)

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 1000100
#define MOD 1000000007

long long dp[maxn][3];
int val[maxn];
long long a,b;
int prime[maxn];
int vis[maxn];
int n,cnt,fac[maxn];

void init()
{
cnt = 0;
for(int i = 2; i < maxn; i++)
{
if(!vis[i])
prime[cnt++] = i;
for(int j = 0; j < cnt && prime[j]*i < maxn; j++)
{
vis[prime[j]*i] = 1;
if(i%prime[j] == 0)
break;
}
}
}
long long solve(int st,int ed,int v)
{
memset(dp,0x3f3f3f3f3f3f3f3f,sizeof(dp));
dp[st-1][0] = 0;
for(int i = st; i <= ed; i++)
{
dp[i][1] = min(dp[i-1][0],dp[i-1][1]) + a;
if(val[i] % v != 0)
{
if((val[i]+1)%v == 0 || (val[i]-1)%v==0)
{
dp[i][0] = dp[i-1][0] + b;
dp[i][2] = min(dp[i-1][1],dp[i-1][2]) + b;
}
}
else
{
dp[i][0] = dp[i-1][0];
dp[i][2] = min(dp[i-1][1],dp[i-1][2]);
}
}
return min(min(dp[ed][0],dp[ed][1]),dp[ed][2]);
}
int factor(int x)
{
int tot = 0;
for(int j = 0; j < cnt && x >= prime[j]; j++)
{
if(x%prime[j] == 0)
{
fac[tot++] = prime[j];
while(x%prime[j] == 0)
x /= prime[j];
}
}
if(x != 1)
fac[tot++] = x;
return tot;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,C = 1;
//scanf("%d",&t);
init();
while(scanf("%d%lld%lld",&n,&a,&b) != EOF)
{
for(int i = 1; i <= n; i++)
scanf("%d",&val[i]);
long long ans = a * (long long)n;
for(int i = -1; i <= 1; i++)
{
long long cost = i == 0?0:b;
int len = factor(val[n]+i);
for(int j = 0; j < len; j++)
ans = min(ans,solve(1,n-1,fac[j])+cost);

len = factor(val[1]+i);
for(int j = 0; j < len; j++)
ans = min(ans,solve(2,n,fac[j])+cost);
}
printf("%lld\n",ans);
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：110752次
• 积分：4295
• 等级：
• 排名：第7305名
• 原创：306篇
• 转载：187篇
• 译文：0篇
• 评论：1条
最新评论