Under construction

Still being built. Things may change

Magic Cards (Hard)

Time limit10sMemory limit1024 MB

Summary
Pick K distinct cards from 1 to N, discard one, and order the remaining K-1 cards so the magician can recover the discarded card.
Level

Medium7 of 10

Topics
Combinatorics, Math, Implementation
Solved
No attempts yet

Problem

Pak Dengklek will perform a magic trick. His assistant, Pak Ganesh, has NN cards numbered from 11 to NN. A spectator comes to the stage, picks KK distinct cards, and gives them to Pak Ganesh. Pak Ganesh looks at the cards, discards one of the KK, and leaves the remaining K−1K - 1 cards on the table in any order. Pak Dengklek then looks at those K−1K - 1 cards and must determine the discarded card.

Pak Dengklek and Pak Ganesh cannot communicate once the trick starts, but they can agree on a strategy beforehand. Design that strategy for them. This time, they will perform the trick QQ times with the same NN and KK.

Constraints

  • 2≤K≤82 ≤ K ≤ 8
  • K≤NK ≤ N
  • A strategy for the magician and the assistant exists for the given NN and KK
  • 1≤Q≤50 0001 ≤ Q ≤ 50\,000

For each call to choose_cards:

  • 1≤cards[i]≤N1 ≤ cards[i] ≤ N for every ii such that 0≤i≤K−10 ≤ i ≤ K - 1.
  • All the elements of cards are distinct.

For each call to find_discarded_card:

  • All the inputs given are the same as the QQ return values of choose_cards, in random order.

Examples1

  1. Example 1

    Input
    3 3 1
    1 2 3
    
    Expected output
    2 3