Array Stabilization (AND version) solution codeforces- You are given an array a[0…n−1]=[a0,a1,…,an−1]a[0…n−1]=[a0,a1,…,an−1] of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one.

Array Stabilization (AND version)

You are given an array a[0n1]=[a0,a1,,an1]a[0…n−1]=[a0,a1,…,an−1] of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one.

In one step, the array aa is replaced by another array of length nn according to the following rules:

  1. First, a new array ada→d is defined as a cyclic shift of the array aa to the right by dd cells. The elements of this array can be defined as adi=a(i+nd)modnai→d=a(i+n−d)modn, where (i+nd)modn(i+n−d)modn is the remainder of integer division of i+ndi+n−d by nn.It means that the whole array ada→d can be represented as a sequence

    Array Stabilization (AND version) solution codeforces

  2. Then each element of the array aiai is replaced by ai&adiai&ai→d, where && is a logical “AND” operator.

For example, if a=[0,0,1,1]a=[0,0,1,1] and d=1d=1, then ad=[1,0,0,1]a→d=[1,0,0,1] and the value of aa after the first step will be [0&1,0&0,1&0,1&1][0&1,0&0,1&0,1&1], that is [0,0,0,1][0,0,0,1].

The process ends when the array stops changing. For a given array aa, determine whether it will consist of only zeros at the end of the process. If yes, also find the number of steps the process will take before it finishes.

Array Stabilization (AND version) solution codeforces

The first line contains an integer tt (1t10001≤t≤1000) — the number of test cases.

The next 2t2t lines contain descriptions of the test cases.

The first line of each test case description contains two integers: nn (1n1061≤n≤106) — array size and dd (1dn1≤d≤n) — cyclic shift offset. The second line of the description contains nn space-separated integers aiai (0ai10≤ai≤1) — elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 106106.

Array Stabilization (AND version) solution codeforces

Print tt lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the number of steps after which the array will contain only zeros for the first time. If there are still elements equal to 11 in the array after the end of the process, print -1.

Array Stabilization (AND version) solution codeforces


2 1
0 1
3 2
0 1 0
5 2
1 1 0 1 0
4 2
0 1 0 1
1 1



Array Stabilization (AND version) solution codeforces

In the third sample test case the array will change as follows:

  1. At the beginning a=[1,1,0,1,0]a=[1,1,0,1,0], and a2=[1,0,1,1,0]a→2=[1,0,1,1,0]. Their element-by-element “AND” is equal to
  2. Now a=[1,0,0,1,0]a=[1,0,0,1,0], then a2=[1,0,1,0,0]a→2=[1,0,1,0,0]. Their element-by-element “AND” equals to
  3. And finally, when a=[1,0,0,0,0]a=[1,0,0,0,0] we get a2=[0,0,1,0,0]a→2=[0,0,1,0,0]. Their element-by-element “AND” equals to

Thus, the answer is 33 steps.In the fourth sample test case, the array will not change as it shifts by 22 to the right, so each element will be calculated as 0&00&0 or 1&11&1 thus not changing its value. So the answer is -1, the array will never contain only zeros.

Leave a Comment

Your email address will not be published.