codeforces 792 B. Counting-out Rhyme (模拟链表||约瑟夫环)

来源:互联网 发布:2016网络自制剧排行榜 编辑:程序博客网 时间:2024/06/05 01:09


题意:n个人占成一圈,每点ai个人淘汰一个,输出淘汰顺序 


解题思路:直接用数组模拟链表遍历即可


n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader is eliminated, and the next player after him becomes the new leader.

For example, if there are children with numbers [8, 10, 13, 14, 16] currently in the circle, the leader is child 13 and ai = 12, then counting-out rhyme ends on child 16, who is eliminated. Child 8 becomes the leader.

You have to write a program which prints the number of the child to be eliminated on every step.

Input

The first line contains two integer numbers n and k (2 ≤ n ≤ 1001 ≤ k ≤ n - 1).

The next line contains k integer numbers a1, a2, ..., ak (1 ≤ ai ≤ 109).

Output

Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.

Examples
input
7 510 4 11 4 1
output
4 2 5 6 1 
input
3 22 5
output
3 2 
Note

Let's consider first example:

  • In the first step child 4 is eliminated, child 5 becomes the leader.
  • In the second step child 2 is eliminated, child 3 becomes the leader.
  • In the third step child 5 is eliminated, child 6 becomes the leader.
  • In the fourth step child 6 is eliminated, child 7 becomes the leader.
  • In the final step child 1 is eliminated, child 3 becomes the leader.

#include<bits/stdc++.h>using namespace std;const int N = 111;int pre[N],nnext[N];int main(){    int n,m,num,first;    int last,Next;    int i,j;    cin>>n>>m;    pre[1]=n,nnext[1]=2;    for(i=2;i<=n;i++)    {        pre[i]=i-1;        nnext[i]=i+1;    }    nnext[n]=1;    first=1;    while(m--) {        cin>>num;        num%=n;        while(num--) {            first=nnext[first];        }        last = pre[first];        Next = nnext[first];        nnext[last] = Next;        pre[Next] = last;        cout<<first<<" ";        first = nnext[first];        n--;    }    cout<<endl;    return 0;}





0 0
原创粉丝点击