天天看點

hdu 5900 QSC and Master【區間dp】QSC and Master

QSC and Master

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 706    Accepted Submission(s): 257

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

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

2

題目大意:有n個數組對,其中第一個值表示key,另外一個值表示val。每一次可以拿取相鄰的兩個數組對,要求是其兩個數組對gcd(key1,key2)!=1.取值是val1+val2,問能夠拿取的最大值。

另外:

1 2 3 4

如果23取走了之後,14相當于相鄰的。

思路:

T T區間dp知識面不牢固,比賽調了兩個小時也沒過T T

1、很明顯的區間dp,設定dp【i】【j】為從i到j按照規則取值的最大值。

2、那麼狀态轉移方程:

if(gcd(key【i】,key【j】)!=1)

dp【i】【j】=val【i】+val【j】;(i+1==j)

dp【i】【j】=max(dp【i】【j】,dp【i+1】【j-1】+val【i】+val【j】)(dp【i+1】【j-1】==sum(從i+1到j-1的所有val累加)){對于這個sum值,我們可以設定sum【i】表示從第一個數到第i個數的val累加值,在輸入val【i】的時候維護一下這個字首和即可。}

Ac代碼:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
ll dp[501][501];
ll key[501];
ll val[501];
ll sum[501];
ll gcd(ll x,ll y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(dp,0,sizeof(dp));
        memset(key,0,sizeof(key));
        memset(val,0,sizeof(val));
        for(int i=0;i<n;i++)
        {
            scanf("%I64d",&key[i]);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%I64d",&val[i]);
            if(i==0)sum[i]=val[i];
            else sum[i]=sum[i-1]+val[i];
        }
        for(int len=1;len<=n;len++)
        {
            for(int i=0;i+len<n;i++)
            {
                int j=i+len;
                if(gcd(key[i],key[j])!=1)
                {
                    if(j-i==1)
                    {
                        dp[i][j]=val[i]+val[j];
                    }
                    else if(sum[j-1]-sum[i]==dp[i+1][j-1])
                    {
                        dp[i][j]=max(dp[i][j],dp[i+1][j-1]+val[i]+val[j]);
                    }
                }
                for(int k=0;k<n;k++)
                {
                    dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
                }
            }
        }
        printf("%I64d\n",dp[0][n-1]);
    }
}