2016 Shenyang Network Competition --- QSC and Master (interval DP), --- qscdp

Source: Internet
Author: User

2016 Shenyang Network Competition --- QSC and Master (interval DP), --- qscdp

Question Link

Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5900

 

Problem DescriptionEvery 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.

Cocould you solve this puzzle?

(Data range: 1 <= N <= 300
1 & lt; = Ai. key & lt; = 1,000,000,000
0 <Ai. value <= 1,000,000,000) InputFirst 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. OutputFor each test case, output the max score you cocould get in a line. Sample Input331 2 31 1 131 2 41 1 141 3 4 31 1 1 Sample Output020 Source2016 ACM/ICPC Asia Regional Shenyang Online

 

RecommendWange2014 | We have carefully selected several similar problems for you: 5901 5899 5898 5897 5896 meaning: nItems pair<int , int>, You can select two adjacent pair. If their firstThey can be deleted without mutual quality and obtained secondThe total score. the maximum score. Idea: interval DP, similar to the question matching parentheses (a sequence of parentheses, requires that the minimum number of parentheses be added to match the sequence) define dp [I] [j] to indicate I ~ The maximum score that can be obtained from the range of j is the state transition equation: dp [I] [j] = dp [I] [k] + dp [k + 1] [j] In addition, pay attention to the situation when determining the two sides, that is, the interval I + 1 ~ The J-1 is removed (Judgment condition dp [I + 1] [J-1] = sum (B [I + 1] + .... + B [J-1]) If gcd (a [I], a [j])> 1 dp [I] [j] = dp [I + 1] [J-1] + B [I] + B [j]; the Code is as follows:
#include <iostream>#include <algorithm>#include <stdio.h>#include <queue>#include <cmath>#include <string.h>using namespace std;long long a[305],b[305];long long dp[305][305];long long sum[305];long long GCD(long long a,long long b){    return  (b==0)?a:GCD(b,a%b);}int main(){    int T,N;    cin>>T;    while(T--)    {        scanf("%d",&N);        for(int i=1;i<=N;i++)            scanf("%lld",&a[i]);        sum[0]=0;        for(int i=1;i<=N;i++)        {            scanf("%lld",&b[i]);            sum[i]=sum[i-1]+b[i];        }        memset(dp,0,sizeof(dp));        for(int len=1;len<N;len++)        {            for(int i=1;i+len<=N;i++)            {                if(sum[i+len-1]-sum[i]==dp[i+1][i+len-1])                {                    dp[i][i+len]=dp[i+1][i+len-1];                    if(GCD(a[i],a[i+len])>1) dp[i][i+len]+=b[i]+b[i+len];                }                for(int k=i;k<i+len;k++)                {                    dp[i][i+len]=max(dp[i][i+len],dp[i][k]+dp[k+1][i+len]);                }            }        }        printf("%lld\n",dp[1][N]);    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.