Find the Joker 2
InterviewTime limit1sMemory limit1024 MB
Given shuffles that rotate the deck by one card or point back to an earlier deck state, find the joker's final position starting from position 1.
- Level
Medium4 of 10
- Topics
- Tree, Prefix sum, Math
- Solved
- No attempts yet
Problem
Suhyeon, a student at Chung-Ang University, learned card-cheating techniques. This time, after watching a magic video on YouTube, Suhyeon wants to copy the trick. Suhyeon places the joker on top of a card deck and shuffles the cards to send the joker to a desired position.
The deck has cards in total: blank cards and 1 joker card. Call the top card card 1 and the bottom card card . In the initial deck, the joker is at position 1.
Suhyeon shuffles the cards times. There are 3 shuffle methods.
1: Move card on top of card 1. Repeat this times.2: Move card 1 under card . Repeat this times.3: Change the deck state to the deck state after shuffles from the initial state. If , change to the initial deck.- This does not turn back the deck's history. It only changes the deck state.
Input
The first line contains and , separated by a space.
The next lines describe the shuffles. Line describes the -th shuffle.
Output
Print the position of the joker from the top of the deck after all shuffles are finished.
Constraints
- All input values are integers.