1745 Divisibility
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的更多相关文章
- POJ 1745 Divisibility (线性dp)
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10598 Accepted: 3787 Des ...
- POJ 1745 Divisibility
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9476 Accepted: 3300 Desc ...
- POJ 1745 Divisibility DP
POJ:http://poj.org/problem?id=1745 A完这题去买福鼎肉片,和舍友去买滴~舍友感慨"这一天可以卖好几百份,每份就算赚一块钱..那么一个月..一年...&quo ...
- POJ 1745 Divisibility【DP】
题意:给出n,k,n个数,在这n个数之间任意放置+,-号,称得到的等式的值能够整除k则为可划分的,否则为不可划分的. 自己想的是枚举,将所有得到的等式的和算出来,再判断它是否能够整除k,可是有1000 ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- dp题目列表
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- [转] POJ DP问题
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
随机推荐
- 下载安装配置 Scala-2.12.11
文章更新于:2020-03-24 安装惯例,文件附上链接放在文首. 文件名:scala-2.12.11.tgz 文件大小:19.83 MB 下载链接:https://downloads.lightbe ...
- CORS漏洞的学习与分析
同源策略 同源策略(Same origin policy)是一种约定,一种非常重要的安全措施,也是最基本的安全功能,它禁止了来自不同源的脚本对当前页面的读取或修改,从而限制了跨域访问甚至修改资源,防止 ...
- 刮刮乐自定义view
说明:该代码是参考鸿洋大神的刮刮乐自定义view来写的. 实现:刮刮乐-刮奖的效果,如下效果 下面直接放代码了:只有一个自定义view,要实现真正的功能还需要进一步封装 /** * 自定义view-刮 ...
- svg整体缩放至指定大小
一.问题 svg画面跑在分辨率低的电脑上,导致不能完全显示. 二.要求 svg要能够根据电脑的屏幕大小自动缩放至适配电脑的尺寸. 三.实现 1.获取本机窗口高度.宽度 let clientWidth ...
- Extjs——简单的Grid panel小实例
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- stand up meeting 12-8
根据计划今天项目组成员和travis老师毕然同学进行了最后一次关于design和feature的确认meeting. 项目design和UI的改动较大,feature改动较小,需对UI进行重新整合,对 ...
- Eight HDU - 1043 (双向BFS)
记得上人工智能课的时候老师讲过一个A*算法,计算估价函数(f[n]=h[n]+g[n])什么的,感觉不是很好理解,百度上好多都是用逆向BFS写的,我理解的逆向BFS应该是从终点状态出发,然后把每一种状 ...
- Present CodeForces - 1323D (思维+二分)
题目大意比较简单,就是求一堆(二元组)的异或和. 思路:按位考虑,如果说第k位为1的话,那么一定有奇数个(二元组)在该位为1.二元组内的数是相加的,相加是可以进位的.所以第k位是0还是1,至于k为后边 ...
- vue如何添加jquery?
1.首选通过npm安装jquery? 2.在build/webpack.base.conf文件当中引入jquery <pre>module.exports = { ... resolve: ...
- leetcode-0101 对称二叉树
题目地址 https://leetcode-cn.com/problems/symmetric-tree/ 1.递归 本题最简单的思路是递归,可以假设两棵一模一样的树在进行镜像对比.他们之间的关系满足 ...