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
按照题目给的规则,对输入数列里的每个数选择加或减(负负得正,正负得负)操作,问能不能让最后的值被k整除(ans%k=0)。第一眼看上去这题可以用深搜做,思路也很简单,一分钟就敲完了:
public static void dfs(int res, int start) {
if (start >= n) {
divisible |= res % k == 0;
return;
}
for (int i = start; i < n; i++) {
dfs(res + input[start + 1], start + 1);
dfs(res - input[start + 1], start + 1);
}
}
事实上自己测的时候就发现了……写了个48个数的测试数据,直接炸了,出不来答案。poj这题评论区里有位大佬的评论是:n = 10000时,给你全世界的计算机都算不出答案。虽然没这么夸张,但是这种堪比死循环运算量,普通深搜?还是算了吧…..
所以…..上吧记忆化搜索!Dynamic Programing!利用余数的性质进行递推,利用二维dp数组的j来记录余数,比如说第三个数%k得2,我们就把dp[2][2]给标上1。然后通过循环来取数。
这时候就要考虑到负数的情况,输入是有负数的,要是负数直接这样丢进去的话肯定会报错的(数组超限),所以再次利用余数的性质,处理每一个数:
public static int posMod(int x) {
return Math.abs(x % k);
}
这样就每个数都变成正数了,由于最后值是什么我们不关心,只要知道能不能%k=0就行了,所以破坏了原值也没问题…..
得到递推式:
public static void dp() {
dp[0][posMod(input[0])] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < k; j++) {
if (dp[i - 1][j] == 0) {
continue;
}
dp[i][posMod(j + input[i])] = 1;
dp[i][posMod(j - input[i])] = 1;
}
}
}
最后看dp[n-1][0]位是不是1就好啦,1代表这个数%k等于0嘛。于是这样就可以交了…..ac代码:
public class Main {
static int k;
static int n;
static int[] input = new int[10005];
static int[][] dp = new int[10005][105];// 因为题目说了输入数字最大100,所以j位开100就好了
// static boolean divisible = false;
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
n = reader.nextInt();
k = reader.nextInt();
for (int i = 0; i < n; i++) {
input[i] = reader.nextInt();
}
// dfs(input[0], 0);
// System.out.println(divisible ? "Divisible" : "Not divisible");
dp();
System.out.println(dp[n - 1][0] == 0 ? "Not divisible" : "Divisible");
}
// 大家好,我系TLE的深搜QWQ
/*public static void dfs(int res, int start) {
if (start >= n) {
divisible |= res % k == 0;
return;
}
for (int i = start; i < n; i++) {
dfs(res + input[start + 1], start + 1);
dfs(res - input[start + 1], start + 1);
}
}*/
public static void dp() {
dp[0][posMod(input[0])] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < k; j++) {
if (dp[i - 1][j] == 0) {
continue;
}
dp[i][posMod(j + input[i])] = 1;
dp[i][posMod(j - input[i])] = 1;
}
}
}
public static int posMod(int x) {
return Math.abs(x % k);
}
}