Description

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 ≤ 100, 1 ≤ 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.

Sample Input

Input
7 5
10 4 11 4 1
Output
4 2 5 6 1 
Input
3 2
2 5
Output
3 2 

Hint

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个人围成一圈坐着,每个人(编号定为1~n)有一个数字a(限定条件:a的范围为1~n,且每个数字只出现一次),

现在如果指定一个人为leader,下一个leader为从他的顺时针方向第一个人开始数 a .以此类推,现在给出一系列按顺序成为leader的人的编号,

问是否存在这样一种情况,能够在限定条件内确定每个人的数字,满足成为leader顺序的结果,如果有多种结果,输出一种,没有输出 -1。

解题思路:这其实就是一道约瑟夫环问题

 #include<stdio.h>
#include<deque>
#include<string.h>
using namespace std;
int main()
{
int n,k,i,j,b[],a,c;
deque<int> q;
scanf("%d%d",&n,&k);
for(i=;i<=n;i++)
{
q.push_back(i);
}
for(i=;i<=k;i++)
{
scanf("%d",&b[i]);
}
for(i=;i<=k;i++)
{
b[i]=b[i]%n;
for(j=;j<=b[i];j++)
{
a=q.front();
q.pop_front();
q.push_back(a);
}
c=q.front();
printf("%d ",c);
q.pop_front();
n--;
}
return ;
}

B. Counting-out Rhyme(约瑟夫环)的更多相关文章

  1. Roman Roulette(约瑟夫环模拟)

    Roman Roulette Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. C#实现约瑟夫环问题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace orde ...

  3. C语言数组实现约瑟夫环问题,以及对其进行时间复杂度分析

    尝试表达 本人试着去表达约瑟夫环问题:一群人围成一个圈,作这样的一个游戏,选定一个人作起点以及数数的方向,这个人先数1,到下一个人数2,直到数到游戏规则约定那个数的人,比如是3,数到3的那个人就离开这 ...

  4. C语言链表实现约瑟夫环问题

    需求表达:略 分析: 实现: #include<stdio.h> #include<stdlib.h> typedef struct node { int payload ; ...

  5. javascript中使用循环链表实现约瑟夫环问题

    1.问题 传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第 ...

  6. tc 147 2 PeopleCircle(再见约瑟夫环)

    SRM 147 2 600PeopleCircle Problem Statement There are numMales males and numFemales females arranged ...

  7. HDU 3089 (快速约瑟夫环)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...

  8. 约瑟夫环(Josehpuse)的模拟

    约瑟夫环问题: 0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字,求出这个圆圈里剩下的最后一个数字. 这里给出以下几种解法, 1.用队列模拟 每次将前m-1个元 ...

  9. C++ 约瑟夫环问题

    约瑟夫环比较经典了 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直 ...

随机推荐

  1. VB

    on error resume next: 从该语句开始,遇到错误时程序不会中止,也不会出现错误提示,将继续运行.作用范围直至程序结束或语句所在函数等结束 Public Property :可读也可写 ...

  2. java基础需要掌握的内容

    一.Java的基本程序设计结构 二.对象与类 三.继承 四.接口.lambda表达式与内部类 五.异常,断言与日志 六.泛型程序设计 七.集合 八.并发(线程) 九.输入与输出(IO流) 十.网络 十 ...

  3. summernote富文本编辑器的使用

    最近在开发一个微信公众号的后台,微信公众号编辑的文章一直没有得到很好地适应,大多数人也是在其他的编辑软件中编辑好之后直接去复制到微信公众平台中,考虑到复制后会排版出现问题,所以给大家推荐一款很不错的W ...

  4. python3 datetime和time获取当前日期和时间

    import datetime import time # 获取当前时间, 其中中包含了year, month, hour, 需要import datetime today = datetime.da ...

  5. CentOS7 更换OpenStack-queens源

    根据官网的安装文档来对OpenStack搭建时碰到一个问题,安装完centos-release-openstack-queens后相当于是增加了一个OpenStack的源,但是因为这个源是在国外安装一 ...

  6. Java调用WeChat's API总结

    微信公众号结合着内置浏览器,有着普通浏览器无法完成的服务,前者可以获取浏览页面的微信用户的信息,从而根据信息为用户提供基于微信的更多服务:而后者仅仅能够浏览页面,通过用户的输入信息与用户互动. 本人根 ...

  7. Linux网卡配置文件 参数详解

    之所以弄这玩意儿是图个清晰方便,最近这段时间弄了好十来次虚拟机网络了ubuntu,centos,rhat7各种折腾,其实把网上各种命令行下的攻略折腾最后关键无非都是对/etc/sysconfig/ne ...

  8. MongoDB基础命令及操作

    MongoDB:NoSQL数据库 MongoDB中的重要指示点 MongoDB中的三要素 数据库 集合 文档 MongoDB中的数据存储是以Bson的形式存储的,Bson是二进制的json,所以看上去 ...

  9. STM32F4 SPI双机通讯调试总结

    1.如果查询方式进行数据收发,不管是Master,还是Slave,流程如下:(假设收发n+1个字节) a.等待TXE,写入一个字节 b.等待TXE,写入一个字节 c.等待RXNE,读取一个字节 循环b ...

  10. linux使用docker-compose部署软件配置

    本篇将分享一些 docker-compose 的配置,可参考其总结自己的一套基于docker的开发/生产环境配置.下面话不多说了,来一起看看详细的介绍吧 安装docker及docker-compose ...