QSC and Master (interval dp)

QSC and Master

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


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
0
2
0
 
Title:
 
Give n logarithms if the gcd of the first of the adjacent pairs! = 1, then these two numbers can be taken together to obtain the benefit of the sum of the second number, and find the maximum benefit;



The general idea of ​​interval dp:

1 Enumeration interval length

2 Enumeration starting point

3 Consider the interval expansion method to recursively deduce large intervals

The recursive method of this question is to add an element to each side of the interval, l, r, if gcd(l, r)! = 1 and all elements in the interval have been merged, then

The range can be expanded. Make a prefix sum to determine whether all elements in the interval have been merged.

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[1005][1005];
int mk[1005][1005];
ll key[1005],val[1005];
ll sum[1005];
ll gcd(ll a, ll b)
{
	return b == 0 ? a : gcd(b, a%b);
}
intmain()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
    	int n;
    	scanf("%d",&n);
    	for(int i = 1; i <= n; i++)
    	{
    		scanf("%lld",&key[i]);
		}
		sum[0] = 0;
		for(int i = 1; i <= n; i++)
		{
			scanf("%lld",&val[i]);
			sum[i]=val[i] + sum[i-1];
		}
		memset(dp,0,sizeof(dp));
		for(int i = 2; i <= n; i++)
		for(int j = 1; j <= n-i+1; j++)
		{
			for(int k = 1; k < i; k++)
			{
				dp[j][i+j-1] = max(dp[j][j+k-1]+dp[j+k][i+j-1],dp[j][i+j-1]);
			}
			if(gcd(key[j],key[i+j-1]) != 1 && sum[i+j-2]-sum[j] == dp[j+1][i+j-2])
			{
 				dp[j][i+j-1] = max(dp[j+1][j+i-2]+val[j]+val[i+j-1],dp[j][i+j-1]);
			}
		}
		cout <<dp[1][n]<< endl;
	}
	return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325812869&siteId=291194637