POJ1745动态规划
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 11622 | Accepted: 4178 |
Description
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 second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it's absolute value.
Output
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动态规划的更多相关文章
- 增强学习(三)----- MDP的动态规划解法
上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- C#动态规划查找两个字符串最大子串
//动态规划查找两个字符串最大子串 public static string lcs(string word1, string word2) { ...
- C#递归、动态规划计算斐波那契数列
//递归 public static long recurFib(int num) { if (num < 2) ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
随机推荐
- MySQL mha 高可用集群搭建
[mha] MHA作为MySQL故障切换和主从提升的高可用软件,在故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一 ...
- tornado源码分析系列一
先来看一个简单的示例: #!/usr/bin/env python #coding:utf8 import socket def run(): sock = socket.socket(socket. ...
- L156
China has specified the definition and diagnosis standard for internet addiction in its latest adole ...
- Jenkins分享
2016-02-26 小马哥 程序员之路 PPT下载地址:http://pan.baidu.com/s/1i4pw6oP Jenkins 是一个开源软件项目,旨在提供一个开放易用的软件平台,使 ...
- ubuntu16 sogou install
1,下载搜狗deb文件(ubuntu16不要参考搜狗旧的安装文档): http://pinyin.sogou.com/linux/ 2,双击sogoupinyin_2.1.0.0086_amd64.d ...
- css 添加伪元素 消除浮动 对父元素高度产生的影响
- Java从入门到精通全套教程免费分享
这是我自己早前听课时整理的Java全套知识,适用于初学者,也可以适用于中级进阶的人,你们可以下载,我认为是比较系统全面的,可以抵得上市场上90%的学习资料.讨厌那些随便乱写的资料还有拿出来卖钱的人!在 ...
- HDU1576 A/B
暴力出奇迹,我就知道没取余那么正当,肯定有什么奇淫怪巧,果然5分钟A掉. #include<cstdio> #include<cstdlib> #include<iost ...
- HTTP请求报头及其处理
ps:详细说明http://www.cnblogs.com/kkgreen/archive/2011/04/11/2012829.html
- BZOJ1690 Usaco2007 Dec 奶牛的旅行 【01分数规划】
BZOJ1690 Usaco2007 Dec 奶牛的旅行 题目描述 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得 ...