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

题意:给你一列整数,在整数间加‘ + ’ 或 ‘ - ‘,使这个算式的值能被k整除。

用dp[ i ][ j ] 表示加上或减去第 i 个数后,所得值取模后的值能否为 j ,所以dp为bool型即可。

状态转移方程:dp[ i ][ abs( j + num[i]) % k] = true;

dp[ i ][ abs( j -  num[i]) % k] = true; (当然,必须满足dp[ i - 1 ][ j ] == true, 才能进行状态转移)

边界条件:dp[ 0 ][ 0 ] = true;

 #include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"map"
#include"set"
#include"stack"
#include"queue"
using namespace std;
const int ms=;
const int mn=;
bool dp[ms][mn];
int a[ms];
int N,K;
void solve()
{
memset(dp,false,sizeof(dp));
dp[][]=true;
for(int i=;i<=N;i++)
for(int j=;j<K;j++)
if(dp[i-][j])
{
dp[i][abs(j+a[i])%K]=true; //涉及一点数论
dp[i][abs(j-a[i])%K]=true;
}
if(dp[N][])
cout<<"Divisible"<<endl;
else
cout<<"Not divisible"<<endl;
return ;
}
int main()
{
cin>>N>>K;
for(int i=;i<=N;i++)
cin>>a[i];
solve();
return ;
}

Divisibility的更多相关文章

  1. cf306 C. Divisibility by Eight(数学推导)

    C. Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. 周赛-Clique in the Divisibility Graph 分类: 比赛 2015-08-02 09:02 23人阅读 评论(3) 收藏

    Clique in the Divisibility Graph time limit per test1 second memory limit per test256 megabytes inpu ...

  3. Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力

    C. Divisibility by Eight Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  4. Divisibility by Eight (数学)

    Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. codeforces 630J Divisibility

    J. Divisibility time limit per test 0.5 seconds memory limit per test 64 megabytes input standard in ...

  6. Codeforces Testing Round #12 A. Divisibility 水题

    A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  7. HDU 3335 Divisibility (DLX)

    Divisibility Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  8. light oj 1078 - Integer Divisibility

    1078 - Integer Divisibility   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

  9. POJ 1745 Divisibility (线性dp)

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

随机推荐

  1. CSS计算样式的获取

    一般来说我们获取CSS的样式的时候会优先采用Elment.style.cssName 这种方法,这种方法类似于对象设置get,set属性获取,例如Elment.style.cssName是获取,Elm ...

  2. c++ Map使用

    引入头文件: #include <map>1.初始化map<int, int> a, b;map<sting, int> a, b;2.添加数据 map<in ...

  3. 一个考察for循环题 讨论一下

    一道Java程序题,主要是考察for循环如下所示: public class Test { static boolean fun(char c) { System.out.print(c); retu ...

  4. elasticsearch配置文件解析

    配置es的集群名称 : cluster.name:  fcz_es

  5. 采用现代Objective-C

    多年来,Objective-C语言已经有了革命性的发展.虽然核心理念和实践保持不变,但语言中的部分内容经历了重大的变化和改进.现代化的Objective-C在类型安全.内存管理.性能.和其他方面都得到 ...

  6. POJ 1847 Tram (最短路)

    Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...

  7. [iOS 多线程 & 网络 - 2.1] - 解析json

    A.iOS中json的基本使用 1.解析json数据 (1)json反序列化 对象{}格式 {key : value, key : value,...} 的键值对的结构可以反序列化为OC中的NSDic ...

  8. 基于OpenCV的iOS开发笔记(1)

    本系列文章采用的的开发环境为: 1)Xcode 6 2)OpenCV for iOS 3.0.0 -------------------分割线---------------------------- ...

  9. android开发教程(八)——环境搭建之java-ndk

    目录 android ndk是android用于开发本地代码的开发工具包.它提供C/C++交叉编译工具.android内核.驱动.已有的C/C++代码,都需要ndk来支持开发. 目前支持以下平台:ar ...

  10. "file:///" file 协议

    [问题] 在WLW中拖入本地图片文件,然后调试过程中,选中对应图片,看到获得的对应的html源码中,图片地址是这样的: href="file:///C:/Users/CLi/AppData/ ...