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
Source
2016 ACM/ICPC Asia Regional Shenyang Online
题意:给一个数列,每次可以消除相邻的两个不互质的数并得到相应位置的得分,问最多得多少分。
分析:考虑最后消除剩下的序列应该是连续的一段一段的,于是我们先处理出哪些段可以被消除干净,f[i][j]表示[I,J]能否被消除干净,枚举j和哪个数配对dp就可以了,复杂度n^3.
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#include<unordered_map>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 1000000007
#define MAXN 303
using namespace std;
typedef long long ll;
int T,n;
bool jud[MAXN][MAXN];
ll a[MAXN],b[MAXN],sum[MAXN],f[MAXN];
ll gcd(ll a,ll b)
{
if(a % b == 0) return b;
return gcd(b,a % b);
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(f,0,sizeof(f));
memset(jud,0,sizeof(jud));
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] = sum[i-1] + b[i];
}
for(int i = 2;i <= n;i++)
for(int j = 1;j <= n-i+1;j++)
{
if(i == 2)
{
jud[j][j+1] = (gcd(a[j],a[j+1]) != 1);
continue;
}
int l = j,r = j + i -1;
for(int k = l+2;k < r-3;k++)
if(jud[l][k-1] && jud[k+1][r-1] && (gcd(a[r],a[k]) != 1)) jud[l][r] = true;
if((gcd(a[l],a[r]) != 1) && jud[l+1][r-1]) jud[l][r] = true;
if((gcd(a[r],a[r-1]) != 1) && jud[l][r-2]) jud[l][r] = true;
}
for(int i = 1;i <= n;i++)
{
f[i] = f[i-1];
for(int j = i;j >= 2;j--)
if(jud[i-j+1][i]) f[i] = max(f[i],f[i-j] + sum[i] - sum[i-j]);
}
cout<<f[n]<<endl;
}
}