标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 612 Accepted Submission(s): 214
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 typedef long long ll; 8 const int maxn = 305; 9 ll dp[maxn][maxn]; 10 ll key[maxn]; 11 ll val[maxn]; 12 ll sum[maxn]; 13 int n; 14 ll gcd(ll a,ll b) 15 { 16 return b == 0 ? a : gcd(b, a % b); 17 } 18 void DP() 19 { 20 memset(dp,0,sizeof(dp)); 21 for(int l=1;l<=n;l++) 22 { 23 for(int i=1;i+l<=n;i++) 24 { 25 int j=i+l; 26 for(int k=i;k<j;k++) 27 dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]); 28 // printf("%I64d\n",gcd(key[i],key[j])); 29 if(gcd(key[i],key[j])>1) 30 { 31 if(j==i+1) dp[i][j] = val[i]+val[j]; 32 else if(dp[i+1][j-1]==sum[j-1]-sum[i]) dp[i][j] = max(dp[i][j],dp[i+1][j-1]+val[i]+val[j]); 33 } 34 } 35 } 36 printf("%I64d\n",dp[1][n]); 37 } 38 int main() 39 { 40 int T;cin>>T; 41 while(T--) 42 { 43 memset(sum,0,sizeof(sum)); 44 scanf("%d",&n); 45 for(int i=1;i<=n;i++) scanf("%I64d",&key[i]); 46 for(int i=1;i<=n;i++) scanf("%I64d",&val[i]),sum[i]=val[i]+sum[i-1]; 47 DP(); 48 } 49 }
标签:
原文地址:http://www.cnblogs.com/littlepear/p/5886036.html