#C11010. Distinct Subarray Products Modifications
Distinct Subarray Products Modifications
Distinct Subarray Products Modifications
You are given an integer array A
of length N
and an integer K
. Consider every contiguous subarray of length K
in A
. For each subarray, compute its product. The task is to determine the minimum number of modifications needed so that the products of all K
-length subarrays become distinct.
A modification is defined as altering an element in the array. The simplest way to ensure distinct products is to count, for every product that appears multiple times, how many extra occurrences it has; i.e. if a product appears f times, you need to modify f-1 of them.
Note: It is guaranteed that the products can be computed without overflow for the given constraints.
inputFormat
The input begins with an integer T
denoting the number of test cases. Each test case consists of two lines:
- The first line contains two space-separated integers
N
andK
. - The second line contains
N
space-separated integers representing the arrayA
.
outputFormat
For each test case, output a single integer representing the minimum number of modifications required so that every contiguous subarray of length K
has a unique product. Each result should be printed on a separate line.
4
4 2
1 2 3 4
3 1
7 7 7
6 3
1 2 3 4 5 6
4 4
10 2 3 5
0
2
0
0
</p>