hdu 5860 Death Sequence(2016 Multi-University Training Contest 10——递推)
来源:互联网 发布:一体打印机推荐 知乎 编辑:程序博客网 时间:2024/05/16 14:23
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860
Death Sequence
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 404 Accepted Submission(s): 179
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.
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.
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
17 2 71234567
Sample Output
1357264
Author
BUPT
Source
2016 Multi-University Training Contest 10
解题思路:这道题首先我们先将编号改成从0开始。这样如果i%k==0那么我们可以知道第i个数字第一轮就被杀死。如果i%k!=0,那么我们知道在这之前一轮杀死了i/k+1个人。这样我们就得到递推式。
详见代码。
//输出第几个被杀的是谁#include <iostream>#include <cstdio>#include <algorithm>using namespace std;#define N 3000000+10struct node{ int d;//表示哪一轮被杀的 int num;//表示当前轮第几个被杀的 int p;//表示本身是几号}s[N];bool cmp(node a,node b){ if (a.d==b.d) return a.num<b.num; else return a.d<b.d;}int main(){ int t,a; scanf("%d",&t); while (t--) { int n,k,q; scanf("%d%d%d",&n,&k,&q); s[0].num=1; for (int i=0;i<n;i++) { s[i].p=i+1; if (i%k==0) { s[i].d=1; if (i==0) continue; s[i].num=s[i-k].num+1; } else { s[i].d=s[i-(i/k+1)].d+1; s[i].num=s[i-(i/k+1)].num; } } sort(s,s+n,cmp); for (int i=0;i<q;i++) { scanf("%d",&a); printf ("%d\n",s[a-1].p); } } return 0;}
0 0
- hdu 5860 Death Sequence(2016 Multi-University Training Contest 10——递推)
- HDU 5860 Death Sequence (递推 + 约瑟夫环 + 思维)——2016 Multi-University Training Contest 10
- hdu 5783 Divide the Sequence(2016 Multi-University Training Contest 5——水题)
- HDU 5288 OO’s Sequence(数学)——2015 Multi-University Training Contest 1
- HDU 5793 A Boring Question(推公式+求逆元)——2016 Multi-University Training Contest 6 (1001)
- hdu 5802 Windows 10(2016 Multi-University Training Contest 6——贪心+dfs)
- hdu 5858 Hard problem(2016 Multi-University Training Contest 10——数学题)
- hdu 5861Road(2016 Multi-University Training Contest 10——线段树+扫描线)
- hdu 5867 Water problem(2016 Multi-University Training Contest 10——水题)
- 2016 Multi-University Training Contest 3 hdu 5753 Permutation Bo【打表+递推】
- HDU 6050 Funny Function(构造矩阵+推公式)——2017 Multi-University Training Contest
- HDU 5860 Death Sequence(递推)
- HDU 5860 Death Sequence(递推)
- hdu 5733 tetrahedron(2016 Multi-University Training Contest 1——几何公式题)
- hdu 5745 La Vie en rose(2016 Multi-University Training Contest 2——暴力)
- hdu 5752 Sqrt Bo(2016 Multi-University Training Contest 3——水题)
- hdu 5753 Permutation Bo(2016 Multi-University Training Contest 3——组合)
- HDU 5755 Gambler Bo(高斯消元裸题)——2016 Multi-University Training Contest 3
- #191 Maximum Product Subarray
- NodeJS 学习笔记
- python 调用win32pai 操作cmd
- 欢迎使用CSDN-markdown编辑器
- 巧用三角函数绘制canvas时钟
- hdu 5860 Death Sequence(2016 Multi-University Training Contest 10——递推)
- ASP.NET比较常用的26个性能优化技巧
- 新建工程模板-库函数
- serulet
- __weak and __block
- NO.2 做一个外挂
- 胡适致毕业生:在不健全的中国,如何不堕落?
- 配置opencv2.4.13 与VS2013
- 最长递增子序列--动态规划