Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Educational Codeforces Round 183/D. Inversion Value of a Permutation
Prev
CodeforcesCodeforces
Expert
Educational Codeforces Round 183

D. Inversion Value of a Permutation

Dynamic ProgrammingArrayCombinatoricsMathProbability and Statistics
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

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:

  • if the desired permutation does not exist, output a single integer 0
  • ;
  • otherwise, output π‘›
  •  distinct integers from 1
  •  to π‘›
  •  β€” the desired permutation. If there are multiple such permutations, you may output any of them.

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


Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Educational Codeforces Round 183
Previous Problem