Test instructions: Enter n ordered numbers, preceded by a number of + or-, to see if there is such a and, so that and can be divisible by the number k
There are only positive or negative two cases in front of each digit, so it fits the 0,1 backpack, and the weight of the backpack is the remainder of the K (all positive)
172 KB297 msc++785 b#include<cstdio> #include <cstring> #include <algorithm> #include <iostream >using namespace Std;int num[10010];bool dp[2][105];int n,mod;int Main () { while (~scanf ("%d%d", &n,&mod )) { memset (dp,0,sizeof (DP)); Dp[0][0]=true; for (int i=1;i<=n;i++) scanf ("%d", &num[i]); for (int i=1;i<=n;i++) { memset (dp[i&1],0,sizeof (dp[i&1])); for (int j=0;j<mod;j++) { int t1= (j+num[i]+10000*mod)%mod; int t2= (j-num[i]+10000*mod)%mod; Dp[i&1][t1]=max (dp[i&1][t1],dp[(i-1) &1][j]); Dp[i&1][t2]=max (dp[i&1][t2],dp[(i-1) &1][j]); } } if (Dp[n&1][0]) printf ("divisible\n"); else printf ("Not divisible\n"); } return 0;}
POJ 1745 divisibility (0,1 backpack) (good title)