POJ1745Divisibility(01背包思想)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11151 | Accepted: 3993 |
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
integers, N and K (1 <= N <= 10000, 2 <= K <= 100) separated by a
space.
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
sequence of integers is divisible by K or "Not divisible" if it's not.
Sample Input
4 7
17 5 -21 15
Sample Output
Divisible
题意:输入n个数,通过添加+和-能否是的结果对k取余为0
思路:智商再次背碾压
首先一个数,不用说,第一个数之前不用加符号就是本身,那么本身直接对K取余,
那么取17的时候有个余数为2
然后来了一个5,
(2 + 5)对7取余为0
(2 - 5)对7取余为4(将取余的负数变正)
那么前2个数有余数0和4
再来一个-21
(0+21)对7取余为0
(0-21)对7取余为0
(4+21)对7取余为4
(4-21)对7取余为4
再来一个-15同样是这样
(0+15)%7 = 1
(0-15)%7 = 6
(4+15)%7 = 5
(4-15)%7 = 3
同理可以找到规律,定义dp[i][j]为前i个数进来余数等于j是不是成立,1为成立,0为不成立
那么如果dp[N][0]为1那么即可以组成一个数对K取余为0
初始化dp为0
然后dp[1][a[1]%k] = 1
for i = 2 to N do
for j = 0 to K do
if(dp[i - 1][j])
dp[i][(j + a[i])%k] = 1;
dp[i][(j - a[i])%k] = 1;
if end
for end
for end
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[ + ][ + ],a[ + ];
int n,k;
int mod(int x)
{
if(x < )
{
return x + k;
}
else
return x;
}
int main()
{
while(scanf("%d%d", &n, &k) != EOF)
{
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
memset(dp, , sizeof(dp));
dp[][ mod(a[] % k) ] = ;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= k; j++)
{
if(dp[i - ][j])
{
dp[i][ mod((j + a[i]) % k)] = ;
dp[i][ mod((j - a[i]) % k)] = ;
}
}
}
if(dp[n][])
printf("Divisible\n");
else
printf("Not divisible\n");
} return ;
}
POJ1745Divisibility(01背包思想)的更多相关文章
- 01背包 Codeforces Round #267 (Div. 2) C. George and Job
题目传送门 /* 题意:选择k个m长的区间,使得总和最大 01背包:dp[i][j] 表示在i的位置选或不选[i-m+1, i]这个区间,当它是第j个区间. 01背包思想,状态转移方程:dp[i][j ...
- SPOJ RENT 01背包的活用+二分
这个题目给定N航班的发出时间和结束时间以及价值,要求不冲突时间的最大价值 第一时间想到经典的N方DP,即对航班按发出时间排一下序之后每个i对前面的都扫一遍 时间过不了N有10万,只能想优化了,一开始想 ...
- HDU 3446 有贪心思想的01背包
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- hdu–2369 Bone Collector II(01背包变形题)
题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...
- DP:Cow Exhibition(POJ 2184)(二维问题转01背包)
牛的展览会 题目大意:Bessie要选一些牛参加展览,这些牛有两个属性,funness和smartness,现在要你求出怎么选,可以使所有牛的smartness和funness的最大,并且这两 ...
- hdu3496 二维01背包
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M ...
- hdu 2955 01背包
http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...
- Balance(01背包)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9163 Accepted: 5617 Description Gigel ...
- HDU -2546饭卡(01背包+贪心)
这道题有个小小的坎,就是低于5块不能选,大于5块,可以任意选,所以就在初始条件判断一下剩余钱数,然后如果大于5的话,这时候就要用到贪心的思想,只要大于等于5,先找最大的那个,然后剩下的再去用背包去选择 ...
随机推荐
- 安装依赖包时--save-dev以及-save的区别及意义
首先这样做会生成一个package.json的配置文件,并在里面增加相应的版本信息,以后运行程序时,安装依赖包可以直接 npm install或者你有安装淘宝镜像,那就cnpm install 就一 ...
- 九、Foundation框架中的NSString常用方法
一.NSString的创建 方式1创建常量字符串 NSString *st = @"this is string!"; //这种方式创建的字符串不需要释放 方式2创建空字符串,给予 ...
- Netty关闭客户端
在启动客户端的时候,我们一般会 channelFuture.channel().closeFuture().sync(); 这是一段阻塞的代码,除非链路断了,否则是不会终止的,我们可以在handler ...
- C# winform版 nbtstat
参考:http://www.cnblogs.com/geqinggao/archive/2013/01/21/2869644.html 一.nbtstat命令 显示基于 TCP/IP 的 NetBIO ...
- ssh scp 复制文件和文件夹
三,复制文件或目录命令: 复制文件: (1)将本地文件拷贝到远程 scp 文件名用户名@计算机IP或者计算机名称:远程路径 本地192.168.1.8客户端 scp /root/install ...
- linux输入输出重定向
http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html 在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据 ...
- Linux 网络编程五(UDP协议)
UDP和TCP的对比 --UDP处理的细节比TCP少. --UDP不能保证消息被传送到目的地. --UDP不能保证数据包的传递顺序. --TCP处理UDP不处理的细节. --TCP是面向连接的协议 - ...
- OAF 中对文字实现html效果及对超级长文本实现默认换行,对只读的messageTextInput中的内容自动换行
今天遇到一个需求,客户注册页面客户化了一个超级长的注册须知,内容很多.但是样式相对又要做起来好看点. 注册须知的内容使用多个message拼接而成. 老大说rawText支持html样式,于是我想到了 ...
- java 顺序 读写 Properties 配置文件
java 顺序 读写 Properties 配置文件 支持中文 不乱码 java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...
- CUDA编程学习(一)
/****c code****/ #include<stdio.h> int main() { printf("Hello world!\n); ; } /****CUDA co ...