Indivisible Inversions
시간 제한2초메모리 제한256 MB
순열이 주어질 때, 역전 수가 K로 나누어떨어지지 않는 가장 긴 연속 부분 배열의 길이를 구하거나 그런 배열이 없으면 -1을 출력한다.
문제
While studying for his algorithms midterm, Busy Beaver came up with the following problem and wants your help to solve it.
You are given an integer and a permutation of length .1 Find the length of the longest contiguous subarray () () of such that the number of inversions2 of is not divisible by , or determine if such a subarray does not exist.
Help Busy Beaver find the answer to this problem!
1A permutation of length is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array).
2An inversion in a permutation p is a pair of indices such that and . For example, a permutation contains 4 inversions: , , , .
입력
Each test contains multiple test cases. The first line of input contains a single positive integer , the number of test cases . The description of each test case follows.
The first line of each test case contains two integers and ().
The second line of each test case contains distinct positive integers ().
It is guaranteed that the sum of across all test cases does not exceed .
출력
For each test case, output one line with a single integer, indicating the length of the longest contiguous subarray of such that the number of inversions of the subarray is not divisible by . If such a subarray does not exist, output .
힌트
In the first test case, the number of inversions of is (the second and third elements form the only inversion pair). Since is not divisible by , the longest contiguous subarray whose number of inversions isn't divisible by is the entire array itself. Thus, the answer to the test case is the length of the entire array, which is .
In the second test case, the number of inversions of each contiguous subarray is because the array is sorted. Since no contiguous subarray has an inversion count not divisible by , the answer is .
In the third test case, it can be shown that the longest contiguous subarray whose number of inversions is not divisible by is , which has inversions.
In the fourth test case, it can be shown that the longest contiguous subarray whose number of inversions is not divisible by is , which has inversions.