题目链接: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. 使用Redis 配置替换fastjson 反序列化报错 com.alibaba.fastjson.JSONException: autoType is not support

    新建的GenericFastJson2JsonRedisSerializer里面添加白名 添加: static {        ParserConfig.getGlobalInstance().ad ...

  2. 【3dsMax安装失败,如何卸载、安装3dMax 2013?】

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  3. 实现类似tail -f file功能

    python版本py3 tail -f file是打印最后10行,然后跟踪文件追加的内容打印出来. python3 以为本方式打开的话,不能回退(f.seek(-1,1)),所有以'rb'方式打开文件 ...

  4. bootstrap框架的使用

    1.默认修改input输入框激活的颜色(充电桩) .form-control:focus, .ms-choice:focus, input[type=text]:focus, input[type=p ...

  5. OpenGL进阶之Instancing

    Instancing Instancing绘制我想很多童鞋都不陌生,这个技术主要用来快速渲染大量相同的几何体,可以大大提高绘制效率.每个instance在shader中都有一个独一无二的索引,可以用来 ...

  6. JVM 类加载全过程

    类加载机制 - JVM把class文件加载到内存中 并对数据进行 校验,解析,初始化,最终形成JVM可以直接使用的java类型的过程 详细过程  加载→ 验证→ 准备→ 解析 → 初始化→ 使用 → ...

  7. tomcat局域网内发布html

    1. 保证tomcat装好,启动 验证:浏览器输入:localhost:8080,看到下面页面表示成功 2. 把html文件或包含html的目录拷到Apach安装目录的Root目录下 (例如:C:\P ...

  8. Linux VFS机制简析(二)

    Linux VFS机制简析(二) 接上一篇Linux VFS机制简析(一),本篇继续介绍有关Address space和address operations.file和file operations. ...

  9. js中判断对象是否存在

    s中判断对象是否存在,写法有很多种: 第一种:if (!myObj) { var myObj = { }; }第二种:var global = this;  if (!global.myObj) {  ...

  10. 使用网络技术---WebView

    混合技术 1.使用WebView 权限声明: 定义WebView 填充网页: webView=findViewById() //启用js WebSetting seter = webView.sett ...