约瑟夫环, 简而言之就是 个人
围成一圈, 第一个人从
开始报数, 报到
的人将被杀, 下一个人接着从
开始报, 如此反复, 最后一个剩下的为胜利者.
如 ,
时, 被杀顺序为
. 此时
为胜利者.
今天的问题并不是经典的约瑟夫环问题,而有一定的进阶. 在本题中, 是循环变化的, 存在一个序列
, 第一次报到
出列, 第二次报到
出列,
, 报到
后, 重新从
开始, 求最后的胜利者(下标从
开始).
Joseph's ring, in short, is a circle of people
, the first one counting from zero, to the
will be killed, and the next one counting from zero. And so on and so on and so on and so on.
For example, when ,
, the order will be
, and
is a winner.
Today, I want to give you a similar problem but not the classic Joseph's ring problem, with a certain degree of progression: in this case, is cyclic, and
belongs to the sequence
, the first person will be killed if he count
, and next will be killed if count
,
, and when reached
, it moves down again to
. Find the number of the last person who will win the game(index start from 0).
题目有多组输入 (最多5组), 每组输入两行.
第一行输入两个整数,
. 分别表示人数和序列
的长度
第二行输入个整数
: 表示序列
.
Multiple groups of input (up to 5 groups), each group has two lines.
First line input two integer,
, denote the total number of people and the length of sequence M.
Second line inputinteger
, denote the sequence
.
对于每组输出样例输出一行.
第一行输出一个整数, 表示最后一位胜利者
.
For each case should output one line.
First line output a integer, denote the last winner
.