Description
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 = 16Input
The first line of the input file contains two integers, N and K (1 <= N <= 10000, 2 <= K <= 100) separated by a space.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
Source
Northeastern Europe 1999#include#include int const MAX = 10005; bool dp[MAX][105]; int a[MAX]; int main() { int n, k; while(scanf("%d %d", &n, &k) != EOF) { memset(dp, false, sizeof(dp)); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); a[i] = a[i] > 0 ? (a[i] % k) : -(a[i] % k); } dp[0][a[0]] = true; for(int i = 1; i < n; i++) { for(int j = 0; j <= k; j++) { if(dp[i - 1][j]) { dp[i][(j + a[i]) % k] = true; dp[i][(k + j - a[i]) % k] = true; } } } printf("%s\n", dp[n - 1][0] ? "Divisible" : "Not divisible"); } }