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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include ;
#include 
#include 
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];
vectorgcd;

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;
}



你可能感兴趣的:(Codeforces,dp-简单dp)