约瑟夫环Pro
题号:NC231193
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

约瑟夫环, 简而言之就是 n 个人 围成一圈, 第一个人从 0 开始报数, 报到 m-1 的人将被杀, 下一个人接着从 0 开始报, 如此反复, 最后一个剩下的为胜利者.

, 时, 被杀顺序为 a_1,a_3,a_0,a_4. 此时 a_2 为胜利者.

今天的问题并不是经典的约瑟夫环问题,而有一定的进阶. 在本题中, m 是循环变化的, 存在一个序列 , 第一次报到 m_0 - 1 出列, 第二次报到 m_1 -1 出列, , 报到 后, 重新从 m_0 开始, 求最后的胜利者(下标从 0 开始).


Joseph's ring, in short, is a circle of n people , the first one counting from zero, to the m-1 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 a_1,a_3,a_0,a_4, and a_2 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, m is cyclic, and m belongs to the sequence , the first person will be killed if he count m_0 -1, and next will be killed if count m_1 -1, , and when reached m_k, it moves down again to m_0. Find the number of the last person who will win the game(index start from 0).

输入描述:

题目有多组输入 (最多5组), 每组输入两行.

第一行输入两个整数 n, k. 分别表示人数和序列 M 的长度

第二行输入 k 个整数 : 表示序列 M.


Multiple groups of input (up to 5 groups), each group has two lines.

First line input two integer n, k, denote the total number of people and the length of sequence M.

Second line input k integer , denote the sequence M.


输出描述:

对于每组输出样例输出一行.

第一行输出一个整数 p, 表示最后一位胜利者 a_p.

For each case should output one line.

First line output a integer p, denote the last winner a_p.


示例1

输入

复制
5 2
2 3
5 2
1 2

输出

复制
0
4

说明

0<n\le 100,0<k\le 10,a_i\le100