codeforces 792B - Counting-out Rhyme

codeforces 792B - Counting-out Rhyme 

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 aipeople 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.

Example
Input
7 5
10 4 11 4 1
Output
4 2 5 6 1 
Input
3 2
2 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.

【分析】有N個人,K個數ai,從第一個開始,每數到第ai個人淘汰,問每次淘汰的人的編號

數組模擬

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define cl(a,b) memset(a,b,sizeof a);
int vis[101];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        cl(vis,0);
        int index = 0;
        for(int i=0;i<m;i++){
            int x;
            scanf("%d",&x);
            x %= (n-i);
            while(x){
                index %= n;
                if(!vis[index]){
                    x--;
                }
                index++;
            }
            index%=n;
            while(vis[index]){
                index++;
                index%=n;
            }
            vis[index]=1;
            if(i)
                printf(" %d",index+1);
            else
                printf("%d",index+1);
        }
        puts("");
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章