Divisibility
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10598   Accepted: 3787

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

field=source&key=Northeastern+Europe+1999">Northeastern Europe 1999



题目链接:

id=1745">http://poj.org/problem?id=1745



题目大意:给n个数,让他们通过加减运算。推断结果可不可能被k整除



题目分析:用dp[i][j]表示通过前i个数的运算得到的余数为j可不可能。先看求a % k。假设a > k,则a = n * k + b。(n * k + b) % k == 0 + b % k = a % k,所以当a > k时,对求余数有影响的部分是不能被整除的部分。因此对于每一个数我们能够做a[i] = a[i] > 0 ? (a[i] % k) : -(a[i] % k)的预处理,然后就是在dp[i - 1][j]的情况下。推出下一状态。下一状态有两种可能,加和减,减的时候防止出现负数加上个k再取余,初始化dp[0][a[0]]
= true最后仅仅要推断dp[n - 1][0]及前n个数通过加减运算是否能得到被k整除的值

#include <cstdio>
#include <cstring>
int const MAX = 10005; bool dp[MAX][105];
int a[MAX]; int main()
{
int n, k;
while(scanf("%d %d", &n, &k) != EOF)
{
memset(dp, false, sizeof(dp));
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
a[i] = a[i] > 0 ? (a[i] % k) : -(a[i] % k);
}
dp[0][a[0]] = true;
for(int i = 1; i < n; i++)
{
for(int j = 0; j <= k; j++)
{
if(dp[i - 1][j])
{
dp[i][(j + a[i]) % k] = true;
dp[i][(k + j - a[i]) % k] = true;
}
}
}
printf("%s\n", dp[n - 1][0] ? "Divisible" : "Not divisible");
}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ 1745 Divisibility (线性dp)的更多相关文章

  1. POJ 1745 Divisibility【DP】

    题意:给出n,k,n个数,在这n个数之间任意放置+,-号,称得到的等式的值能够整除k则为可划分的,否则为不可划分的. 自己想的是枚举,将所有得到的等式的和算出来,再判断它是否能够整除k,可是有1000 ...

  2. POJ 2479-Maximum sum(线性dp)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33918   Accepted: 10504 Des ...

  3. POJ 1745 Divisibility DP

    POJ:http://poj.org/problem?id=1745 A完这题去买福鼎肉片,和舍友去买滴~舍友感慨"这一天可以卖好几百份,每份就算赚一块钱..那么一个月..一年...&quo ...

  4. poj 3356 AGTC(线性dp)

    题目链接:http://poj.org/problem?id=3356 思路分析:题目为经典的编辑距离问题,其实质为动态规划问题: 编辑距离问题定义:给定一个字符串source,可以对其进行复制,替换 ...

  5. POJ 1745 Divisibility

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9476   Accepted: 3300 Desc ...

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

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

  7. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

  8. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  9. LightOJ1044 Palindrome Partitioning(区间DP+线性DP)

    问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...

随机推荐

  1. js中点击空白区域时文本框与隐藏层的问题

    当文本框获得焦点的时候,在文本框的下方显示一个浮动层. 当用户点击除了文本框和浮动层以外的网页空白处时,要隐藏浮动层. 当用户点击浮动层时,改变文本框的值. <!DOCTYPE html PUB ...

  2. VMware WorkStation安装时提示The MSI failed

    以前安装过其他版本的VMware workstation卸载不完全造成的 先把所有VMware相关服务关闭,然后打开注册表,搜索所有VMware相关键值,删除掉,然后再安装就可以了 前提是你机器上没有 ...

  3. JAVA insert() 插入字符串 reverse() 颠倒 delete()和deleteCharAt() 删除字符 replace() 替换 substring() 截取子串

    insert() 插入字符串 StringBuffer insert(int index,String str) StringBuffer insert(int index,char ch) Stri ...

  4. NYOJ 45 棋盘覆盖 模拟+高精度

    题意就不说了,中文题... 小白上讲了棋盘覆盖,于是我就挖了这题来做. 棋盘覆盖的推导不是很难理解,就是分治的思想,具体可以去谷歌下. 公式就是f(k) = f(k - 1) * 4 + 1,再化解下 ...

  5. Ubuntu12.04编译Android4.0.1源码全过程-----附wubi安装ubuntu编译android源码硬盘空间不够的问题解决

    昨晚在编译源码,make一段时间之后报错如下: # A fatal error has been detected by the Java Runtime Environment: # # SIGSE ...

  6. Excel阅读器NPOI

    什么是NPOI? NPOI 它是 POI 项目的 .NET 版本号. POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就能够在没有安装 Office ...

  7. android 编译调用C代码

    博客地址:www.zalezone.cn 前言 需求来源 这几天帮别人做一个简单的androidclient,也没什么功能,主要就是调用C代码来对手机的Wifi网络进行设置.于是也就引出了技术难点所在 ...

  8. eclipse luna 无法安装veloeclipse问题

    问题: 在eclipse 4.4(luna)版本号.安装veloeclipse 2.0.8时,在即将完毕的时候出现下面错误提示: An error occurred while installing ...

  9. TMS320F28335项目开发记录2_CCS与JTAG仿真器连接问题汇总

    CCS与仿真器连接问题 实际使用过程中.仿真器和CCS连接可能出现这样或那样的问题,或许你的连接非常成功,没碰到过什么问题.但我的问题的确不少,可能与电脑配置有关吧,也可能与人品有关吧. 以下的自己的 ...

  10. UVa 12459 - Bees&#39; ancestors

    称号:区区女性有父亲和母亲,区区无人机只有一个母亲,我问一个单纯的无人机第一n随着祖先的数量. 分析:递归.Fib序列. 状态定义:建立f(k)和m(k)分别用于第一k雌蜂和雄蜂的数量: 递推关系:f ...