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得到的最大分数。
dp【i】【j】可以通过两种途径算出:
(1)枚举k(i<=k<j)dp【i】【j】=max(dp【i】【k】+dp【k+1】【j】)
(2)如果a【i】和a【j】不互质而且从i+1到j-1可以全部消除,那么dp【i】【j】就可以直接等于b【i】+b【j】+dp【i+1】【j-1】;
最后dp【1】【n】即为答案;
#include<iostream> #include<cstdio> #include<vector> #include<set> #include<map> #include<string.h> #include<cmath> #include<algorithm> #include<queue> #include<stack> #define LL long long #define mod 1000000007 #define inf 0x3f3f3f3f using namespace std; LL a[310]; LL b[310]; LL dp[310][310]; LL sum[310]; LL gcd(LL a,LL b) { return b==0?a:gcd(b,a%b); } int main() { int t; scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); memset(b,0,sizeof(b)); memset(sum,0,sizeof(sum)); int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); for(int i=1;i<=n;i++) { scanf("%lld",&b[i]); sum[i]=b[i]+sum[i-1]; } LL maxx=0; for(int l=1;l<n;l++) { for(int i=1;i+l<=n;i++) { int j=i+l; for(int k=i;k<j;k++) dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]); if(gcd(a[i],a[j])>1) { if(j==i+1) dp[i][j]=b[i]+b[j]; else if(dp[i+1][j-1]==sum[j-1]-sum[i]) dp[i][j]=dp[i+1][j-1]+b[i]+b[j]; } maxx=max(maxx,dp[i][j]); // printf("maxx=%lld %d %d\n",maxx,i,j); } } printf("%lld\n",maxx); } return 0; }