POJ 1745 【0/1 背包】

本文介绍了一道名为Divisibility的编程题的解题思路与实现过程,该题出自POJ在线评测系统。文章详细解释了如何通过0/1背包问题的方法来判断一组整数经过加减运算后能否被特定整数K整除。

题目链接:http://poj.org/problem?id=1745

Divisibility
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13431 Accepted: 4774

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

Source

 

题目大意:给N个数字和一个K,把N个数字加加减减后得到的数字是否能整除K。

大概思路:

一、暴力,全排列,爆炸,out!

二、0/1背包

状态:bool dp[ i ][ x ], 长度为 i 的序列的加减结果模 K 的后的 X 为真或为假

状态转移: dp[ i+1 ][ (((x-num [i+1])%K)+K)%K ]  =  dp[ i ][ x ]       减第 i+1 个数

      dp[ i+1 ][ (((x+num [i+1])%K)+K)%K ]  =  dp[ i ][ x ]      加第 i+1 个数

 

AC code (1224k 360ms):

 1 ///POJ 1745 【0/1背包】
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #define INF 0x3f3f3f3f
 7 using namespace std;
 8 
 9 const int MAXN = 1e4+10;
10 const int MAXK = 111;
11 
12 int num[MAXN];
13 bool dp[MAXN][MAXK];
14 int N, K;
15 
16 void slv()
17 {
18     memset(dp, 0, sizeof(dp));
19     dp[1][((num[1]%K)+K)%K] = true;
20     for(int i = 1; i < N; i++)
21     {
22         for(int p = 0; p < K; p++)
23         {
24             if(dp[i][p])
25             {
26                 dp[i+1][(((p+num[i+1])%K)+K)%K] = true;
27                 dp[i+1][(((p-num[i+1])%K)+K)%K] = true;
28             }
29         }
30     }
31     if(dp[N][0]) printf("Divisible\n");
32     else printf("Not divisible\n");
33 }
34 
35 int main()
36 {
37     scanf("%d%d", &N, &K);
38     for(int i = 1; i <= N; i++)
39     {
40         scanf("%d", &num[i]);
41     }
42     slv();
43     return 0;
44 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值