HDU 5860 Death Sequence(递推)
HDU 5860 Death Sequence(递推)
题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860
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 7after the first round, 1 3 5 7 will be executed, we have2 4 6and 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
题意:
给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀。然后剩下的人重新编号从1~剩余的人数。按照上面的方式杀。问第几次杀的是谁。
题解:
这道题首先我们先将编号改成从0开始。这样如果i%k0那么我们可以知道第i个数字第一轮就被杀死。如果i%k!=0,那么我们知道在这之前一轮杀死了i/k+1个人。这样我们就得到递推式。
定义dp[i]代表第i轮杀死多少人。num[i]表示i在当前轮第几个被杀死。
对于i%k0我们可以知道第一轮被杀死,所以dp[i]=0,num[i]=i/k+1。
对于i%k!=0通过前面我们知道他们的状态和i-i/k-1有关。故dp[i]=dp[i-i/k-1]+1,num[i]=num[i-i/k-1]。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3001000;
int dp[maxn],num[maxn],add[maxn],ans[maxn];
int n,k,q;
void init()
{
int temp = n;
int acl = 0;
add[0] = 0; //add[i]前i轮kill几个;
while (temp){
acl++;
add[acl] = add[acl-1] + (temp-1)/k+1;
temp -= (temp-1)/k+1;
}
for (int i = 0; i < n; i++){
if (i%k == 0){
dp[i] = 0;
num[i] = i/k+1;
}else {
dp[i] = dp[i-i/k-1] + 1;
num[i] = num[i-i/k-1];
}
}
for (int i = 0; i < n; i++){
ans[add[dp[i]]+num[i]] = i;
}
}
int main()
{
int t;
scanf("%d",&t);
while (t--){
scanf("%d %d %d",&n,&k,&q);
init();
int cha;
while (q--){
scanf("%d",&cha);
printf("%d\n",ans[cha]+1);
}
}
return 0;
}
HDU 5860 Death Sequence(递推)的更多相关文章
- hdu 5860 Death Sequence(递推+脑洞)
Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...
- HDU 5860 Death Sequence(死亡序列)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5950 Recursive sequence 递推转矩阵
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu 5950 Recursive sequence 递推式 矩阵快速幂
题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...
- 2016 Multi-University Training Contest 10 || hdu 5860 Death Sequence(递推+单线约瑟夫问题)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 题目大意:给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀.然后 ...
- HDU 5860 Death Sequence
用线段树可以算出序列.然后o(1)询问. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<c ...
- HDU 2085 核反应堆 --- 简单递推
HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...
- hdu-5496 Beauty of Sequence(递推)
题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java ...
- hdu 2604 Queuing(dp递推)
昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...
随机推荐
- 统计知识选讲(一)——主成分分析(PCA)的思想
主成分分析的主要目的是希望用较少的变量去解释原来资料中的大部分变异,将我们手中许多相关性很高的变量转化成彼此相互独立或不相关的变量,从而达到降维的目的.在原始数据“预处理”阶段通常要先对它们采用PCA ...
- Python中的eval()、exec()及其相关函数
刚好前些天有人提到eval()与exec()这两个函数,所以就翻了下Python的文档.这里就来简单说一下这两个函数以及与它们相关的几个函数,如globals().locals()和compile() ...
- 浅析CDN存在的必要性
CDN行业从出现至今,已经有近20年的历史.但是直到近些年互联网进入超高速发展阶段,CDN才得以得到更广泛的应用和发展.最开始,CDN的主要任务只是简单的内容分发,对于静态内容的加速没有问题.但是随着 ...
- Laravel5.3 流程粗粒度分析之bootstrap
从laravel入口文件index里面外面不难定位到Illuminate\Foundation\Http\Kernel的handle方法,同样也不难发现handle方法的核心是 $response = ...
- 嵌入式SQL
一.包含嵌入式SQL 程序的处理过程 由预处理程序对源程序进行扫描,识别出ESQL语句 把它们转换成主语言的函数调用语句,使主语言编译程序能够识别 最后由主语言的编译程序将整个源程序编译成目标码 ...
- 哈工大数据库系统 实验:练习并熟练掌握交互式 SQL 语言
实验目的:基于给定的 OrderDB 数据库, 练习并熟练掌握交互式 SQL 语言实验环境:sql sever 2008 附:OrderDB 表结构及表间的关系 /* 1 查询职工工资按高低排序的前2 ...
- [bzoj2957][楼房重建] (线段树)
Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...
- Python random模块(获取随机数)常用方法和使用例子
random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...
- Java基础知识之文件操作
流与文件的操作在编程中经常遇到,与C语言只有单一类型File*即可工作良好不同,Java拥有一个包含各种流类型的流家族,其数量超过60个!当然我们没必要去记住这60多个类或接口以及它们的层次结构,理解 ...
- JavaScript的Array.prototype.filter()详解
摘抄与:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 概述 ...
题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860
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 7after the first round, 1 3 5 7 will be executed, we have2 4 6and 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
题意:
给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀。然后剩下的人重新编号从1~剩余的人数。按照上面的方式杀。问第几次杀的是谁。
题解:
这道题首先我们先将编号改成从0开始。这样如果i%k0那么我们可以知道第i个数字第一轮就被杀死。如果i%k!=0,那么我们知道在这之前一轮杀死了i/k+1个人。这样我们就得到递推式。
定义dp[i]代表第i轮杀死多少人。num[i]表示i在当前轮第几个被杀死。
对于i%k0我们可以知道第一轮被杀死,所以dp[i]=0,num[i]=i/k+1。
对于i%k!=0通过前面我们知道他们的状态和i-i/k-1有关。故dp[i]=dp[i-i/k-1]+1,num[i]=num[i-i/k-1]。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3001000;
int dp[maxn],num[maxn],add[maxn],ans[maxn];
int n,k,q;
void init()
{
int temp = n;
int acl = 0;
add[0] = 0; //add[i]前i轮kill几个;
while (temp){
acl++;
add[acl] = add[acl-1] + (temp-1)/k+1;
temp -= (temp-1)/k+1;
}
for (int i = 0; i < n; i++){
if (i%k == 0){
dp[i] = 0;
num[i] = i/k+1;
}else {
dp[i] = dp[i-i/k-1] + 1;
num[i] = num[i-i/k-1];
}
}
for (int i = 0; i < n; i++){
ans[add[dp[i]]+num[i]] = i;
}
}
int main()
{
int t;
scanf("%d",&t);
while (t--){
scanf("%d %d %d",&n,&k,&q);
init();
int cha;
while (q--){
scanf("%d",&cha);
printf("%d\n",ans[cha]+1);
}
}
return 0;
}
Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 题目大意:给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀.然后 ...
用线段树可以算出序列.然后o(1)询问. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<c ...
HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...
题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java ...
昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...
主成分分析的主要目的是希望用较少的变量去解释原来资料中的大部分变异,将我们手中许多相关性很高的变量转化成彼此相互独立或不相关的变量,从而达到降维的目的.在原始数据“预处理”阶段通常要先对它们采用PCA ...
刚好前些天有人提到eval()与exec()这两个函数,所以就翻了下Python的文档.这里就来简单说一下这两个函数以及与它们相关的几个函数,如globals().locals()和compile() ...
CDN行业从出现至今,已经有近20年的历史.但是直到近些年互联网进入超高速发展阶段,CDN才得以得到更广泛的应用和发展.最开始,CDN的主要任务只是简单的内容分发,对于静态内容的加速没有问题.但是随着 ...
从laravel入口文件index里面外面不难定位到Illuminate\Foundation\Http\Kernel的handle方法,同样也不难发现handle方法的核心是 $response = ...
一.包含嵌入式SQL 程序的处理过程 由预处理程序对源程序进行扫描,识别出ESQL语句 把它们转换成主语言的函数调用语句,使主语言编译程序能够识别 最后由主语言的编译程序将整个源程序编译成目标码 ...
实验目的:基于给定的 OrderDB 数据库, 练习并熟练掌握交互式 SQL 语言实验环境:sql sever 2008 附:OrderDB 表结构及表间的关系 /* 1 查询职工工资按高低排序的前2 ...
Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...
random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...
流与文件的操作在编程中经常遇到,与C语言只有单一类型File*即可工作良好不同,Java拥有一个包含各种流类型的流家族,其数量超过60个!当然我们没必要去记住这60多个类或接口以及它们的层次结构,理解 ...
摘抄与:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 概述 ...