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
题意:给n个数,每个数有他的key值和valua值,每次从中取出两个相邻的数,要求这两个数的key不互质,然后能得到这两个的valua值相加,要求从中不断取直到valua最大,取不到则为0
a b c d 取走b c后序列就变成 a d,可以继续取
以valua值作为前缀和,求出每个key值和其他key直是否互质
在区间l r求出不互质最大和
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 310
#define ll long long
using namespace std;
int a[N],b[N],t[N][N];
ll gcd(ll x,ll y){
return (y>0)?gcd(y,x%y):x;
}
long long int sum[N],f[N][N],dp[N],res[N];
bool check(int l,int r)
{
int i;
if(f[l][r]) return 1;
for(i=l;i<r;i++){
if(f[l][i]&&f[i+1][r])
return 1;
}
return 0;
}
int main(){
int i,j,n,m,T,l;
cin>>T;
while(T--){
cin>>n;
memset(sum,0,sizeof(sum));
memset(t,0,sizeof(t));
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{scanf("%d",&b[i]);
sum[i]=b[i]+sum[i-1];
}
memset(f,0,sizeof(f));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++){
if(gcd(a[i],a[j])!=1)
t[i][j]=1;
else t[i][j]=0;
}
for(i=1;i<n;i++)
if(t[i][i+1]) f[i][i+1]=b[i+1]+b[i];
for(l=3;l<=n;l+=2)
for(i=1;i+l<=n;i++)
if(t[i][i+l]&&check(i+1,i+l-1)||check(i,i+l))
f[i][i+l]=sum[i+l]-sum[i-1];
memset(dp,0,sizeof(dp));
memset(res,0,sizeof(res));
for(i=1;i<=n;i++)
for(j=1;i-j>=0;j+=2)
{
dp[i]=max(dp[i],res[i-j-1]+f[i-j][i]);
res[i]=res[i-1];
res[i]=max(dp[i],res[i]);
}
printf("%lld\n",res[n]);
}
return 0;
}