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. Lua参数绑定函数实现方法

    背景 对于某一个函数, 其被调用多次, 每次调用的入参都是一致的. 不想每次都填写参数, 如果能够定义一个新的函数, 将参数跟此函数绑定就棒哒哒了. local function pirntfunc( ...

  2. UE4入门与精通

    由于目前在使用UE4引擎,多少也有一些心得,比如在日常使用中会遇到一些问题.坑(潜规则)或者一些使用技巧等.本人决定开一个大坑,主要有两个目的:一是可以自己做个记录,二是可以给大家提供一些参考吧.主要 ...

  3. python---tcp/ip网络编程

    重点总结: 服务端:一直运行(while true),监听运行所在机器(ip)某端口,多线程或多进程接收客户端的socket请求 客户端:主动发起请求,需求知道服务器的ip和端口 服务端: # -*- ...

  4. hibernate 多表查询

    Hibernate主要支持两种查询方式:HQL查询和Criteria查询.前者应用较为广发,后者也只是调用封装好的接口. 现在有一个问题,就是实现多表连接查询,且查询结果集不与任何一个实体类对应,怎么 ...

  5. Android Bitmap转换WebPng图片导致损坏的分析及解决方案

    出现问题的code!!! private void saveImage(String uri, String savePath) throws IOException { // 创建连接 HttpUR ...

  6. [Android] 修改ImageView的图片颜色

    有两种方法: 方法1: ImageView imageView = (ImageView) findViewById(R.id.arrow_image); Drawable tipsArrow = i ...

  7. [原创]Visual Studio 使用 Just My Code引起无法断点

    今天遇到的问题,同样的代码,在一台机器上用Release配置可以命中断点,在另一台上用Release断点就都失效了.后来发现是因为断点失效的机器上设置了Just My Code.在Debug-Opti ...

  8. Material Design Lite,简洁惊艳的前端工具箱 之 交互组件。

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接, 博客地址为http://www.cnblogs.com/jasonnode/ . 网站上有对应 ...

  9. String空值判定

    不能用 str.equalsIgnoreCase(null). 写了个判定的方法,供以后直接调用: public static boolean isEmpty(String str) { if (st ...

  10. python之编写登陆接口(第一天)

    作业:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 针对此实例写了有二种类型的脚本,略有不同,具体如下: 帐号文件account.txt内容如下: sam 123 david ...