P5154 数列游戏 [区间DP]

传送门

先来一波区间DP的套路

f[l][r] = max(f[l][k]+f[k+1][r])

f[l][r] = max(f[l][r],f[l+1][r-1]+B[l]+B[r])(gcd(A[l],A[r]!=1))

记dp[i] 为1-i 的答案

dp[i]=max(dp[i-1],dp[j-1]+f[j][i])(1<=j<=i-1)


#include
#define N 805
#define LL long long
#define inf 1000000000000000
using namespace std;
LL f[N][N], dp[N];
int A[N], B[N], n;
int read(){
	int cnt = 0; char ch = 0;
	while(!isdigit(ch)) ch = getchar();
	while(isdigit(ch)) cnt = cnt * 10 + (ch-'0'), ch = getchar();
	return cnt; 
}
int gcd(int a,int b){ return !b ? a : gcd(b, a%b);}
int main(){
	n = read();
	for(int i=1;i<=n;i++) A[i] = read();
	for(int i=1;i<=n;i++) B[i] = read();
	for(int i=1;i

 

你可能感兴趣的:(DP)