原题链接

Problem Description
You may heard of the Joseph Problem, the story comes from a Jewish historian living in 1st century. He and his 40 comrade soldiers were trapped in a cave, the exit of which was blocked by Romans. They chose suicide over capture and decided that they would form a circle and start killing themselves using a step of three. Josephus states that by luck or maybe by the hand of God, he and another man remained the last and gave up to the Romans.

Now the problem is much easier: we have N men stand in a line and labeled from 1 to N, for each round, we choose the first man, the k+1-th one, the 2*k+1-th one and so on, until the end of the line. These poor guys will be kicked out of the line and we will execute them immediately (may be head chop, or just shoot them, whatever), and then we start the next round with the remaining guys. The little difference between the Romans and us is, in our version of story, NO ONE SURVIVES. Your goal is to find out the death sequence of the man.

For example, we have N = 7 prisoners, and we decided to kill every k=2 people in the line. At the beginning, the line looks like this:

1 2 3 4 5 6 7

after the first round, 1 3 5 7 will be executed, we have

2 4 6

and then, we will kill 2 6 in the second round. At last 4 will be executed. So, you need to output 1 3 5 7 2 6 4. Easy, right?

But the output maybe too large, we will give you Q queries, each one contains a number m, you need to tell me the m-th number in the death sequence.

 
Input
Multiple cases. The first line contains a number T, means the number of test case. For every case, there will be three integers N (1<=N<=3000000), K(1<=K), and Q(1<=Q<=1000000), which indicate the number of prisoners, the step length of killing, and the number of query. Next Q lines, each line contains one number m(1<=m<=n).
 
Output
For each query m, output the m-th number in the death sequence.
 
Sample Input
1
7 2 7
1
2
3
4
5
6
7
 
Sample Output
1
3
5
7
2
6
4
 
Author
BUPT
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5867 5866 5865 5864 5863 
 
题意:输入n表示1到n ,输入k,q   去掉一些数,从1开始 1、k+1、2*k+1、3*k+1、4*k+1、5*k+1........直到i*k+1>n结束   然后剩下的数又成一个1到(n)  的序列,重复操作,直至序列为空,q次询问,每次输入x,输出第x次去掉的数的下标;
 
思路:仔细推一下,发现存在递推关系,若第i个数满足(i-1)%k==0 则a[i]=1;否则a[i]=a[i-(i-1)/k-1]+1;  并且在循环中用前行星记录当前数是在第几层去掉的,然后再一重循环从层次最大处开始给a[i]赋值是第i次去掉的数在序列中的下标;
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int N=;
int head[N];
int a[N];
struct Node
{
int to;
int next;
}node[N]; int main()
{
int T;
cin>>T;
while(T--)
{
int n,k,q;
scanf("%d%d%d",&n,&k,&q);
memset(head,,sizeof(head));
int tot=;
for(int i=;i<=n;i++)
{
if((i-)%k==) a[i]=;
else a[i]=a[i-(i-)/k-]+;
node[tot].to=i;
node[tot].next=head[a[i]];
head[a[i]]=tot++;
}
int cnt=n;
for(int i=tot-;i>=;i--)
{
for(int j=head[i];j;j=node[j].next)
{
a[cnt--]=node[j].to;
}
}
while(q--)
{
int x;
scanf("%d",&x);
printf("%d\n",a[x]);
}
}
return ;
}

2016暑假多校联合---Death Sequence(递推、前向星)的更多相关文章

  1. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  2. 2016暑假多校联合---Another Meaning

    2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...

  3. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  4. 2016暑假多校联合---Substring(后缀数组)

    2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...

  5. 2016暑假多校联合---To My Girlfriend

    2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...

  6. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

  7. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  8. hdu 5860 Death Sequence(递推+脑洞)

    Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...

  9. 2016暑假多校联合---GCD

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

随机推荐

  1. 爱上MVC3~布局页的继承与扩展

    回到 目录 在MVC3中引入了Razor引擎,这对于代码的表现力上是个突破,同时母板页也变成了_Layout,所以,我们就习惯上称它为布局页面,在razor里,布局页面是可以继承的,即,一个上下公用的 ...

  2. javaweb学习总结—Apache的DBUtils框架学习

    注明: 本文转载自http://www.cnblogs.com/xdp-gacl/p/4007225.html 一.commons-dbutils简介 commons-dbutils 是 Apache ...

  3. iOS---NSAutoreleasePool自动释放原理及详解

    前言:当您向一个对象发送一个autorelease消息时,Cocoa就会将该对象的一个引用放入到最新的自动释放池.它仍然是个正当的对象,因此自动释放池 定义的作用域内的其它对象可以向它发送消息.当程序 ...

  4. Maven Myeclipse 搭建项目

    一.maven概述 Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具! 简单说Maven 是一个项目管理和构建自动化工具 二.环境搭建 1. ...

  5. 重构Mybatis与Spring集成的SqlSessionFactoryBean(1)

    一般来说,修改框架的源代码是极其有风险的,除非万不得已,否则不要去修改.但是今天却小心翼翼的重构了Mybatis官方提供的与Spring集成的SqlSessionFactoryBean类,一来是抱着试 ...

  6. SQL-基础知识

    SQL Server中的关于时间转换和获取时间的方法 1.获取当前UTC时间 GETUTCDATE()

  7. Oracle Dataguard之switchover

    Oracle Dataguard的角色转换包含两类:Switchover和Failover.Switchover指主备之间角色转换,主库降为备库,备库升级为主库.而failover则是指主库出现问题时 ...

  8. web基础---->request的请求参数分析

    当contentType为application/json的时候,在servlet中通过request.getParameter得到的数据为空.今天我们就java的请求,分析一下request得到参数 ...

  9. PHP函数处理函数实例详解

    1. call_user_func和call_user_func_array:  以上两个函数以不同的参数形式调用回调函数.见如下示例: <?php class AnotherTestClass ...

  10. Elasticsearch 管理文档

    ES支持近实时的索引.更新.查询.删除文档,近实时就意味着刚刚索引的数据需要1秒钟后才能搜索到,这也是与传统的SQL数据库不同的地方. 更多的ES文档资料参考:Elasticsearch官方文档翻译 ...