【区间DP】HDU - 5900 B - QSC and Master

B - QSC and Master  HDU - 5900 

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

3
3
1 2 3
1 1 1
3
1 2 4
1 1 1
4
1 3 4 3
1 1 1 1
 

Sample Output

0
2
0

给你n对数,每对数都有一个键值和一个权值

每次你都可以将任意两个相邻的数(如果他们的键值gcd!=1)拿走,你将会得到它们的权值

比如,我拿走2号和3号,那么1号和4号就相邻了

问你最后可以得到的值最大是多少

预处理处所有的gcd不等于1时的权值和,预处理处1-n的权值前缀和

如果dp[i+1][j-1]==sum[j-1]-sum[i],就代表dp[i+1][j-1]中所有的数都已经拿走了,此时如果__gcd(i,j)!=1,那么dp[i][j]=dp[i+1][j-1]+value[i]+value[j]

然后去判断dp[i][j]和dp[i][k]+dp[k+1][j]谁更大

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int value[305],key[305];
ll dp[305][305],svalue[305];
int gcd[305][305];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&key[i]);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&value[i]);
            svalue[i]=svalue[i-1]+value[i];
        }
        memset(dp,0,sizeof(dp));
        memset(gcd,0,sizeof(gcd));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j) continue;
                if(__gcd(key[i],key[j])!=1) gcd[i][j]=value[i]+value[j];
            }
        }
        for(int len=2;len<=n;len++)
        {
            for(int i=1;i<=n;i++)
            {
                int j=i+len-1;
                if(j>n) break;
                if(gcd[i][j]&&dp[i+1][j-1]==svalue[j-1]-svalue[i])
                {
                    dp[i][j]=dp[i+1][j-1]+value[i]+value[j];
                }
                for(int k=i;k<j;k++)
                {
                    dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
                }
            }
        }
        printf("%lld\n",dp[1][n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/82771002