题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5900

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
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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5901 5899 5898 5897 5896 
 
 
题意:n 个pair<int , int>,每次可以选相邻两个pair。如果他们的first不互质就可以把它们都删掉,并且获得second之和的分数,问最大得分。
 
思路:区间DP,类似于括号匹配的题(一个括号序列,要求添加最少的括号使这个括号序列匹配) 定义dp[i][j] 表示i~j的区间能得的最大分,那么有状态转移方程:dp[i][j]=dp[i][k]+dp[k+1][j]   另外注意特判取两边时的情形,即区间 i+1~j-1 都删除了(判断条件dp[i+1][j-1]==sum(b[i+1]+....+b[j-1])),如果gcd(a[i],a[j])>1 dp[i][j]=dp[i+1][j-1]+b[i]+b[j];
 
代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cmath>
#include <string.h>
using namespace std;
long long a[],b[];
long long dp[][];
long long sum[];
long long GCD(long long a,long long b)
{
return (b==)?a:GCD(b,a%b);
}
int main()
{
int T,N;
cin>>T;
while(T--)
{
scanf("%d",&N);
for(int i=;i<=N;i++)
scanf("%lld",&a[i]);
sum[]=;
for(int i=;i<=N;i++)
{
scanf("%lld",&b[i]);
sum[i]=sum[i-]+b[i];
}
memset(dp,,sizeof(dp));
for(int len=;len<N;len++)
{
for(int i=;i+len<=N;i++)
{
if(sum[i+len-]-sum[i]==dp[i+][i+len-])
{
dp[i][i+len]=dp[i+][i+len-];
if(GCD(a[i],a[i+len])>) dp[i][i+len]+=b[i]+b[i+len];
}
for(int k=i;k<i+len;k++)
{
dp[i][i+len]=max(dp[i][i+len],dp[i][k]+dp[k+][i+len]);
}
}
}
printf("%lld\n",dp[][N]);
}
return ;
}

2016 年沈阳网络赛---QSC and Master(区间DP)的更多相关文章

  1. 2016沈阳网络赛 QSC and Master

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  2. HDU 5900 QSC and Master 区间DP

    QSC and Master Problem Description   Every school has some legends, Northeastern University is the s ...

  3. HDU 5901 Count primes (2016 acm 沈阳网络赛)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5901 题意:输入n,输出n以内质数个数 模板题,模板我看不懂,只是存代码用. 官方题解链接:https ...

  4. hdu 6199 沈阳网络赛---gems gems gems(DP)

    题目链接 Problem Description Now there are n gems, each of which has its own value. Alice and Bob play a ...

  5. 2019ICPC沈阳网络赛-D-Fish eating fruit(树上DP, 换根, 点分治)

    链接: https://nanti.jisuanke.com/t/41403 题意: State Z is a underwater kingdom of the Atlantic Ocean. Th ...

  6. 2018 ICPC 沈阳网络赛

    2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...

  7. 沈阳网络赛 F - 上下界网络流

    "Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...

  8. hdu_5900_QSC and Master(区间DP)

    题目链接:hdu_5900_QSC and Master 题意: 有n个数,每个数有个key值,有个val,如果相邻的两个数的key的gcd大于1那么就可以得到这两个数的val的和,现在问怎么取使得到 ...

  9. 2016沈阳网络赛 odd-even number

    odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. mybatis结合分页的使用及解析.

    首先说明: 这里分页是使用了SSM框架+ jsp 来做的, 当然分页还有其他的很多做法, 比如easyUI自带的分页效果. 但是这些原理都是很相似的, 再次只做为学习总结之用. 一, 效果图这里的截图 ...

  2. [数据库事务与锁]详解六: MySQL中的共享锁与排他锁

    注明: 本文转载自http://www.hollischuang.com/archives/923 在MySQL中的行级锁,表级锁,页级锁中介绍过,行级锁是Mysql中锁定粒度最细的一种锁,行级锁能大 ...

  3. Backbone中 View之间传值的解决办法

    Backbone中的View就是用来展示由Model层传出的数据,或者在View里产生的一些数据,包括输入框中输入等产生的数据,由当前View传递到另外一个View层里,应该怎么办呢,我之前读到一位博 ...

  4. 搭建LNAMP环境(四)- 源码安装PHP7

    上一篇:搭建LNAMP环境(三)- 源码安装Apache2.4 一.安装PHP7 1.yum安装编译php需要的包 yum -y install libxml2 libxml2-devel curl- ...

  5. git查看日志

    git查看日志 git log -n 显示前N条记录 git log -3 退出log命令 直接输入: q git log --stat -n 显示提交的文件的简要的修改统计 $ git log -- ...

  6. ServletConfig接口默认是哪里实现的?

    问题:Servlet接口默认是哪里实现的? 答:GenericServlet 1.结构 2.ServletConfig.GenericServlet.HttpServlet的关系如下: public ...

  7. Spring.NET的AOP怎么玩

    之前公司一直不让使用第三方组件,因此AOP方面的组建一直不能使用,很多面向切面的应用只能通过自己写一些GenericMethod的泛型方法来解决,有一些呆板.由于公司已经开始全面转Java,因此架构组 ...

  8. Shader预处理宏、内置状态变量、多版本编译等

    预定义shader预处理宏: Target platform: SHADER_API_OPENGL - desktop OpenGL SHADER_API_D3D9 - Direct3D SHADER ...

  9. 深入了解ibatis源码----简单ibatis示例代码

    搭建源码环境: 1.创建sql数据库. CREATE TABLE USER_ACCOUNT ( USERID INT(3) NOT NULL AUTO_INCREMENT, USERNAME VARC ...

  10. Android 自动补全提示输入AutoCompleteTextView、 MultiAutoCompleteTextView

    以在搜索框搜索时,自动补全为例: 其中还涉及到一个词,Tokenizer:分词器,分解器. 上效果图: MainActivity.java: package com.joan.testautocoml ...