Under construction

Still being built. Things may change

Shuffle

Time limit1sMemory limit512 MB

Summary
Recover the episode number on each CD by grouping CDs into boxes and asking for the set of episodes in each box, using at most Q queries.
Level

Medium6 of 10

Topics
Array, Bit manipulation, Implementation
Solved
No attempts yet

Problem

Lim Li loves anime and has recently bought a complete season of Umaru from Amazon. The NN episodes of Umaru come in a set of NN CDs, with one episode on each CD. The CDs are labelled 1,…,N1, \ldots, N, and the episodes are also numbered 1,…,N1, \ldots, N. She was ready to start watching when she realised that, due to a manufacturing fault, the episode numbers do not match the labels on the CDs. She contacted Amazon, which said the CD labels do not match the episode numbers, but the episodes are only shuffled and no single episode is missing from the set. To avoid spoilers, Lim Li does not want to play the CDs herself to find which episode is on each CD, so she asks her friend Rar the Cat for help. The procedure is as follows. Lim Li groups her CDs into BB boxes with KK CDs each (N=B×KN = B \times K). The boxes are shipped to Rar. In transit, the CDs in each box may move around within the box, and the boxes may arrive in a different order. Rar plays the CDs on his own CD player and writes down the set of episodes he finds in each box. He sends this information back to Lim Li on BB separate pieces of paper, each listing the KK episode numbers inside one box. The entire set of NN CDs is then returned to Lim Li. This procedure can be done a maximum of QQ times, after which Rar gets annoyed and stops helping. Find the episode number on each CD with as little help from Rar as possible.

Constraints

B,K≥2B, K \ge 2, N≥6N \ge 6, and N=B×KN = B \times K.

Hint

Suppose N=6N = 6, B=3B = 3, K=2K = 2, Q=100Q = 100, and the test case belongs to Subtask 2. The episodes are [3,1,4,5,2,6][3, 1, 4, 5, 2, 6]. Your function is called with solve(6, 3, 2, 100, 2). A possible interaction: shuffle([[1, 2], [3, 4], [5, 6]]) = [[6, 2], [5, 4], [3, 1]]. The boxes are misordered and the CDs in each box are shuffled, so Rar may receive the boxes as [[6, 5], [4, 3], [1, 2]] and reports [[6, 2], [5, 4], [3, 1]]. Next, shuffle([[2, 6], [3, 1], [5, 4]]) = [[6, 1], [2, 5], [4, 3]], where the boxes are shuffled and Rar reports [[6, 1], [2, 5], [4, 3]]. Then shuffle([[6, 5], [4, 2], [3, 1]]) = [[5, 1], [3, 4], [2, 6]]. Your program then decides it has enough information and concludes that the episodes are [3,4,1,5,2,6][3, 4, 1, 5, 2, 6], so it returns [3,4,1,5,2,6][3, 4, 1, 5, 2, 6]. The episodes are correct and fewer than 100 queries were used, so this test case is accepted.

Examples1

  1. Example 1

    Input
    6 3 2 100
    
    Expected output
    1 2 3 4 5 6