Divisibility
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11622   Accepted: 4178

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

思路:定义bool数组dp[10005][105],dp[i][j]表示前i+1个数所形成的和模上k是否为j.状态转移方程:if(dp[i-1][j]){ dp[i][mod(j+a[i],k)]=true;dp[i][mod(j-a[i],k)]=true;}
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
bool dp[MAXN][];
int a[MAXN];
int n,k;
int mod(int x,int m)
{
x%=m;
if(x<) x+=m;
return x;
}
int main()
{ while(scanf("%d%d",&n,&k)!=EOF)
{
memset(dp,false,sizeof(dp));
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
dp[][mod(a[],k)]=true;
for(int i=;i<n;i++)
{
for(int j=;j<k;j++)
{
if(dp[i-][j])
{
dp[i][mod(j+a[i],k)]=true;
dp[i][mod(j-a[i],k)]=true;
}
}
} if(dp[n-][])
{
printf("Divisible\n");
}
else
{
printf("Not divisible\n");
}
}
return ;
}

POJ1745动态规划的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. String类运算符重载,自己实现

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  2. Qt实现简单的单例模式

    单例模式十分的常见也很常用,Boost库中就有单例的泛型实现,Qt中,可以利用原子指针来实现一个单例模式: class SingleTon{ public: static SingleTon & ...

  3. SpringAnnotation之配置AnnotationXML文件

    配置Annotation的环境:只需修改applicationContext.xml文件即可 1 2 3 4 5 6 7 8 9 10 11 <?xml version="1.0&qu ...

  4. C++中几个值得分析的小问题(1)

    下面3个小问题都是我认为C++ Beginner应该能够解答或辨别清楚的.希望我们能通过题目挖掘更多的信息,而不仅仅局限在解题.我最喜欢说的话:能力有限,所以作为抛砖引玉,希望共同讨论,指出错误. 另 ...

  5. PostgreSQL 系统参数调整及并行设置(转)

    转自:https://yq.aliyun.com/teams/5 OS 准备 # yum -y install coreutils glib2 lrzsz sysstat e4fsprogs xfsp ...

  6. Sql Server- 性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON

    1.前言 对于优化SQL语句或存储过程,以前主要是用如下语句来判断具体执行时间,但是SQL环境是复杂多变的,下面语句并不能精准判断性能是否提高:如果需要精确知道CPU.IO等信息,就无能为力了. 1 ...

  7. The Interview Outline

    ************************* 一.基础部分************************* 1.1 常用数据类型 - 字符串 split/strip/replace/find/ ...

  8. 如何让PPT播放时仅电脑显示备注页,而投影仪不显示

    完全可以!第一步:在电脑上右键点击桌面选择属性,进入显示属性选着设置,点击2号屏(前提已连接投影仪或第2显示器),并且在“将WINDOS桌面扩展到改监视器上”(这个关键)前面打钩,且自主选择分辨率,应 ...

  9. echarts-detail--柱状图

    一:柱形图 1.Echarts-柱状图柱图宽度设置-----只需要设置series中的坐标系属性barWidth就可以 /** * 堆积柱状图 * @param xaxisdata x轴:标签(数组) ...

  10. 将sass快速引入到移动端项目中加速开发

    本文以上图为例子: 首先在根目录创建一个sass文件,在sass文件中分别创建4个文件夹bsae,pages,libs,style; base下存放:样式重置_normalize.scss; 自己定义 ...