首页 > 代码库 > HDU 5900

HDU 5900

QSC and Master

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 612    Accepted Submission(s): 214


Problem Description
Every school has some legends, Northeastern University is the same.

Enter from the north gate of Northeastern University,You are facing the main building of Northeastern University.Ninety-nine percent of the students have not been there,It is said that there is a monster in it.

QSCI am a curious NEU_ACMer,This is the story he told us.

It’s a certain period,QSCI am in a dark night, secretly sneaked into the East Building,hope to see the master.After a serious search,He finally saw the little master in a dark corner. The master said:

“You and I, we‘re interfacing.please solve my little puzzle!

There are N pairs of numbers,Each pair consists of a key and a value,Now you need to move out some of the pairs to get the score.You can move out two continuous pairs,if and only if their keys are non coprime(their gcd is not one).The final score you get is the sum of all pair’s value which be moved out. May I ask how many points you can get the most?

The answer you give is directly related to your final exam results~The young man~”

QSC is very sad when he told the story,He failed his linear algebra that year because he didn‘t work out the puzzle.

Could you solve this puzzle?

(Data range:1<=N<=300
1<=Ai.key<=1,000,000,000
0<Ai.value<=1,000,000,000)
 

 

Input
First line contains a integer T,means there are T(1≤T≤10) test case。

  Each test case start with one integer N . Next line contains N integers,means Ai.key.Next line contains N integers,means Ai.value.
 

 

Output
For each test case,output the max score you could get in a line.
 

 

Sample Input
331 2 31 1 131 2 41 1 141 3 4 31 1 1 1
 

 

Sample Output
020
 
补个区间DP题,越来越发现区间DP题都好明显啊,首先范围都是100,200,300的,其次在考虑最优子结构时都是一段区间。
对于这个题,像其他的区间题一样,考虑区间[i,j]:
首先dp[i][j]初始化,那就是枚举k点啦。
然后处理:如果key[i]和key[j]是gcd的话,如果j=i+1,那么直接dp[i][j] = val[i]+val[j],否则必须[i+1,j-1]内全部去掉才可以dp[i][j] =
max(dp[i][j],dp[i+1][j-1]+val[i]+val[j]).可以用个前缀和来判断。
 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 }

 

 

HDU 5900