Divisibility
InterviewTime limit1sMemory limit128 MB
Given a sequence and a modulus K, decide whether some choice of + or - before each later element makes the total divisible by K.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Math, Number theory, Implementation
- Solved
- No attempts yet
Problem
Consider a sequence of integers. By placing a + or - operator between each pair of adjacent integers, you can form many different arithmetic expressions with different values. The first integer's sign is always fixed as +. For example, the sequence 17, 5, -21, 15 yields eight possible expressions:
- 17 + 5 + -21 + 15 = 16
- 17 + 5 + -21 - 15 = -14
- 17 + 5 - -21 + 15 = 58
- 17 + 5 - -21 - 15 = 28
- 17 - 5 + -21 + 15 = 6
- 17 - 5 + -21 - 15 = -24
- 17 - 5 - -21 + 15 = 48
- 17 - 5 - -21 - 15 = 18
We say the sequence is divisible by if + and - operators can be placed so that the resulting value is divisible by . In the example above, the sequence is divisible by 7 (17 + 5 + -21 - 15 = -14) but not by 5.
Write a program that determines whether a sequence of integers is divisible by .
Input
The first line contains two integers and (, ) separated by a space.
The second line contains a sequence of integers separated by spaces. Each integer has absolute value at most 10000.
Output
Print "Divisible" if the given sequence is divisible by , or "Not divisible" if it is not.