题目链接:http://poj.org/problem?id=1745

Divisibility
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13431   Accepted: 4774

Description

Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expressions that evaluate to different values. Let us, for example, take the sequence: 17, 5, -21, 15. There are eight possible expressions: 17 + 5 + -21 + 15 = 16 
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 first line of the input file contains two 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

Write to the output file the word "Divisible" if given 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

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 背包】的更多相关文章

  1. POJ 1636 Prison rearrangement DFS+0/1背包

    题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS   Memor ...

  2. POJ 1745 线性和差取余判断

    POJ 1745 线性和差取余判断 题目大意:每个数都必须取到,相加或相减去,问所有的方案最后的得数中有没有一个方案可以整除k 这个题目的难点在于dp数组的安排上面 其实也就是手动模仿了一下 比如 一 ...

  3. poj1417 带权并查集+0/1背包

    题意:有一个岛上住着一些神和魔,并且已知神和魔的数量,现在已知神总是说真话,魔总是说假话,有 n 个询问,问某个神或魔(身份未知),问题是问某个是神还是魔,根据他们的回答,问是否能够确定哪些是神哪些是 ...

  4. P1417 烹调方案 (0/1背包+贪心)

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  5. 洛谷 P1064 金明的预算方案 (有依赖的0/1背包)

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”. ...

  6. 浙大PAT CCCC L3-001 凑零钱 ( 0/1背包 && 路径记录 )

    题目链接 分析 : 就是一个 0/1 背包,但是需要记录具体状态的转移情况 这个可以想象成一个状态转移图,然后实际就是记录路径 将状态看成点然后转移看成边,最后输出字典序最小的路径 这里有一个很巧妙的 ...

  7. 牛客网 TaoTao要吃鸡 ( 0/1背包变形 )

    题意 : 题目链接 分析 :  如果没有 BUG (即 h == 0 的时候)就是一个普通的 0 / 1 背包 需要讨论一下 h != 0 的情况 此时有就相当于有物品是有特权的 而且背包装有特权的物 ...

  8. poj 1837 Balance (0 1 背包)

    Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10326   Accepted: 6393 题意:给你n个挂 ...

  9. POJ 1155 (树形DP+背包+优化)

    题目链接: http://poj.org/problem?id=1155 题目大意:电视台转播节目.对于每个根,其子结点可能是用户,也可能是中转站.但是用户肯定是叶子结点.传到中转站或是用户都要花钱, ...

随机推荐

  1. 97 条 Linux 运维工程师常用命令总结[转]

    1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件 -A 通-a,但不列出"."和".." -l 列出 ...

  2. 昨天太晚了,今天教你用Debug模式来分析程序执行顺序

    还是以昨天的XML文件解析来做栗子,希望通过这个好吃的栗子可以举一反三 学会用debug来看源码和找Bug 事件类型主要有五种START_DOCUMENT:xml头的事件类型    = 0END_DO ...

  3. 使用Serva通过网络PXE方式安装Windows10/CentOS

    下载Servahttp://www.vercot.com/~serva/download.html也可以从本文附件下载Serva_Community_64_v3.0.0.zip,这是社区版,使用50m ...

  4. 数据库保存session

    一般情况下,php.ini里的session.save_handler默认是file,也就是用文件来保存session,这种方式有几个缺点: 1.如果单靠session自己的垃圾回收机制,时间久了,保 ...

  5. Nginx+Keepalived配置

    1. Nginx安装 (1) 环境:分别在2台服务器上部署nginx且步骤一致: 如192.138.86.1和192.138.86.2 (2) 下载官网最新稳定版,地址:https://nginx.o ...

  6. FZU 2221—— RunningMan——————【线性规划】

     Problem 2221 RunningMan Accept: 17    Submit: 52Time Limit: 1000 mSec    Memory Limit : 32768 KB  P ...

  7. 【一】JMeter的介绍安装和使用

    利用JMeter进行性能测试 一.JMeter介绍二.Jmeter安装三.工作原理四.脚本录制五.运行JMeter进行测试六.JMeter主要组件介绍七.参数化设置八.动态数据关联九.使用插件进行服务 ...

  8. 关于微信小程序的动态跳转

    最近在研究微信小程序.在做一个简单的购物小程序时,遇到一个问题:如何通过扫码实现动态的跳转页面功能, 通过研究终于找到了解决方法: 首先当然要实现扫码解析功能js的代码: click: functio ...

  9. php 不用第三个变量 交换两个变量的值汇总

    //方法一:$a ="abc";$b="def"; $a = $a^$b;$b = $b^$a;$a = $a^$b; //方法二:list($a, $b)= ...

  10. jdbc封装DBUtil

    1.编写实体类User public class User { private Integer id; private String username; private Integer age; pr ...