No.123 Card Shuffle
タグ : / 解いたユーザー数 562
作問者 :
nmnmnmnmnmnmnm
Problem Statement
There are cards with the numbers from $1$ to $N$ written on them, $1$ card for each number, for a total of $N$ cards.
The cards are stacked in order from $1$ to $N$ starting from the top. The topmost card is the card numbered $1$.
Take out the $A_i$-th card from the top and place it on the top of the stack. This counts as $1$ shuffle.
What is the number on the topmost card after $M$ shuffles have been performed?
Input
$N$ $M$ $A_1\,A_2\dots\,A_m$
$N$ is the largest number on the cards and also the number of cards. ($1 \le N \le 50$)
$M$ is the number of shuffles. ($1 \le M \le 100000$)
$A_i$ means that in the $i$-th shuffle, the $A_i$-th card from the top is taken out and placed on the top. ($1 \le A_i \le N$)
Output
Output the number on the topmost card after performing $M$ shuffles.
Do not forget the newline at the end.
Sample
Sample 1
Input
3 1 2
Output
2
Initially, 3 cards numbered from 1 to 3 are stacked, in the order 1,2,3 from the top.
In the 1st shuffle, the 2nd card from the top, which is the card numbered 2, is taken out and placed on the top.
Therefore, the topmost card is 2.
Sample 2
Input
3 2 3 2
Output
1
Initially, 3 cards numbered from 1 to 3 are stacked, in the order 1,2,3 from the top.
In the 1st shuffle, the 3rd card from the top, which is the card numbered 3, is taken out and placed on the top.
The cards are now stacked in the order 3,1,2 from the top.
In the 2nd shuffle, the 2nd card from the top, which is the card numbered 1, is taken out and placed on the top.
Therefore, the topmost card is 1.
Sample 3
Input
4 4 1 1 1 1
Output
1
Initially, 4 cards numbered from 1 to 4 are stacked, in the order 1,2,3,4 from the top.
The shuffle of taking out the 1st card from the top and placing it on the top is performed 4 times.
The order of the cards does not change at all. Therefore, the number on the topmost card is 1.
Sample 4
Input
10 8 6 10 4 2 7 1 4 3
Output
10
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。