题目链接:http://poj.org/problem?id=1745
类似的题目之前写过一个差不多的(链接:http://www.cnblogs.com/a-clown/p/5982611.html)//线性dp?
思路是一样的,不细说了。。
//还有一个不懂就是滚动数组,用%2优化的,现在不懂,等我搞懂了再来更新。
AC代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; const int maxn=1e4+5; int a[maxn]; int dp[maxn][105]; int main() { int N,K; while(scanf("%d%d",&N,&K)==2) { for(int i=1; i<=N; i++) scanf("%d",&a[i]); memset(dp,0,sizeof(dp)); dp[0][0]=1; dp[1][abs(a[1])%K]=1; for(int i=2; i<=N; i++) { for(int j=0; j<K; j++) { if(dp[i-1][j]) { dp[i][abs(j+a[i])%K]=1; dp[i][abs(j-a[i])%K]=1; } } } if(dp[N][0]) puts("Divisible"); else puts("Not divisible"); } return 0; }