QSC and Master
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 529 Accepted Submission(s): 169
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)
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.
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
dp题,用dp[[i][j]表示从i到j之间的最优解,转移方程可以这样写
当i>=j的时,dp[i][j]=0;
当i+1==j且key[i]和key[j]互质时,dp[i][j]=0;
当i+1==j且key[i]和key[j]不互质时,dp[i][j]=val[i]+val[j]
其他情况为,枚举k从i到j-1, dp[i][j]=max(dp[i][k]+dp[k+1][j]).
上面还遗漏了一种情况,就是i+1到j-1之间都可以取,且i和j不互质,这种情况i到j所有的数都可以取。
以上就是所有情况,接下来写代码就可以了。
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
long long key[305];
long long val[305];
long long dp[305][305];
long long gcd(long long a,long long b)
{
return b?gcd(b,a%b):a;
}
long long cal(int x,int y)
{
if(dp[x][y]!=-1)
return dp[x][y];
long long t=gcd(key[x],key[y]);
if(x>=y)
{
return dp[x][y]=0;
}
if(x+1==y&&t!=1)
{
return dp[x][y]=val[x]+val[y];
}
if(x+1==y&&t==1)
{
return dp[x][y]=0;
}
long long _max=0;
if(t!=1)
{
long long xx=0;
for(int i=x+1;i<=y-1;i++)
{
xx+=val[i];
}
if(cal(x+1,y-1)==xx)
_max=xx+val[x]+val[y];
}
for(int i=x;i<=y-1;i++)
{
_max=max(_max,cal(x,i)+cal(i+1,y));
}
return dp[x][y]=_max;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(dp,-1,sizeof(dp));
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lld",&key[i]);
}
for(int i=0;i<n;i++)
{
scanf("%lld",&val[i]);
}
long long ans=cal(0,n-1);
printf("%lld\n",ans);
}
//system("pause");
}