POJ 1745:Divisibility 枚举某一状态的DP

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;
}

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

时间: 2024-11-02 09:09:43

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

poj 2411 Mondriaan&#39;s Dream(状态压缩+dp)

 题意:用1*2砖块铺满n*m的房间. 思路转自:http://www.cnblogs.com/scau20110726/archive/2013/03/14/2960448.html 因为这道题输入范围在11*11之间,所以可以先打表直接输出.......... 状态压缩DP 经典覆盖问题,输入n和m表示一个n*m的矩形,用1*2的方块进行覆盖,不能重叠,不能越出矩形边界,问完全覆盖完整个矩形有多少种不同的方案 其中n和m均为奇数的话,矩形面积就是奇数,可知是不可能完全覆盖的.接着我们来看

poj - 1691 - Painting A Board(状态压缩dp)

题意:N(1 <= N <= 15)个矩形,每个矩形要涂上指定的颜色C(1 <= C <= 20),如果给一个矩形涂色,那么与它相邻的上方矩形必须已经涂色,问最少要取几次画笔. 题目链接:http://poj.org/problem?id=1691 -->>状态:dp[S][color] 表示达到状态 S 且最后一次涂色为 color 时的最小取画笔数 状态转移方程:dp[S][color] = min(dp[S][color], dp[sub][i]); 或者 dp[

POJ 1745 Divisibility (线性dp)

Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10598   Accepted: 3787 Description Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmet

poj 1745 Divisibility(DP + 数学)

题目链接:http://poj.org/problem?id=1745 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, f

POJ 1745 Divisibility【DP】

题意:给出n,k,n个数,在这n个数之间任意放置+,-号,称得到的等式的值能够整除k则为可划分的,否则为不可划分的. 自己想的是枚举,将所有得到的等式的和算出来,再判断它是否能够整除k,可是有10000个数-_- 5555---还是看的题解-- 话说这样的状态好奇妙啊啊啊--- 用dp[i][j]表示等式中有i个数的时候余数为j是否成立,成立的话dp[i][j]的值为1,否则为0 然后就是递推的过程, 如果dp[i-1][j]为1,那么dp[i][(j-a[i])%k]=1,dp[i][(j+a

poj 1745 Divisibility 【DP】

Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11044   Accepted: 3949 Description Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmet

poj 1745 Divisibility

Divisibility Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Description Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expr

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

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6115   Accepted: 1720 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been

poj 1745 Divisibility(DP)

最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description 给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大. Input 输入数据的第一行为一个正整数T,表示有T组测试数据.每一组测试数据的第一行为四个正整数m,n,x,y(0<m,n<1000 AND 0<x<=m AND 0<y