Divisibility
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11001   Accepted: 3933

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

题意就是给了N个数,在N-1个位置变换+ -号,问得到的结果中有没有能够整除K的,如果有,输出Divisible。没有,输出Not Divisible。

DP真是一片很深的海。

越做DP越觉得DP的花样很多,这个是我做了POJ1837觉得DP是可以做这道题的。觉得DFS也应该可以,没试。。。

POJ1837和这道题都是固定枚举其中的某个状态或者变量,这里的可以枚举的状态就是余数,给了K,所以我只需对0到K-1这些余数做枚举,然后从i的余数状态推i+1的余数状态。

就是这样:

dp[i][(j+value[i])%mod] +=dp[i-1][j];

dp[i][(j-value[i]+mod)%mod] +=dp[i-1][j];

然后这样做可能是因为数目比较大了溢出还是怎样WA了一次,于是我控制了一下数值。这样:

dp[i][(j+value[i])%mod] +=dp[i-1][j];

dp[i][(j-value[i]+mod)%mod] +=dp[i-1][j];





if(dp[i][(j+value[i])%mod]>10)

dp[i][(j+value[i])%mod]=10;

if(dp[i][(j-value[i]+mod)%mod]>10)

dp[i][(j+value[i])%mod]=10;

。。。很幼稚的方法,但还是涨姿势长见识了。。。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
using namespace std; int num,mod;
int dp[10005][102];
int value[10005]; int main()
{
int temp,i,j;
cin>>num>>mod; cin>>value[1];
value[1]=abs(value[1])%mod;
for(i=2;i<=num;i++)
{
cin>>temp;
value[i]=abs(temp);
value[i]=value[i]%mod;
}
memset(dp,0,sizeof(dp));
dp[1][value[1]]=1; for(i=2;i<=num;i++)
{
for(j=0;j<mod;j++)
{
dp[i][(j+value[i])%mod] +=dp[i-1][j];
dp[i][(j-value[i]+mod)%mod] +=dp[i-1][j]; if(dp[i][(j+value[i])%mod]>10)
dp[i][(j+value[i])%mod]=10;
if(dp[i][(j-value[i]+mod)%mod]>10)
dp[i][(j+value[i])%mod]=10;
}
}
if(dp[num][0])
{
cout<<"Divisible"<<endl;
}
else
{
cout<<"Not divisible"<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1745:Divisibility 枚举某一状态的DP的更多相关文章

  1. POJ 2836:Rectangular Covering(状态压缩DP)

    题目大意:在一个平面内有若干个点,要求用一些矩形覆盖它们,一个矩形至少覆盖两个点,可以相互重叠,求矩形最小总面积. 分析: 数据很小,很容易想到状压DP,我们把点是否被覆盖用0,1表示然后放在一起得到 ...

  2. POJ 2411 Mondriaan's Dream [经典状态压缩dp]

    题意:略. 思路:这一题开始做的时候完全没有思路,便去看了别人的题解. 首先,对于这个题目解法想有一个初步的了解,请看这里:http://www.2cto.com/kf/201208/146894.h ...

  3. POJ 1745 Divisibility (线性dp)

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10598   Accepted: 3787 Des ...

  4. POJ 1745 Divisibility【DP】

    题意:给出n,k,n个数,在这n个数之间任意放置+,-号,称得到的等式的值能够整除k则为可划分的,否则为不可划分的. 自己想的是枚举,将所有得到的等式的和算出来,再判断它是否能够整除k,可是有1000 ...

  5. POJ 1745 Divisibility DP

    POJ:http://poj.org/problem?id=1745 A完这题去买福鼎肉片,和舍友去买滴~舍友感慨"这一天可以卖好几百份,每份就算赚一块钱..那么一个月..一年...&quo ...

  6. poj 1873(枚举所有的状态+凸包)

    The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6115   Accepted: 1 ...

  7. POJ 1745 Divisibility

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9476   Accepted: 3300 Desc ...

  8. poj 2441 Arrange the Bulls(状态压缩dp)

    Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to ...

  9. poj 2411 Mondriaan's Dream(状态压缩dp)

    Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...

随机推荐

  1. mysql提示 Lock wait timeout exceeded解决办法 事务锁死

    查询  select concat('KILL ',id,';') from information_schema.processlist; 复制结果 新建sql脚本粘贴并执行

  2. R box-cox变换 《回归分析与线性统计模型》page100

    > rm(list = ls()) > library(openxlsx) > electric= read.xlsx("data101.xlsx",sheet ...

  3. 033、Java中使用简化运算符

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. php 文件缓存 include vs serialize vs json_encode

    大神:http://techblog.procurios.nl/k/news/view/34972/14863/cache-a-large-array-json-serialize-or-var_ex ...

  5. 3 JVM配置参数

  6. NO1 ip-systemctl-fdisk

    一.IP相关·man·man:show manual info   查看一个命令的帮助信息:man ip·ip命令: show device显示设备,device address显示地址,route ...

  7. .NET配置问题

    Ext.NET MVC 配置问题总结       随着VS版本和.NET MVC版本.EF的版本的不断更新,虽然很多功能随着版本的提升而更完善,但对于旧版本开发的软件就有点悲催了,或许很多开发者都遇到 ...

  8. LINUX——LVM逻辑卷管理

    LVM: logical volumes manager LVM逻辑卷部署 物理卷—>卷组—>逻辑卷 第一步:关机添加磁盘:两个磁盘可以构成一个磁盘组. 第二步:查看磁盘 # ls /de ...

  9. Elasticsearch开启试用x-pack license

    一.Elasticsearch 6.7.2开启trial  x-pack license:x-pack的license试用期只有30天 1.ES6.7.2版本默认已经安装了x-pack插件,这里就没有 ...

  10. 杂记 -- 关于ref、kepp-alive、nextTick、fetch

    1.ref:定义一个普通的dom节点或一个vue的组件实例对象 定义方法: <div class="page1"> <button @click="li ...