Uva 10036 - Divisibility
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的更多相关文章
- uva 10036
10036 - Divisibility 额..直接复制不过来,只好叙述一下了...t组样例,n个数(1-10000),k(2-100)是要取余的数,然后给出n个数第一个数前不能加正负号,其他的数前面 ...
- uva 10036 Problem C: Divisibility
题意:能否在一个整数序列的每相邻的两项之间添加一个加减号,使得最终结果能被一个给定整数K<=100整除. dp[i][j]表示第i个数取余k为j的布尔值. #include <cstdio ...
- (DP)uva 10036 Problem C: Divisibility
链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88171#problem/F 代码: #include <cstdio> ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- 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 ...
随机推荐
- 安卓app测试之Monkey日志分析
转:原文:https://blog.csdn.net/a136332462/article/details/76066909 一.一般测试结果分析-搜索关键字: 1.无响应问题可以在日志中搜索 “A ...
- Eclipse添加背景图片
①前言:我本人是一个比较喜新厌旧的人,经常换头像,背景图片之类的,喜欢折腾,哈哈,所以每次都上网查怎么修改背景图片比较麻烦,这里也记录一下怎么修改. ②原文链接:https://yq.aliyun.c ...
- Linux学习笔记记录(二)
- linux nethogs-终端下的网络流量监控工具
推荐:更多linux 性能监测与优化 关注:linux命令大全 有很多适用于Linux系统的开源网络监视工具.比如说,你可以用命令iftop来检查带宽使用情况.netstat用来查看接口统计报告,还有 ...
- 高德地图将字符串地址转为经纬度的一个demo
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...
- ebay 店铺状态
OrderStatusCodeType @XmlEnumValue("Active") @XmlEnumValue("Inactive") @XmlEnumVa ...
- Spring AOP 学习(五)
1. 使用动态代理实现AOP package com.atguigu.spring.aop; import java.lang.reflect.InvocationHandler; import ja ...
- Android layer-list(1)
Android layer-list(1) Android layer-list,顾名思义,实现列表组合后形成的图层,写一个例子. activity_main.xml文件: <?xml v ...
- noip模拟赛 列车调度
[ 问题描述 ] 有N辆列车,标记为1,2,3,…,N.它们按照一定的次序进站,站台共有K个轨道,轨道遵从 先进先出的原则.列车进入站台内的轨道后可以等待任意时间后出站,且所有列车不可后退.现在要使出 ...
- UVAL - 6755 - Swyper Keyboard
先上题目: https://icpcarchive.ecs.baylor.edu/external/67/6755.pdf 题目复制起来比较麻烦. 题意:定义一种操作:给出一个字符串,然后手指就按照给 ...