POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13431 | Accepted: 4774 |
Description
17 + 5 + -21 - 15 = -14
17 + 5 - -21 + 15 = 58
17 + 5 - -21 - 15 = 28
17 - 5 + -21 + 15 = 6
17 - 5 + -21 - 15 = -24
17 - 5 - -21 + 15 = 48
17 - 5 - -21 - 15 = 18
We call the sequence of integers divisible by K if + or - operators can be placed between integers in the sequence in such way that resulting value is divisible by K. In the above example, the sequence is divisible by 7 (17+5+-21-15=-14) but is not divisible by 5.
You are to write a program that will determine divisibility of sequence of integers.
Input
The second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it's absolute value.
Output
Sample Input
4 7
17 5 -21 15
Sample Output
Divisible
Source
题目大意:给N个数字和一个K,把N个数字加加减减后得到的数字是否能整除K。
大概思路:
一、暴力,全排列,爆炸,out!
二、0/1背包
状态:bool dp[ i ][ x ], 长度为 i 的序列的加减结果模 K 的后的 X 为真或为假
状态转移: dp[ i+1 ][ (((x-num [i+1])%K)+K)%K ] = dp[ i ][ x ] 减第 i+1 个数
dp[ i+1 ][ (((x+num [i+1])%K)+K)%K ] = dp[ i ][ x ] 加第 i+1 个数
AC code (1224k 360ms):
///POJ 1745 【0/1背包】
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e4+;
const int MAXK = ; int num[MAXN];
bool dp[MAXN][MAXK];
int N, K; void slv()
{
memset(dp, , sizeof(dp));
dp[][((num[]%K)+K)%K] = true;
for(int i = ; i < N; i++)
{
for(int p = ; p < K; p++)
{
if(dp[i][p])
{
dp[i+][(((p+num[i+])%K)+K)%K] = true;
dp[i+][(((p-num[i+])%K)+K)%K] = true;
}
}
}
if(dp[N][]) printf("Divisible\n");
else printf("Not divisible\n");
} int main()
{
scanf("%d%d", &N, &K);
for(int i = ; i <= N; i++)
{
scanf("%d", &num[i]);
}
slv();
return ;
}
POJ 1745 【0/1 背包】的更多相关文章
- POJ 1636 Prison rearrangement DFS+0/1背包
题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS Memor ...
- POJ 1745 线性和差取余判断
POJ 1745 线性和差取余判断 题目大意:每个数都必须取到,相加或相减去,问所有的方案最后的得数中有没有一个方案可以整除k 这个题目的难点在于dp数组的安排上面 其实也就是手动模仿了一下 比如 一 ...
- poj1417 带权并查集+0/1背包
题意:有一个岛上住着一些神和魔,并且已知神和魔的数量,现在已知神总是说真话,魔总是说假话,有 n 个询问,问某个神或魔(身份未知),问题是问某个是神还是魔,根据他们的回答,问是否能够确定哪些是神哪些是 ...
- P1417 烹调方案 (0/1背包+贪心)
题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...
- 洛谷 P1064 金明的预算方案 (有依赖的0/1背包)
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”. ...
- 浙大PAT CCCC L3-001 凑零钱 ( 0/1背包 && 路径记录 )
题目链接 分析 : 就是一个 0/1 背包,但是需要记录具体状态的转移情况 这个可以想象成一个状态转移图,然后实际就是记录路径 将状态看成点然后转移看成边,最后输出字典序最小的路径 这里有一个很巧妙的 ...
- 牛客网 TaoTao要吃鸡 ( 0/1背包变形 )
题意 : 题目链接 分析 : 如果没有 BUG (即 h == 0 的时候)就是一个普通的 0 / 1 背包 需要讨论一下 h != 0 的情况 此时有就相当于有物品是有特权的 而且背包装有特权的物 ...
- poj 1837 Balance (0 1 背包)
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10326 Accepted: 6393 题意:给你n个挂 ...
- POJ 1155 (树形DP+背包+优化)
题目链接: http://poj.org/problem?id=1155 题目大意:电视台转播节目.对于每个根,其子结点可能是用户,也可能是中转站.但是用户肯定是叶子结点.传到中转站或是用户都要花钱, ...
随机推荐
- Java基础19-封装、方法重载、构造方法(构造函数)
1.封装 封装就是把不想或者不该告诉别人的东西隐藏起来,把可以告诉别人的公开 做法:修改属性的访问权限来限制对属性的访问.并为每一个属性创建一对取值方法和赋值方法,用于对这些属性的访问 class D ...
- 【ORACLE】sqlplus使用记录
1.设置输出长度 SEGMENT_NAME--------------------------- BYTES----------TZ01_LOGIN_DATA 20971520 TZ02_EP_GAT ...
- 024-cxf.xml配置文件模板
版本一 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...
- 3d旋转卡牌
做成向中心缩放就行了,和旋转效果一样的
- APP测试点集合
一.功能性测试: (1)根据产品需求文档编写测试用例 (2)软件设计文档编写用例 二.兼容性适配性测试: (1)Android.iOS版本的兼容性 (2)手机分辨率兼容性 (3)网络的兼容性:2G/3 ...
- OpenLayers3 实现自定义放大缩小滑块,自定义方向按钮
由于项目需要,需要自定义滑块.为此记录如下: <div id="map" class = "map"> <div id = "zoo ...
- 利用ajax短轮询+php与服务器交互制作简易即时聊天网站
主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Server-sent Events). 本文主要介绍ajax短轮询的简易实现方式. 看懂此文 ...
- css之margin,padding的百分比
注意:上下内边距与左右内边距一致:即上下内边距的百分数会相对于父元素宽度设置,而不是相对于高度. PS:而且是基于父元素内容的宽度(width属性的大小),不是基于父元素整个框架的宽度
- configparser模块——配置文档
configparser模块用于生成和修改常见配置文档. 预制配置文件:conf.ini [DEFAULT] ServerAliveInterval = 45 Compression = yes Co ...
- 闭包中的this
var name="pushline";//全局变量 var obj=new Object(); obj.name="jms"; obj.getName=fun ...