#include<iostream>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<cstdlib>
#include<stdio.h>
using namespace std;
const int maxn = 10017;
const int maxk = 117;
int n, k;
int d[maxn];
bool dp[maxn][maxk];
const char str[2][30] = {"Not divisible", "Divisible"};
int DP(){
while(d[0] < 0){
d[0] += k;
}
dp[0][d[0]%k] = true;
for(int i = 1; i < n; ++i){
for(int j = 0; j < k; ++j){
if(dp[i-1][j]){
int tmp = (j + d[i]) % k;
while(tmp < 0){
tmp += k;
}
tmp %= k;
dp[i][tmp] = true;
tmp = (j - d[i]) % k;
while(tmp < 0){
tmp += k;
}
tmp %= k;
dp[i][tmp] = true;
}
}
}
return 0;
}
int main(){
while(scanf("%d%d",&n,&k)!=EOF){
for(int i = 0; i < n; ++i){
scanf("%d", d + i);
while(d[i] < 0){
d[i] += k;
}
}
memset(dp, 0, sizeof(dp));
DP();
if(dp[n-1][0]){
printf("Divisible\n");
}
else{
printf("Not divisible\n");
}
}
return 0;
}
这道题开始wa了几次,看网上的参考代码才发现自己的问题在于:
对于负数取余的处理错了....