AIM Tech Round (Div. 1) B. Array GCD(数论+dp)
2017-02-17 22:43
387 查看
题目链接
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 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.
Examples
input
output
input
output
input
output
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 to 2·3 + 2 = 8 coins.
题意:
给你n个数,可以执行两种操作最多各一次,一是移除一个连续的序列,代价是序列长度*a,二是选一些数改变1,即可以有的加一,有的减一,代价是数字个数*b,注意不可以将整个序列移走。
最终使得剩下所有数字的最大公约数大于1,求最少代价。
题解:
由于最后序列必不为空,那么a[1],a
至少留一个,那么可以将a[1]-1、a[1]、a[1]+1、a
-1、a
、a
+1六个数的所有质因数预处理,然后枚举每个质因数为约数时的最小代价。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll inf=9e18;
const ll mod=1000000007;
const int maxn=1e6+100;
VI V;
int n;
ll x,y;
ll d[maxn][3],cost[maxn];
int a[maxn];
void solve(int x)
{
int i=2;
while(x!=1&&i*i<=x)
{
if(x%i==0)
{
V.pb(i);
while(x%i==0&&x!=1)
x/=i;
}
i++;
}
if(x>1) V.pb(x);
}
int main()
{
scanf("%d%I64d%I64d",&n,&x,&y);
rep(i,1,n+1) scanf("%d",&a[i]);
rep(i,-1,2) solve(a[1]+i),solve(a
+i);
sort(V.begin(),V.end());
V.erase(unique(V.begin(),V.end()),V.end());
ll ans=inf;
rep(i,0,V.size())
{
int p=V[i];
memset(cost,0,sizeof(cost));
rep(i,1,n+1) rep(j,0,3) d[i][j]=inf;
rep(i,1,n+1)
{
if(a[i]%p==0) cost[i]=0;
else if((a[i]-1)%p==0||(a[i]+1)%p==0) cost[i]=1;
else cost[i]=-1;
}
rep(i,1,n+1)
{
if(cost[i]==0)
{
d[i][1]=min(d[i-1][0],d[i-1][1])+x;
d[i][0]=d[i-1][0];
d[i][2]=min(min(d[i-1][1],d[i-1][0])+x,d[i-1][2]);
}
else if(cost[i]==1)
{
d[i][1]=min(d[i-1][0],d[i-1][1])+x;
d[i][0]=d[i-1][0]+y;
d[i][2]=min(min(d[i-1][1],d[i-1][0])+x,d[i-1][2]+y);
}
else
{
d[i][0]=inf;
d[i][1]=min(d[i-1][1],d[i-1][0])+x;
d[i][2]=min(min(d[i-1][1],d[i-1][0])+x,inf);
}
}
ll res=inf;
rep(i,0,3) res=min(res,d
[i]);
ans=min(ans,res);
}
printf("%I64d\n",ans);
return 0;
}
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 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.
Examples
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 to 2·3 + 2 = 8 coins.
题意:
给你n个数,可以执行两种操作最多各一次,一是移除一个连续的序列,代价是序列长度*a,二是选一些数改变1,即可以有的加一,有的减一,代价是数字个数*b,注意不可以将整个序列移走。
最终使得剩下所有数字的最大公约数大于1,求最少代价。
题解:
由于最后序列必不为空,那么a[1],a
至少留一个,那么可以将a[1]-1、a[1]、a[1]+1、a
-1、a
、a
+1六个数的所有质因数预处理,然后枚举每个质因数为约数时的最小代价。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll inf=9e18;
const ll mod=1000000007;
const int maxn=1e6+100;
VI V;
int n;
ll x,y;
ll d[maxn][3],cost[maxn];
int a[maxn];
void solve(int x)
{
int i=2;
while(x!=1&&i*i<=x)
{
if(x%i==0)
{
V.pb(i);
while(x%i==0&&x!=1)
x/=i;
}
i++;
}
if(x>1) V.pb(x);
}
int main()
{
scanf("%d%I64d%I64d",&n,&x,&y);
rep(i,1,n+1) scanf("%d",&a[i]);
rep(i,-1,2) solve(a[1]+i),solve(a
+i);
sort(V.begin(),V.end());
V.erase(unique(V.begin(),V.end()),V.end());
ll ans=inf;
rep(i,0,V.size())
{
int p=V[i];
memset(cost,0,sizeof(cost));
rep(i,1,n+1) rep(j,0,3) d[i][j]=inf;
rep(i,1,n+1)
{
if(a[i]%p==0) cost[i]=0;
else if((a[i]-1)%p==0||(a[i]+1)%p==0) cost[i]=1;
else cost[i]=-1;
}
rep(i,1,n+1)
{
if(cost[i]==0)
{
d[i][1]=min(d[i-1][0],d[i-1][1])+x;
d[i][0]=d[i-1][0];
d[i][2]=min(min(d[i-1][1],d[i-1][0])+x,d[i-1][2]);
}
else if(cost[i]==1)
{
d[i][1]=min(d[i-1][0],d[i-1][1])+x;
d[i][0]=d[i-1][0]+y;
d[i][2]=min(min(d[i-1][1],d[i-1][0])+x,d[i-1][2]+y);
}
else
{
d[i][0]=inf;
d[i][1]=min(d[i-1][1],d[i-1][0])+x;
d[i][2]=min(min(d[i-1][1],d[i-1][0])+x,inf);
}
}
ll res=inf;
rep(i,0,3) res=min(res,d
[i]);
ans=min(ans,res);
}
printf("%I64d\n",ans);
return 0;
}
相关文章推荐
- AIM Tech Round (Div. 2) D. Array GCD dp
- AIM Tech Round (Div. 2) D. Array GCD(dp)
- AIM Tech Round (Div. 2) D. Array GCD(dp,two pointers)
- 【DP】AIM Tech Round (Div. 2) D
- AIM Tech Round 3 (Div. 1) C. Centroids(树形DP)
- AIM Tech Round 3 (Div. 2) E. Centroids (树形dp) ★ ★ ★
- AIM Tech Round 3 (Div. 1) C. Centroids(树形dp)
- AIM Tech Round 3 (Div. 2) E. Centroids (树形dp)
- 【AIM Tech Round 5 (rated, Div. 1 + Div. 2) A】 Find Square
- AIM Tech Round 4 (Div. 2) A. Diversity
- codeforces AIM Tech Round 3 (Div. 2) B. Checkpoints
- AIM Tech Round 3 (Div. 2) -- B. Checkpoints (枚举)
- AIM Tech Round (Div. 2)题解
- AIM Tech Round 3 (Div. 2)(题解)
- AIM Tech Round (Div. 2) C. Graph and String
- Codeforces AIM Tech Round (Div. 2) 题解
- 【AIM Tech Round 5 (rated, Div. 1 + Div. 2) 总结】【题解往前或往后翻,不在这】
- 【Codeforces AIM Tech Round 4 (Div. 2) C】
- AIM Tech Round 3 (Div. 2) A.Juicer
- AIM Tech Round 3 (Div. 2) A , B , C