POJ 1745 Divisibility (线性dp)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 10598 | Accepted: 3787 |
Description
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 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
Source
field=source&key=Northeastern+Europe+1999">Northeastern Europe 1999
题目链接:
id=1745">http://poj.org/problem?id=1745
题目大意:给n个数,让他们通过加减运算。推断结果可不可能被k整除
题目分析:用dp[i][j]表示通过前i个数的运算得到的余数为j可不可能。先看求a % k。假设a > k,则a = n * k + b。(n * k + b) % k == 0 + b % k = a % k,所以当a > k时,对求余数有影响的部分是不能被整除的部分。因此对于每一个数我们能够做a[i] = a[i] > 0 ? (a[i] % k) : -(a[i] % k)的预处理,然后就是在dp[i - 1][j]的情况下。推出下一状态。下一状态有两种可能,加和减,减的时候防止出现负数加上个k再取余,初始化dp[0][a[0]]
= true最后仅仅要推断dp[n - 1][0]及前n个数通过加减运算是否能得到被k整除的值
#include <cstdio>
#include <cstring>
int const MAX = 10005; bool dp[MAX][105];
int a[MAX]; int main()
{
int n, k;
while(scanf("%d %d", &n, &k) != EOF)
{
memset(dp, false, sizeof(dp));
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
a[i] = a[i] > 0 ? (a[i] % k) : -(a[i] % k);
}
dp[0][a[0]] = true;
for(int i = 1; i < n; i++)
{
for(int j = 0; j <= k; j++)
{
if(dp[i - 1][j])
{
dp[i][(j + a[i]) % k] = true;
dp[i][(k + j - a[i]) % k] = true;
}
}
}
printf("%s\n", dp[n - 1][0] ? "Divisible" : "Not divisible");
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
POJ 1745 Divisibility (线性dp)的更多相关文章
- POJ 1745 Divisibility【DP】
题意:给出n,k,n个数,在这n个数之间任意放置+,-号,称得到的等式的值能够整除k则为可划分的,否则为不可划分的. 自己想的是枚举,将所有得到的等式的和算出来,再判断它是否能够整除k,可是有1000 ...
- POJ 2479-Maximum sum(线性dp)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33918 Accepted: 10504 Des ...
- POJ 1745 Divisibility DP
POJ:http://poj.org/problem?id=1745 A完这题去买福鼎肉片,和舍友去买滴~舍友感慨"这一天可以卖好几百份,每份就算赚一块钱..那么一个月..一年...&quo ...
- poj 3356 AGTC(线性dp)
题目链接:http://poj.org/problem?id=3356 思路分析:题目为经典的编辑距离问题,其实质为动态规划问题: 编辑距离问题定义:给定一个字符串source,可以对其进行复制,替换 ...
- POJ 1745 Divisibility
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9476 Accepted: 3300 Desc ...
- POJ 1745 线性和差取余判断
POJ 1745 线性和差取余判断 题目大意:每个数都必须取到,相加或相减去,问所有的方案最后的得数中有没有一个方案可以整除k 这个题目的难点在于dp数组的安排上面 其实也就是手动模仿了一下 比如 一 ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
随机推荐
- .net设计模式 - 单例模式
DoNet设计模式实例之单例模式( Singleton Pattern) 一 : 单例模式的简介:(Brief Introduction) 单例模式(Singleton Pattern),保证一个类只 ...
- Python-Tkinter的Entry详解
#Tkinter教程之Entry篇 #Entry用来输入单行文本 '''1.第一个Entry程序''' from Tkinter import * root = Tk() Entry(root,tex ...
- leetcode - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Android Fragment使用
通常地 fragment做为宿主activity UI的一部分, 被作为activity整个view hierarchy的一部分被嵌入. 有2种方法你能够加入一个fr ...
- java中由类名和方法名字符串实现其调用【反射机制】
js里通过eval()函数,在知道某个方法名是可以实现调用该方法,那么在java里边又怎么实现的呢? java里边是通过反射机制来实现,代码如下: import java.lang.reflect.M ...
- MySQL外键约束OnDelete和OnUpdate的使用
On Delete和On Update都有Restrict,No Action, Cascade,Set Null属性.现在分别对他们的属性含义做个解释. ON DELETE restrict(约束) ...
- ubuntu 12.04英文版设置成中文版
适用于ubuntu 12.04英文版的系统,其他版本号的设置应该是大同小异的. 进入ubuntu系统,在顶部齿状标志找到system... 2.在personal找到Language Support ...
- Android它Service
服务是一段代码的后台执行. 无法处理,也不是螺纹,但它是在进程和线程的执行. Android该服务与Activity不同,不能与用户交互,无法启动自己. 媒体播放服务.当用户退出媒体选择用户界面,不过 ...
- Visual Studio跨平台开发实战(1) - Hello Xamarin!
原文 Visual Studio跨平台开发实战(1) - Hello Xamarin! 前言 应用程式发展的脚步, 从来没有停过. 从早期的Windows 应用程式, 到网路时代的web 应用程式, ...
- ecshop首页调用某分类下的商品|assign_cat_goods()
ecshop首页调用分类下的商品其实很简单,也有模板设置那里可以设置,不过那个只可以用cat_goods.lib,不方便,所以我想看看怎么能简单的实现ecshop首页调用分类下的商品 只需要在inde ...