poj divisibility

 
// 背包问题
 
#include 

#include 

int dp[10001][101];

int n, m;

int a[10001];

int main()

{

int i, j, k, tmp;

while (scanf ("%d%d", &n, &m) != EOF)

{

for (i = 1; i <= n; i++)

{

scanf ("%d", &a[i]);

while (a[i] < 0)

a[i] += m;

a[i] = a[i] % m;

}

memset (dp, 0, sizeof (dp));

dp[0][0] = 1;

for (i = 1; i <= n; i++)

{

for (j = 0; j <= m; j++)

{

if (dp[i - 1][j]) 

{

dp[i][(j + a[i]) % m] = 1; 

tmp = j - a[i];

while (tmp < 0) tmp += m;

dp[i][tmp] = 1; 

}

}

}

if(dp[n][0]) printf("Divisible\n");

else printf("Not divisible\n"); 

}

return 0;

}

 


你可能感兴趣的:(poj)