Cubes
Time limit1sMemory limit1024 MB
Counts the distinct weight sequences reachable from a row when adjacent cubes whose total weight is at most K can swap, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Combinatorics, Sorting, Hash map
- Solved
- No attempts yet
Problem
Octavia has cubes of identical size among her toys. The cubes may differ in weight, and two cubes may also share the same weight. Octavia likes to set the cubes in a long row. After many hours of play, she started to wonder how many different rows of cubes she can make.
A row of cubes is given as integers, where each integer is the weight of the corresponding cube in the initial row. Octavia can swap two adjacent cubes only if their total weight is at most . She may repeat this swap on any adjacent pair as many times as she wants. Two rows are different if their sequences of cube weights differ.
For example, take 4 cubes with weights and . There are 3 possible rows. The first is the initial row. To get the second, swap the cubes at positions 2 and 3 to get . The cubes at positions 1 and 3 cannot be swapped because they are not adjacent. The cubes at positions 3 and 4 cannot be swapped because their total weight is 4, which is larger than . The last row comes from swapping the first two cubes, giving . With the same initial row and , there is only one possible row. With , there is also only one. With , there are 6 rows, and with , there are 12 rows.
Write a program named cubes that finds the number of different rows of cubes Octavia can obtain.
Input
The first line has two integers , the number of cubes, and , the maximum total weight of two adjacent cubes that can be swapped. The second line has positive integers , the weights in the initial row.
Output
Print a single line with the number of possible rows, modulo .
Constraints
Hint
In the second case, only the cubes with weights 1 and 3 can be swapped, so the answer is 2. The two possible rows are and .