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. 基于线程池、消息队列和epoll模型实现并发服务器架构

    引言 并发是什么?企业在进行产品开发过程中为什么需要考虑这个问题?想象一下天猫的双11和京东的618活动,一秒的点击量就有几十万甚至上百万,这么多请求一下子涌入到服务器,服务器需要对这么多的请求逐个进 ...

  2. [笔记]ul>li>a做分布时, 让其居中显示效果

    结构: <div id="page"> <ul> <li><a href="#">首页</a>< ...

  3. MS SQLSERVER 自增ID列竟然会重复

    MS SQLSERVER 2008 R2 datacenter edition 自增的ID列,设为了主键. 从没遇到过的情况.

  4. 从MSSQL表中删除重复项

    declare @ids int=1 declare @count int while @ids<471 begin select @count=COUNT(*) From LotNO wher ...

  5. MYSQL登录及常用命令

    1.mysql服务的启动和停止 mysql> net stop mysql mysql> net start mysql 2.登陆mysql mysql> 键入命令mysql -ur ...

  6. 51nod 1515:明辨是非 并查集合并

    1515 明辨是非 题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给n组操作,每组操作形式为x y p. 当p为1时,如果第x ...

  7. POJ 2309:BST lowbit

    BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9140   Accepted: 5580 Description C ...

  8. C++ 非白名单程序间接启动

    主要的思路是不能用不受信任的程序直接参与创建进程,而是间接启动目标进程.比如你可以把目标程序创建快捷方式,然后设置快捷键.然后向桌面发快捷键的按键消息,目标程序就会被桌面程序启动.

  9. pixi的图片处理

    pixi的图片处理   var texture = PIXI.Texture.fromImage('sprite.png');var sprite = new PIXI.Sprite(texture) ...

  10. 洛谷 三月月赛 B

    搞出每一位与前一位的差,然后区间修改只是会影响区间的端点,所以只修改一下端点的值就好. %%%高一神犇线段树 #include<bits/stdc++.h> #define N 10000 ...