Magic Cards (Hard)
Time limit10sMemory limit1024 MB
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 cards numbered from to . A spectator comes to the stage, picks distinct cards, and gives them to Pak Ganesh. Pak Ganesh looks at the cards, discards one of the , and leaves the remaining cards on the table in any order. Pak Dengklek then looks at those 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 times with the same and .
Constraints
- A strategy for the magician and the assistant exists for the given and
For each call to choose_cards:
- for every such that .
- All the elements of
cardsare distinct.
For each call to find_discarded_card:
- All the inputs given are the same as the return values of
choose_cards, in random order.