Chef and Deque Problem Code: CHEFDQE Codechef Solutions
Chef and Deque Problem Code: CHEFDQESubmit
Read problem statements in Mandarin, Russian, and Vietnamese as well.
Chef and one of his friends were practicing for ICPC together by giving each other programming problems.
In Chef's turn, Chef gave his friend a modified deque (double-ended queue), initially with elements , which supported performing operations of the following type:
- Choose an integer , which must be a power of (possibly ). Additionally, each particular value of can only be chosen if it has not been chosen in any previous operation.
- Delete elements from the end of the deque.
- Then, you may also decide to delete elements from the beginning of the deque.
- However, the deque is not allowed to become completely empty.
Chef also gave his friend a special integer and asked him to find the minimum number of operations needed such that the sum of the elements remaining in the deque is divisible by , or determine that it is impossible to achieve this goal.
Chef's friend could not solve the problem and asked for your help. Can you solve it for him?
Input Format
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains two space-separated integers and .
- The second line contains space-separated integers .
Output Format
For each test case, print a single line containing one integer --- the minimum number of required operations or if it is impossible to achieve the goal.
Constraints
- for each valid
- the sum of over all test cases does not exceed
Subtasks
Subtask #1 (15 points):
- the sum of over all test cases does not exceed
Subtask #2 (20 points): if it is possible to achieve the goal, at most operations are needed
Subtask #3 (65 points): original constraints
Sample Input 1
4
4 7
8 6 4 6
7 13
10 2 3 8 1 10 4
7 1
7 3 7 2 9 8 10
3 11
3 4 8
Sample Output 1
1
2
0
-1
Explanation
Example case 1: The only possible way is to keep the first two elements and , since , which is divisible by . Hence we must delete elements from the end in operation.
Example case 3: The sum of all elements is already divisible by .
Example case 4: It is impossible to achieve the goal.
Comments
Post a Comment