問題一覧 > 通常問題

No.123 Card Shuffle

レベル : / AI推定難易度 : 126 / 実行時間制限 : 1ケース 5.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 562
作問者 : nmnmnmnmnmnmnm
お気に入りにしたユーザー ProblemId : 284 / 自分の提出
問題文最終更新日: 2026-06-25 01:59:35
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 75 (順位表) の他の問題:

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もしくは右上の雲マークをクリックしてアカウントを作成してください。