Shuffled Deck
Time limit2sMemory limit512 MB
For a deck of P distinct cards, find how many times the given interleaving shuffle must be repeated until the deck returns to its sorted order.
- Level
Medium5 of 10
- Topics
- Math, Sorting, Number theory
- Solved
- No attempts yet
Problem
A deck holds an even number of cards , all distinct (). At the start the deck is perfectly sorted: the first card is , the second card is , and so on until the last card, which is .
A dealer then repeats a shuffle made of two steps.
- Split the deck into two halves.
- Interleave the cards of the two halves. If the card order at the start of step 1 is , then after step 2 the order becomes .
Given the number of cards in the deck, write a program that determines how many times this shuffle must be repeated for the deck to return to its original sorted order.
Input
The first line contains an even integer , the number of cards in the deck (). The value corresponds to in the description above.
Output
Print one line with a single integer, the minimum number of times the shuffle must be repeated for the deck to be sorted again.