HDU 5900
QSC and Master
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 612 Accepted Submission(s): 214
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)
Each test case start with one integer N . Next line contains N integers,means Ai.key.Next line contains N integers,means Ai.value.
3
1 2 3
1 1 1
3
1 2 4
1 1 1
4
1 3 4 3
1 1 1 1
2
0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = ;
ll dp[maxn][maxn];
ll key[maxn];
ll val[maxn];
ll sum[maxn];
int n;
ll gcd(ll a,ll b)
{
return b == ? a : gcd(b, a % b);
}
void DP()
{
memset(dp,,sizeof(dp));
for(int l=;l<=n;l++)
{
for(int i=;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+][j]);
// printf("%I64d\n",gcd(key[i],key[j]));
if(gcd(key[i],key[j])>)
{
if(j==i+) dp[i][j] = val[i]+val[j];
else if(dp[i+][j-]==sum[j-]-sum[i]) dp[i][j] = max(dp[i][j],dp[i+][j-]+val[i]+val[j]);
}
}
}
printf("%I64d\n",dp[][n]);
}
int main()
{
int T;cin>>T;
while(T--)
{
memset(sum,,sizeof(sum));
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%I64d",&key[i]);
for(int i=;i<=n;i++) scanf("%I64d",&val[i]),sum[i]=val[i]+sum[i-];
DP();
}
}
HDU 5900的更多相关文章
- HDU 5900 QSC and Master (区间DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意:给出序列$A_{i}.key$和$A_{i}.value$,若当前相邻的两个数$A_{ ...
- HDU 5900 QSC and Master 区间DP
QSC and Master Problem Description Every school has some legends, Northeastern University is the s ...
- 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp
QSC and Master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- HDU 5900 - QSC and Master [ DP ]
题意: 给n件物品,有key和value 每次可以把相邻的 GCD(key[i], key[i+1]) != 1 的两件物品,问移除的物品的总value最多是多少 key : 1 3 4 2 移除3 ...
- HDU 5900 QSC and Master
题目链接:传送门 题目大意:长度为n的key数组与value数组,若相邻的key互斥,则可以删去这两个数同时获得对应的两 个value值,问最多能获得多少 题目思路:区间DP 闲谈: 这个题一开始没有 ...
- hdu 5900 区间dp
题意:给你n对pair 里面有两个值,分别是key 和 val .你可以取相邻的两个pair 获得其中的val,前提是两个pair 的key 的 gcd 不为 1.当然你把相邻的两个取走了之后原本不相 ...
- BestCoder17 1001.Chessboard(hdu 5100) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目意思:有一个 n * n 的棋盘,需要用 k * 1 的瓷砖去覆盖,问最大覆盖面积是多少. ...
- HDU 5100
http://acm.hdu.edu.cn/showproblem.php?pid=5100 用1*k方格覆盖n*n方格 有趣的一道题,查了下发现m67的博客还说过这个问题 其实就是两种摆法取个最大值 ...
- hdu 5100 Chessboard
http://acm.hdu.edu.cn/showproblem.php?pid=5100 在比赛时没看懂题就没看,结束之后,看了解题报告才知道怎么做. 解题报告: 首先,若n<k,则棋盘连一 ...
随机推荐
- IE6下a标签失效(背景穿透)
background:fixed url(about:blank);有时候做感应区域的时候在ie6下不给背景就会感应不到,比如说当鼠标移到图片的左半边部分,鼠标手势变成向左的箭 头,比如说有些时候a标 ...
- cocos2d-x 获得系统语言繁体
IosLocalUtil.h #ifndef __IOS_LOCALUTIL_H__ #define __IOS_LOCALUTIL_H__ class IosLocalUtil{ public: s ...
- 使用node.js创建第一个应用
node.js应用组成部分 1.引入require模块:我们可以使用require指令来载入Node.js模块. 2.创建服务器:服务器可以监听客户端的请求,类似于Apache,Nninx等HTTP服 ...
- Linux使用期间命令积累
1.调出终端 Ctrl+Alt+t 2.sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,如halt,reboot,su等等. sudo apt-g ...
- hdu_4734_F(x)(数位DP水)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:给你一个F(x)的定义,然后给你a,b,问你在0到b包括b有多少个数满足F(x)<= ...
- mib.c
函数 sprint_realloc_by_type (E:\code\net-snmp-5.4.2.1\snmplib\mib.c)的主要作用是: 解析传入的mib中的节点,根据mib节点类型,将其 ...
- LeetCode OJ 292.Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- 第13章 Swing程序设计----JDialog窗体
JDialog窗体是Swing组件中的对话框 JDialog窗体的功能是从一个窗体中弹出另一个窗体,就像是在使用IE浏览器时弹出的确定对话框一样. 在应用程序中创建JDialog窗体需要实例化JDia ...
- Shell脚本,自动化发布tomcat项目【转载】
Shell脚本,自动化发布tomcat项目脚本. 1. vko2c_auto_build_by_scp.sh 文件内容: #---------------------start------------ ...
- td里的内容宽度自适应 及 鼠标放上显示标题div title
td里的内容自适应宽度, 用 width:100%控制 strRight+="<td bordercolor='#DEDEDE' width='500px' height='50px' ...