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 a integer M indicating the number of cases to be analyzed. Then M couples of lines follow. For each one of this couples, 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

For each case in the input file, 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

2

4 7

17 5 -21 15

4 5

17 5 -21 15

Sample

Output Divisible

Not divisible

题目大意:给n个数,在这n个中放上‘+’  ‘-’使得结果能被k整除。

dp[i][j]表示 前i个数余数j 是否可能

/* ***********************************************
Author :guanjun
Created Time :2016/9/6 15:25:54
File Name :uva10036.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int dp[maxn][];
int a[maxn];
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t,n,k;
cin>>t;
while(t--){
scanf("%i %i",&n,&k);
for(int i=;i<=n;i++)scanf("%i",&a[i]),a[i]=abs(a[i]%k);
cle(dp);
dp[][]=;
for(int i=;i<=n;i++){
for(int j=;j<k;j++){
if(dp[i][j]){
dp[i+][(j-a[i]+k)%k]=;
dp[i+][(j+a[i])%k]=;
}
}
}
if(dp[n+][])puts("Divisible");
else puts("Not divisible");
}
return ;
}

Uva 10036 - Divisibility的更多相关文章

  1. uva 10036

    10036 - Divisibility 额..直接复制不过来,只好叙述一下了...t组样例,n个数(1-10000),k(2-100)是要取余的数,然后给出n个数第一个数前不能加正负号,其他的数前面 ...

  2. uva 10036 Problem C: Divisibility

    题意:能否在一个整数序列的每相邻的两项之间添加一个加减号,使得最终结果能被一个给定整数K<=100整除. dp[i][j]表示第i个数取余k为j的布尔值. #include <cstdio ...

  3. (DP)uva 10036 Problem C: Divisibility

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88171#problem/F 代码: #include <cstdio> ...

  4. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  7. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  8. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  9. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

随机推荐

  1. JMeter怎样测试WebSocket,示例演示(二)

    一.测试案例演示 以  http://www.websocket.org/echo.html 网站为例. 地址为:ws://echo.websocket.org 二.长连接的影响 1.没有勾选stre ...

  2. Oracle RAC 后台进程

    LMS  - Gobal         全局缓存服务进程 LMD  - Global Enqueue Service Daemon 全局查询服务守护进程 LMON -  全局服务器监控进程 LCK0 ...

  3. BZOJ 1176: [Balkan2007]Mokia KDtree

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin), ...

  4. 新一代 Linux 文件系统 btrfs 简介

    https://www.ibm.com/developerworks/cn/linux/l-cn-btrfs/ Btrfs 简介 文件系统似乎是内核中比较稳定的部分,多年来,人们一直使用 ext2/3 ...

  5. Mkdocs在html网页上看markdown

    目录 Mkdocs在html网页上看markdown 1. 本文目的 2. Mkdocs介绍 3. DEMO的演示 3.1 配置需求 3.2 安装mkdocs 3.3 新建工程 3.4 启动服务器 3 ...

  6. jquery 实现点评标签 类似淘宝大众点评的 快速准时 货品完好等

    111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...

  7. boostrap标签

    字体: <lead>:加强显示 <strong><b>:字体加粗 <i><em>:斜体字 .text-muted:提示,使用浅灰色(#999 ...

  8. 迭代器,生成器(generator)和Promise的“微妙”关系

    需要Promise源码版的朋友:传送链接 本文主要讲述(iterator)和生成器*/yield之间的联系和各自的用法,以及生成器的高配版本aysnc/await的使用. 大纲: 迭代器(iterat ...

  9. 洛谷——P1594 护卫队

    P1594 护卫队 题目描述 护卫车队在一条单行的街道前排成一队,前面河上是一座单行的桥.因为街道是一条单行道,所以任何车辆都不能超车.桥能承受一个给定的最大承载量.为了控制桥上的交通,桥两边各站一个 ...

  10. Spring 获取当前activeProfile

    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()) ...