Divisibility
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11151   Accepted: 3993

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
题意:输入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背包思想)的更多相关文章

  1. 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 ...

  2. SPOJ RENT 01背包的活用+二分

    这个题目给定N航班的发出时间和结束时间以及价值,要求不冲突时间的最大价值 第一时间想到经典的N方DP,即对航班按发出时间排一下序之后每个i对前面的都扫一遍 时间过不了N有10万,只能想优化了,一开始想 ...

  3. HDU 3446 有贪心思想的01背包

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  4. hdu–2369 Bone Collector II(01背包变形题)

    题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...

  5. DP:Cow Exhibition(POJ 2184)(二维问题转01背包)

        牛的展览会 题目大意:Bessie要选一些牛参加展览,这些牛有两个属性,funness和smartness,现在要你求出怎么选,可以使所有牛的smartness和funness的最大,并且这两 ...

  6. hdu3496 二维01背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M ...

  7. hdu 2955 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...

  8. Balance(01背包)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9163   Accepted: 5617 Description Gigel ...

  9. HDU -2546饭卡(01背包+贪心)

    这道题有个小小的坎,就是低于5块不能选,大于5块,可以任意选,所以就在初始条件判断一下剩余钱数,然后如果大于5的话,这时候就要用到贪心的思想,只要大于等于5,先找最大的那个,然后剩下的再去用背包去选择 ...

随机推荐

  1. Linux下用信号量实现对共享内存的访问保护

    转自:http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108768.html 最近一直在研究多进程间通过共享内存来实现通信的事情,以便高 ...

  2. Linux 图形化操作

    //Linux图形化操作 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  3. Linux 网络编程七(非阻塞socket:epoll--select)

    阻塞socket --阻塞调用是指调用结果返回之前,当前线程会被挂起.函数只有在得到结果之后才会返回. --对于文件操作 read,fread函数调用会将线程阻塞(平常使用read感觉不出来阻塞, 因 ...

  4. 突然想起android与mfc差异

    两者都可以算作是客户端程序,都是做上位机用的.而且都是被动执行. 相同点: 1.MFC中,它是由 project的名字 里面的某个成员函数来初始化,窗体,以及窗体里面的变量. 后面都是监听消息循环.数 ...

  5. Linux常用指令---netstat(网络端口)

    netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP ...

  6. 学习Shell脚本编程(第3期)_在Shell程序中使用的参数

    位置参数 内部参数 如同ls命令可以接受目录等作为它的参数一样,在Shell编程时同样可以使用参数.Shell程序中的参数分为位置参数和内部参数等. 3.1 位置参数 由系统提供的参数称为位置参数.位 ...

  7. Solr(5.1.0) 与Tomcat 从0开始安装与配置

    1.什么是Solr? Solr是一个基于Lucene的Java搜索引擎服务器.Solr 提供了层面搜索.命中醒目显示并且支持多种输出格式(包括 XML/XSLT 和 JSON 格式).它易于安装和配置 ...

  8. 关于那些难改的bug

    多年的测试经验中,经常发现有这么一种现象:总有些提了的bug不能顺利的被修复.这些bug往往有4个走向: 1.在被发现的版本中最终被解决,但中途花费较多周折. 2.有计划的在后续的版本中被解决. 3. ...

  9. 我的第二个app上线:术购管家

    忙了两周写完的app,终于发布了,可是等上线竟然等了两周多,今天终于上线了,一路顺畅,没有被打回过...

  10. 在C#中使用官方驱动操作MongoDB ---转载

    http://blog.csdn.net/dannywj1371/article/details/7440916