Official algorithmic problem description and constraints.
A permutation of length π
is an array of π
integers, where each number from 1
to π
appears exactly once. An inversion in a permutation π
is a pair of indices (π,π)
such that π<π
and π
π
>π
π
.
For a permutation π
, we define its inversion value as the number of its subsegments that contain at least one inversion. Formally, this is the number of pairs of integers (π,π)
(1β€π<πβ€π
) for which there exists a pair of indices (π,π)
satisfying the following conditions: πβ€π<πβ€π
and π
π
>π
π
.
For example, for the permutation [3,1,4,2]
, the inversion value is 5
.
You are given two integers π
and π
. Your task is to construct a permutation of length π
with an inversion value equal to exactly π
.
Input
The first line contains one integer π‘
(1β€π‘β€500
) β the number of test cases.
Each test case consists of a single line containing two integers π
and π
(2β€πβ€30
; 0β€πβ€π(πβ1)
2
).
Output
For each test case, output the answer as follows:
Example
Input
Copy
5 4 5 5 10 5 0 6 8 3 1
Output
Copy
3 1 4 2 5 4 3 2 1 1 2 3 4 5 2 3 5 6 1 4 0
Detailed video explanations, mathematical intuition, and clean C++ implementation code.