【HDU5900】【区间动规】QSC and Master 相邻的不互质的数取出代价为他们对应的value和,求代价最大

来源:互联网 发布:时钟js代码大全 编辑:程序博客网 时间:2024/06/14 01:50

传送门:HDU 5900 QSC and Master

描述:

QSC and Master

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


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
331 2 31 1 131 2 41 1 141 3 4 31 1 1 1
 

Sample Output
020
 

Source
2016 ACM/ICPC Asia Regional Shenyang Online
 

Recommend
wange2014
 
题意:

给出n对数keyi,vali表示当前这对数的键值和权值,可以操作将连续的两个数合并,如果满足gcd(a[i],a[i+1])>1,得到

的价值是两个数的权值和,每次合并两个数之后,这两个数就会消失,然后旁边的数会接上

比如3 2 4 6 合并了 2 4 则 剩下3 6也可以合并


思路:

像极了石头归并的模型,网赛的时候没想出来,可惜了,石头归并模型题解见Here

记忆化搜索一下,考虑消去两边的情况,前提是中间的已经消去或者把区间从中间的断开再拼起来

代码:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define ll __int64using  namespace std;const int N=310;int n;ll a[N],b[N];ll dp[N][N];ll ans=0;ll gcd(ll a,ll b){    return b==0?a:gcd(b,a%b);}ll dfs(int x,int y){  if(dp[x][y]!=-1)return dp[x][y];  if(x>=y)return dp[x][y]=0;  ll tmp=gcd(a[x], a[y]);  if(x+1==y && tmp!=1)return dp[x][y]=b[x]+b[y];  if(x+1==y && tmp==1)return dp[x][y]=0;  ll mx=0;  if(tmp!=1){//考虑消去两边的情况,前提是中间的已经消去    ll sum=dfs(x+1, y-1),now=0;    for(int i=x+1; i<=y-1; i++)now+=b[i];    if(sum==now)mx=b[x]+b[y]+sum;  }  for(int i=x; i<y; i++)//中间的断开再拼起来    mx=max(mx, dfs(x, i)+dfs(i+1, y));  return dp[x][y]=mx;}int  main(){//  #ifndef ONLINE_JUDGE//  freopen("in.txt","r",stdin);//  #endif  int t;  scanf("%d",&t);  while(t--){    scanf("%d",&n);    memset(dp, -1, sizeof(dp));    for(int i=1; i<=n; i++){      scanf("%I64d",&a[i]);    }    for(int i=1; i<=n; i++){      scanf("%I64d",&b[i]);    }    ans=dfs(1, n);    printf("%I64d\n",ans);  }  return 0;}


0 0