HDU 5860 Death Sequence(死亡序列)

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

 

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.
你可能听说过约瑟夫问题,这个故事来源于公元1世纪的犹太历史学家。他和40位战友被困在一个山洞中,洞口盘踞这罗马人。他们宁死不从,于是围成一个圈,每此经过两个人自杀一人。约瑟夫似乎得“天神眷顾”,和另一人得以苟活,降于罗马人。

现在这个问题更加简单:现有N个从1到N编号的人,每回合选择第1人,第k+1人,第2*k+1人,以此类推直至末尾。这些可怜虫会被即刻揪出行刑(或斩首,或枪毙,或其他),剩余的人则开始下一回合。我们的故事与罗马版略有不同,魔劫千万,生机尽灭。你的任务是找出这些人的死亡序列。

举个例子,现有N = 7个人,我们选择杀死队伍中k=2的人。一开始的队伍如下:

第一回合后,   7卒,还剩

随后,我们在第二回合杀死2 。4最后死。因此你需要输出1      。简单否?

然而这样输出太长了,我们会给你Q个询问,每个询问有一个数字m,你需要输出死亡序列第m个数。

CN

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).
多组用例。第一行为一个整数T,表示测试用例的数量。
对于每个测试用例,有三个整数N (<=N<=),K(<=K),和Q(<=Q<=),分别表示囚徒的数量,死亡间隔数,还有询问的数量。
随后N行,每行有一个整数m(<=m<=n)。

CN

Output - 输出

For each query m, output the m-th number in the death sequence.
对于每个询问m,输出死亡序列中第m个数。

CN

Sample Input - 输入样例

1
7 2 7
1
2
3
4
5
6
7

Sample Output - 输出样例

1
3
5
7
2
6
4

题解

  动态规划。
  为了取模方便,可以从0开始。
  用turn[i]表示,表示当前位置可以存活多少轮,利用下一轮中的位置比本轮中的位置多苟活1s,可以用得到当前的苟活数。
  即:turn[i] = i%k ? turn[i - i / k - 1] + 1 : 0
  还能再用一个数组sum算出每回合死亡的人生,为了最后的输出做准备。

代码 C++

 #include <cstdio>
#define mx 3000005
int turn[mx], sum[mx], opt[mx];
int main(){
int t, n, k, q, m, i, j;
for (scanf("%d", &t); t; --t){
scanf("%d%d%d", &n, &k, &q);
sum[] = ;
for (j = n, i = ; j; ++i){
sum[i] = j / k;
if (j%k) ++sum[i];
j -= sum[i];
sum[i] += sum[i - ];
}
for (i = ; i < n; ++i){
turn[i] = i%k ? turn[i - i / k - ] + : ;
opt[++sum[turn[i]]] = i + ;
}
for (i = ; i < q; ++i){
scanf("%d", &m);
printf("%d\n", opt[m]);
}
}
return ;
}

HDU 5860 Death Sequence(死亡序列)的更多相关文章

  1. HDU 5860 Death Sequence(递推)

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

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

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

  3. 2016 Multi-University Training Contest 10 || hdu 5860 Death Sequence(递推+单线约瑟夫问题)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 题目大意:给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀.然后 ...

  4. HDU 5860 Death Sequence

    用线段树可以算出序列.然后o(1)询问. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<c ...

  5. hdu 6299 Balanced Sequence (括号序列,贪心)

    大意: 记$f(t)$表示字符串$t$的最长括号匹配子序列, 给定n个括号序列, 求它们重排后的最大f(t). 首先可以注意到一个括号序列中已经匹配的可以直接消去, 一定不会影响最优解. 那么这样最终 ...

  6. HDU 1560 DNA sequence(DNA序列)

    HDU 1560 DNA sequence(DNA序列) Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K  ...

  7. 2016暑假多校联合---Death Sequence(递推、前向星)

    原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...

  8. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  9. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

随机推荐

  1. weblogic

    停止(2) 更新 原路径 下一步 激活更新 启动(1)

  2. 第九章伪代码编程过程 The PseudoCode Programming Process

    目录: 1.创建类和子程序的步骤概述 2.伪代码 3.通过伪代码编程过程创建子程序 4.伪代码编程过程的替代方案 一.创建类和子程序的步骤概述 (1)创建一个类的步骤 1.创建类的总体设计 2.创建类 ...

  3. Production环境中iptables常用参数配置

    production环境中iptables常用参数配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我相信在实际生产环境中有很多运维的兄弟跟我一样,很少用到iptables的这个 ...

  4. DIY PIXHAWK APM等飞控用的PPM转接板

    需要的硬件 一块arduino pro mini(推荐这个,比较小,当然如果你没有USB转转口的烧写工具买个ardunio nano板也是不错的,直接用USB线连接电脑就可以,用nano板要注意.它的 ...

  5. 最简单的基于JSP标准标签库的增删改查

    创建数据库中的表:CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DE ...

  6. Python爬虫爬取糗事百科段子内容

    参照网上的教程再做修改,抓取糗事百科段子(去除图片),详情见下面源码: #coding=utf-8#!/usr/bin/pythonimport urllibimport urllib2import ...

  7. win7,安装node失败

    win7下,下载node安装包,安装之后 打开命令行输入 node -v,仍然提示命令不可用. 解决办法: 找到环境变量: 在用户变量里修改 path: 添加系统变量 NODE_PATH: 关机重启电 ...

  8. Tanks坦克大战

    创建工程,场景: 将素材导入,Unity5以上的版本,无需担心素材包的路径问题,中文路径也可以直接导入了,简单方法就是将素材包直接拖到Project面板 游戏所需要的场景在Prefabs里的Level ...

  9. AppCan开发者资料分享(定期更新)

    开发者培训 上海20150925开发者培训资料:链接:http://pan.baidu.com/s/1mgCLzz6 密码:mqgi 版权声明:本文为博主原创文章,未经博主允许不得转载.

  10. Dynamics AX 2012 R2 AIF No Endpoint Behavior Named 'clientEndpointBehavior'

          最近,Reinhard在使用Http Adapter类型的AIF入站端口时,总是报以下错误: Server Error in '/MicrosoftDynamicsAXAif60' App ...