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. nginx 客户端不缓存header

    location ~* \.(html|htm)$ { add_header Cache-Control no-store; }

  2. mongodb学习(一)

    操作系统环境:ubuntu. 安装mongodb:apt-get install mongodb 安装后运行:mongod提示:[initandlisten] exception in initAnd ...

  3. android 流量 压缩

    引用:http://my.eoe.cn/blue_rain/archive/340.html 对于目前的状况来说,移动终端的网络状况没有PC网络状况那么理想.在一个Android应用中,如果需要接收来 ...

  4. web分享QQ好友、QQ空间、新浪微博的api接口

    QZone: "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={{URL}}&title={{TITL ...

  5. struts 异常机制

    1.try-catch 在Action中通过try-catch捕获异常,在catch中返回字符串, 通过返回的字符串在Action的result元素中进行匹配,最终返回响应的页面 2. 局部异常映射 ...

  6. html5与html4的区别,如何一眼区分

    还是在面试过程中遇到的这个问题,但是当时感觉回答的不是很全面,回来以后自己做的总结: 以上这个问题如果教大家看源码,可能一眼很直观的就能认出,那个是html4,那个是html5,但是面试的时候问道这个 ...

  7. 手机sdk 开发

    题记:很多做游戏开发的人,估计都或多或少地接过渠道SDK,什么UC,当乐,91,小米,360......据统计国内市场当前不下于100家渠道,还包括一些没有SDK的小渠道.每个渠道SDK接入的方法呢, ...

  8. bootstrap——下拉菜单右对齐

    通过向 .dropdown-menu 添加 .pull-right 类来向右对齐下拉菜单. Bootstrap默认情况下,下拉菜单自动沿着父元素的上沿和左侧被定位为 100% 宽度. 为 .dropd ...

  9. Apache_proxy负载均衡和Session复制

    今天上网查了查资料,之前使用apache的jk模块做负载均衡.后来觉得jk的负载配置有点死板,只能按照负载权重值来进行请求的分发,没有做到比较智能的负载平衡,并且使用mod_jk访问页面发现确实比较慢 ...

  10. 图片使用base64展示代码,后台为jfinal

    前台使用ajax获取数据,下面步骤为把图片对应的id获取到,然后判断是否为空,不为空则发送请求获取数据,数据为base64数据格式: img需要注明数据类型格式:即data:image/jpg:bas ...