Codeforces 623B:Array GCD

B. Array GCD
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 na 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 test(s)
input
3 1 4
4 2 3
output
1
input
5 3 2
5 17 13 5 6
output
8
input
8 3 4
3 7 5 4 3 12 9 4
output
13
Note

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.


给定一个数组,删除一个元素的代价是a,删除元素的话只能删除一段连续的。对元素+1或者-1的代价是b,在这些操作之后,使得所有元素的最大公约数大于等于2,问最小代价。

a1与an肯定会有一个剩余,所以对a1-1 a1 a1+1 an-1 an an+1操作找其质因数,再用dp[i][j]表示扫描到第i个元素时 没有删除元素/正在删除元素/已经删除完元素的最小代价。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <ctime>;
#include <set>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x3fffffffffffffff

const ll mod = 1e9 + 7;
const int maxn = 1000005;

ll n, a, b;
ll cost[maxn],val[maxn], dp[maxn][3];
vector<ll>gcd;

ll cal(ll x)
{
	int i;
	for (i = 1; i <= n; i++)
	{
		cost[i] = 1e12 + 7;
		if (val[i] % x == 0)
		{
			cost[i] = 0;
		}
		else if ((val[i] + 1) % x == 0 || (val[i] - 1) % x == 0)
		{
			cost[i] = b;
		}
	}
	for (i = 1; i <= n; i++)
	{
		dp[i][0] = dp[i - 1][0] + cost[i];
		dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + a;
		dp[i][2] = min(dp[i - 1][1], dp[i - 1][2]) + cost[i];
	}
	return min(min(dp[n][2], dp[n][1]), dp[n][0]);
}

void pri(ll x)
{
	int i;
	for (i = 2; i*i <= x; i++)
	{
		if (x % i == 0)
			gcd.push_back(i);
		while (x % i == 0)
			x = x / i;
	}
	if (x != 1)
		gcd.push_back(x);
}

void solve()
{
	int i;
	scanf("%I64d%I64d%I64d", &n, &a, &b);

	for (i = 1; i <= n; i++)
	{
		scanf("%d", &val[i]);
	}
	pri(val[1]);
	pri(val[1] + 1);
	pri(val[1] - 1);
	pri(val[n]);
	pri(val[n] + 1);
	pri(val[n] - 1);
	sort(gcd.begin(), gcd.end());
	gcd.erase(unique(gcd.begin(), gcd.end()), gcd.end());
	ll ans = INF, sz = gcd.size();
	for (i = 0; i < sz; i++)
	{
		ans = min(ans, cal(gcd[i]));
	}
	cout << ans << endl;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("i.txt", "r", stdin);
	freopen("o.txt", "w", stdout);
#endif
	
	solve();

	return 0;
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值