Square-Free Division (hard version)
Contest: Codeforces Round 708 (Div. 2)
Difficulty rating: 2500
Tags: data structures, dp, greedy, math, number theory, two pointers

This is the hard version of the problem. The only difference is that in this version $$$0 \leq k \leq 20$$$.

There is an array $$$a_1, a_2, \ldots, a_n$$$ of $$$n$$$ positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.

Moreover, it is allowed to do at most $$$k$$$ such operations before the division: choose a number in the array and change its value to any positive integer.

What is the minimum number of continuous segments you should use if you will make changes optimally?

Input format:
The first line contains a single integer $$$t$$$ $$$(1 \le t \le 1000)$$$  — the number of test cases.

The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \leq k \leq 20$$$).

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^7$$$).

It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case print a single integer  — the answer to the problem.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case it is possible to change the array this way: $$$[\underline{3}, 6, 2, 4, \underline{5}]$$$ (changed elements are underlined). After that the array does not need to be divided, so the answer is $$$1$$$.

In the second test case it is possible to change the array this way: $$$[6, 2, \underline{3}, 8, 9, \underline{5}, 3, 6, \underline{10}, \underline{11}, 7]$$$. After that such division is optimal:

- $$$[6, 2, 3]$$$
- $$$[8, 9, 5, 3, 6, 10, 11, 7]$$$

Example input:
3
5 2
18 6 2 4 1
11 4
6 2 2 8 9 1 3 6 3 9 7
1 0
1

Example output:
1
2
1

Editorial:
Let's use the same definitions as in tutorial of E1. So after making $$$a_i = mask(a_i)$$$ for all $$$1 \le i \le n$$$ we need to split the whole array into minimal amount of contiguous subsegments with all different elements. Also, we can change $$$k$$$ elements how we want.
Firstly, for each $$$1 \le i \le n$$$ and $$$0 \le j \le k$$$ let's find $$$left_{i,j}$$$  — such minimal index $$$l$$$, so that after making $$$j$$$ changes the segment $$$a_l, a_{l + 1}, \ldots, a_i$$$ contains only distinct values. For fixed $$$j$$$ if $$$i$$$ is increased then $$$left_{i,j}$$$ increases, too, that's why for fixed $$$j$$$ we can use the two pointers technique. This allows us to calculate $$$left_{i,j}$$$ in $$$O(n \cdot k)$$$.
Now for each $$$1 \le i \le n$$$ and $$$0 \le j \le k$$$ let's calculate $$$dp_{i,j}$$$  — the minimal amount of contiguous subsegments that prefix $$$a_1, a_2, \ldots, a_i$$$ is possible to divide into after making $$$j$$$ changes. For each $$$j$$$ let's consider $$$0 \le x \le j$$$  — the amount of changes on the last subsegment. Let's say that $$$l = left_{i,x}$$$ then $$$dp_{i,j} = min(dp_{i,j}, dp_{l - 1,j-x} + 1)$$$. This is done in $$$O(n \cdot k^2)$$$ so the total complexity of the solution after making $$$a_i = mask(a_i)$$$ is $$$O(n \cdot k^2)$$$.