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. Day6 - F - KiKi's K-Number HDU - 2852

    For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. No ...

  2. numpy中的CSV文件

    As we all know,we use numpy to do some data explore.CSV has a good point to get a lot data. so how c ...

  3. hook框架frida的安装以及简单实用案例

    1.下载地址 https://github.co/frida/frida/releases 2.另外两种安装方法 1.Install from prebuilt binaries This is th ...

  4. C# 控件缩写规范

    标准控件缩写规范 类 型 前 缀 示 例 Adrotator adrt adrtTopAd BulletedList blst blstCity Button btn btnSubmit Calend ...

  5. UVA - 1451 Average (斜率优化)

    题意:由01组成的长度为n的子串,AT由0表示,GC由1表示,求一段长度大于等于L且GC率最高的子串的起始终止坐标,若GC率相同,取长度较小,若长度相同,取起始坐标最小. 分析: 1.一个子串(i+1 ...

  6. solidworks快捷键画图

    平移 :ctrl+鼠标中键 旋转:鼠标中键 缩放:移动中键

  7. netty权威指南学习笔记六——编解码技术之MessagePack

    编解码技术主要应用在网络传输中,将对象比如BOJO进行编解码以利于网络中进行传输.平常我们也会将编解码说成是序列化/反序列化 定义:当进行远程跨进程服务调用时,需要把被传输的java对象编码为字节数组 ...

  8. 一百零二、SAP中ALV事件之十五,让ALV表格自动求和

    一.代码如下 二.运行之后,效果如图,表头多了一个求和符号E,最下面一行会列出求和的相关信息 完美

  9. CSU 1126 DFS前缀和

    在一棵树上找影响最小的某个点,某个点的影响是等于其他点到他的距离*其他点的权值 的和 我一开始也找不到什么好的方法,只能想到每个点暴力去判断,但是这样肯定会超时(10^5个点),又有点想用类似前缀和, ...

  10. (20)sopel算法

    基础知识的理论,主要看这个博客:https://blog.csdn.net/github_38140310/article/details/68959931 然后代码展示: #include &quo ...