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. POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29502   Accepted: 840 ...

  2. 06SpringMvc_适配器

    适配器的主要功能是去找控制器.Action实现了什么接口 本文案例实现的功能是:在页面上输入中文名字,然后在另外一个网页上显示出来. 案例结构:

  3. 【C#】WM 消息大全

    消息名 消息值 说明 WM_CREATE 0x0001 应用程序创建一个窗口 WM_DESTROY 0x0002 一个窗口被销毁 WM_MOVE 0x0003 移动一个窗口 WM_SIZE 0x000 ...

  4. [Erlang37]error/1 exit/1 exit/2 throw/1的区别

    1. error/1 主要是系统用来定义内部错误的: Erlang内建的run time error 一共有10种: function_clause/case_clause/if_clause/bad ...

  5. Silverlight中使用MVVM:DataGrid中触发Button的Click事件

    方法1.使用RelativeSource向上查找DataContext中的命令,但是需要注意的是命令绑定需要写全 类似: DataContext.ReLoadCommand<Button Gri ...

  6. HoloLens开发手记 - Unity之Spatial Sounds 空间声音

    本文主要讲述如何在项目中使用空间声音特性.我们主要讲述必须的插件组件和Unity声音组件和属性的设置来确保空间声音的实现. Enabling Spatial Sound in Unity 在Unity ...

  7. Win10/UWP开发—使用Cortana语音指令启动前台App

    这两天进群(53078485)找大咖的童鞋比较多,只是大咖比较忙,目前Demo还没有要到,这里先给大家转载一篇Aran大咖的博客学习下,以下是原文: Win10开发中最具有系统特色的功能点绝对少不了集 ...

  8. CodeIgniter框架中关于URL(index.php)的那些事

    最近,在做自己的个人网站时,采用了轻量级的php框架CodeIgniter.乍一看上去,代码清晰简洁,MVC模型非常容易维护.开发时我采用的工具是Netbeans IDE 8.0,当然,本文的内容和开 ...

  9. JavaScript实例---表格隔行变色以及移入鼠标高亮

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  10. 中国IT 未来何在

      许久之前,已对国内IT业的一些问题颇有看法,而今又恰逢360与AV-C的纠缠,实在忍不住要发发牢骚.IT在中国,发展不过二十来年,却以迅雷之速横扫各个领域,令人感叹,此成就是不可否认的:然而,发展 ...