2016暑假多校联合---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.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int N=;
int head[N];
int a[N];
struct Node
{
int to;
int next;
}node[N]; int main()
{
int T;
cin>>T;
while(T--)
{
int n,k,q;
scanf("%d%d%d",&n,&k,&q);
memset(head,,sizeof(head));
int tot=;
for(int i=;i<=n;i++)
{
if((i-)%k==) a[i]=;
else a[i]=a[i-(i-)/k-]+;
node[tot].to=i;
node[tot].next=head[a[i]];
head[a[i]]=tot++;
}
int cnt=n;
for(int i=tot-;i>=;i--)
{
for(int j=head[i];j;j=node[j].next)
{
a[cnt--]=node[j].to;
}
}
while(q--)
{
int x;
scanf("%d",&x);
printf("%d\n",a[x]);
}
}
return ;
}
2016暑假多校联合---Death Sequence(递推、前向星)的更多相关文章
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2016暑假多校联合---Another Meaning
2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...
- 2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...
- 2016暑假多校联合---Substring(后缀数组)
2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...
- 2016暑假多校联合---To My Girlfriend
2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...
- 2016暑假多校联合---A Simple Chess
2016暑假多校联合---A Simple Chess Problem Description There is a n×m board, a chess want to go to the po ...
- HDU 5860 Death Sequence(递推)
HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...
- hdu 5860 Death Sequence(递推+脑洞)
Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...
- 2016暑假多校联合---GCD
Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...
随机推荐
- JS图片懒加载
简介 当页面图片太多时,加载速度就会很慢.尤其是用2G/3G/4G访问页面,不仅页面慢,而且还会用掉很多流量.图片懒加载的原理就是将页面内所有需要加载的图片全部换成一张默认的图片(一般尺寸很小),只有 ...
- Android开发学习之路-使用Handler和Message更新UI
在Android中,在非主线程中更新UI控件是不安全的,app在运行时会直接Crash,所以当我们需要在非主线程中更新UI控件,那么就需要用到Handler和Message来实现 Demo中,使用到一 ...
- Atitit usrQBK13 html dsl 规范与解决方案
Atitit usrQBK13 html dsl 规范与解决方案 1.1. Vue vs anrular1 1.2. 定义html dsl变量1 1.3. 变量赋值1 1.4. 条件渲染指令1 2 ...
- DOM_02之查找及元素操作
1.查找之按节点间关系查找周围元素: 2.查找之HTML属性:①按id查找:var elem=document.getElementById("id"):找到一个元素,必须docu ...
- ui-router带参数的路由配置
ui-router带参数的路由配置 使用ng-route的时候带参数的连接这样配置: $routeProvider.when('item/itemid/:itemid', { templateUrl: ...
- JS的IE和Firefox兼容性
以下以 IE 代替 Internet Explorer,以 MF 代替 Mozzila Firefox 0.px的问题 obj.style.left = ”100px”; obj.style.top ...
- jsp实现简单的分页
效果如下:<%-- Document : page Created on : 2014-11-18, 8:55:02 Author : HJZ --%> <%@page conten ...
- VS2010项目缺少组件
遇到的问题是解决方案中部分项目无法加载, 提示需要缺少的web组件才能加载该项目,是否通过WEB安装组件来网络安装, 点击确定以后就什么也没有了. 网上查阅了一番,结合自己的使用情况(在家里用的是vs ...
- Elaticsearch REST API常用技巧
在Elasticsearch的REST API中,有很多使用技巧,这里针对官方文档给出的介绍,总结了几个常用的例子. 更多内容参考:Elastisearch文档总结 多索引 ES支持在一次请求中指定多 ...
- 【Swift学习】Swift编程之旅---Subscripts下标(十六)
类.结构体和枚举可以定义下标,他可以快速简单地访问集合(set,array,dict)的元素,你可以使使用下标来获取和设置集合元素. 你可以定义一个类型的多个下标,通过索引值类型的不同来进行重载,而且 ...