Divisibility

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 14084 Accepted: 4989

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

Northeastern Europe 1999

这是简单DP水题,WA了几次,初学的时候这个题还是有必要看看!!

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <cstdlib>
using namespace std;
#define mod(a) (a)<0? (-(a))%k:(a)%k
#define mem(a,b) memset((a),(b),sizeof(a));
int dp[10005][105];
int a[10005]; int main(){
// freopen("test.txt","r",stdin);
mem(dp,0);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
dp[1][mod(a[1])]=1;
for(int i=2;i<=n;i++)
for(int j=0;j<k;j++)
if(dp[i-1][j])
{
dp[i][mod(j+a[i])]=1;
dp[i][mod(j-a[i])]=1;
}
if(dp[n][0]) cout<<"Divisible";
else cout<<"Not divisible";
return 0;}

1745 Divisibility的更多相关文章

  1. POJ 1745 Divisibility (线性dp)

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

  2. POJ 1745 Divisibility

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

  3. POJ 1745 Divisibility DP

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

  4. POJ 1745 Divisibility【DP】

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

  5. POJ 1745 【0/1 背包】

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

  6. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  7. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  8. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  9. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

随机推荐

  1. python 天天生鲜项目

    python 天天生鲜项目 django版:https://github.com/Ivy-1996/fresh flask版:https://github.com/Ivy-1996/flask-fre ...

  2. javascript入门 之 zTree(十三 移动/复制事件)

    <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - copyNode / moveNode</T ...

  3. 【Canvas】(1)---概述+简单示例

    Canvas---概述+简单示例 如果通俗的去理解Canvas,我们可以去理解成它类似于我们电脑自带的画图工具一样,canvas首先是选择一块画布,然后在这个画布上描绘我们想画的东西,画好后展示给用户 ...

  4. Python 1基础语法一(注释、行与缩进、多行语句、空行和代码组)

    一.注释Python中单行注释以 # 开头,实例如下: # 第一个注释 print ("Hello, Python!") # 第二个注释 输出结果为: ============== ...

  5. Java Random 随机数

    package myrandom; import java.util.Random; /* * Random:用于产生随机数 * * 使用步骤: * A:导包 * import java.util.R ...

  6. CVPR2020| 阿里达摩院最新力作SA-SSD

    作者:蒋天园 Date:2020-04-16 来源:SA-SSD:阿里达摩院最新3D检测力作(CVPR2020) Brief 来自CVPR2020的研究工作,也是仅仅使用Lidar数据进行3D检测的文 ...

  7. spark模型运行时无法连接摸个excutors异常org.apache.spark.shuffle.FetchFailedException: Failed to connect to xxxx/xx.xx.xx.xx:xxxx

    error:org.apache.spark.shuffle.FetchFailedException: Failed to connect to xxxx/xx.xx.xx.xx:xxxx 定位来定 ...

  8. pgsql的使用

    Deepin上面pgsql的启动 service postgresql start 停止 service postgresql stop 查看pgsql的版本 psql --version

  9. 查看jdk 线程 日志

    命令:jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令 这些命令 必须 在 linux jdk bin 路径 下执行 eq: ./jstack 10303 即可  如果想把 ...

  10. 1196F - K-th Path

    题目链接: http://codeforces.com/problemset/problem/1196/F 题目大意::无向图,求任意两点第k短的路径 思路:按照边的大小,只保留前K个边,如果不连同的 ...