p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt }
h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; font-size: 24.0000pt }
span.10 { font-family: "Times New Roman" }
span.15 { font-family: "Times New Roman"; color: rgb(26,92,200) }
span.16 { font-family: "Times New Roman"; color: rgb(26,92,200) }
p.pre { margin: 0pt; margin-bottom: .0001pt; text-align: left; font-family: 宋体; font-size: 12.0000pt }
span.msoIns { text-decoration: underline; color: blue }
span.msoDel { text-decoration: line-through; color: red }
table.MsoNormalTable { font-family: "Times New Roman"; font-size: 10.0000pt }
div.Section0 { }

HDU 5860 Death Sequence(死亡序列)

00 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[] = ;
         ; j; ++i){
             sum[i] = j / k;
             if (j%k) ++sum[i];
             j -= sum[i];
             sum[i] += sum[i - ];
         }
         ; i < n; ++i){
             turn[i] = i%k ? turn[i - i / k - ] +  : ;
             opt[++sum[turn[i]]] = i + ;
         }
         ; i < q; ++i){
             scanf("%d", &m);
             printf("%d\n", opt[m]);
         }
     }
     ;
 }

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

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

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

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

  4. HDU 1711 Number Sequence(数列)

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

  5. HDU 1005 Number Sequence(数列)

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

  6. python3 第十七章 - sequence(序列)

    之前我们在讲for循环语句时就提到过序列,那么什么是序列(sequence)? 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 —— 它的索引(位置),第一个索引是0,第二个索引 ...

  7. HDU 4604 Deque 最长子序列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4604 Deque Time Limit: 4000/2000 MS (Java/Others)     ...

  8. HDU 2454 Degree Sequence of Graph G(Havel定理 推断一个简单图的存在)

    主题链接:pid=2454">http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiya ...

  9. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

随机推荐

  1. Apache:如何访问共享目录

    环境说明:Apache的版本是2.4.10,共享目录有两种情况,一种是windows server的目录共享,还有一种是linux的NAS.无论访问哪一种共享目录,都需要用户名和密码. 问题说明:如何 ...

  2. Leetcode: Word Squares &amp;&amp; Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  3. 高性能JS笔记4——算法和流程控制

    一.循环 for.while.do while三种循环的性能都没有多大区别.foreach 的性能较其他三种差 . 既然循环没有多大区别,注意循环内的代码控制. 减少迭代次数. 减少迭代工作量. 推荐 ...

  4. java nio2

    Buffer的基本用法 使用Buffer读写数据一般遵循以下四个步骤: 写入数据到Buffer 调用flip()方法 从Buffer中读取数据 调用clear()方法或者compact()方法 当向b ...

  5. mac使用wget下载网站(仿站)

    wget -c -r -np -k -L -p http://www.xxxx.com 参考 wget的安装 http://blog.csdn.net/ssihc0/article/details/7 ...

  6. OpenStack swift安装

    由于实验室项目需要云存储系统,因此在实验室服务器上搭建一个基于OpenStack swift的云存储系统. 最开始按照官方网站上介绍的方法,使用SAIO方式安装http://docs.openstac ...

  7. IOS 项目名称修改(XCODE4.6)

    最近为了保存苹果商店已有版本软件,打算重新上传一个程序,与原来的软件仅样式不同.在修改网plist文件中的名称后,archive时报错了,结果发现时工程名称没有修改到.下面就与大家分享下修改已有项目名 ...

  8. POJ-魔兽世界之一:备战

    描述 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部.两个司令部之间是依次排列的若干城市. 红司令部,City 1,City 2,……,City n,蓝司令部 两军的司令部都会制造武士.武士一共 ...

  9. Silverlight并行下载与串行下载

    思路清晰后仅仅只需百来行代码便可轻松编写出一套完整的资源动态下载组件- SerialDownloader和ParallelDownloader,它们共用一个完成资源表,且串行下载集成了优先机制(Dow ...

  10. 智能家居项目(2):项目project框架的搭建

    项目管理器: Linux中的项目管理器"make"有些类似于windows中的Visual C++里的"project",它是一种控制编译或者反复编译软件的工具 ...