2016暑假多校联合---Death Sequence(递推、前向星)

原题链接

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.

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

Author

BUPT

Source

2016 Multi-University Training Contest 10

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5867 5866 5865 5864 5863

题意:输入n表示1到n ,输入k,q   去掉一些数,从1开始 1、k+1、2*k+1、3*k+1、4*k+1、5*k+1........直到i*k+1>n结束   然后剩下的数又成一个1到(n)  的序列,重复操作,直至序列为空,q次询问,每次输入x,输出第x次去掉的数的下标;

思路:仔细推一下,发现存在递推关系,若第i个数满足(i-1)%k==0 则a[i]=1;否则a[i]=a[i-(i-1)/k-1]+1;  并且在循环中用前行星记录当前数是在第几层去掉的,然后再一重循环从层次最大处开始给a[i]赋值是第i次去掉的数在序列中的下标;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int N=3000005;
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,0,sizeof(head));
        int tot=1;
        for(int i=1;i<=n;i++)
        {
            if((i-1)%k==0) a[i]=1;
            else a[i]=a[i-(i-1)/k-1]+1;
            node[tot].to=i;
            node[tot].next=head[a[i]];
            head[a[i]]=tot++;
        }
        int cnt=n;
        for(int i=tot-1;i>=1;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 0;
}
时间: 2024-08-08 14:29:43

2016暑假多校联合---Death Sequence(递推、前向星)的相关文章

2016暑假多校联合---Rikka with Sequence (线段树)

2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has an array A with n numbers. Then he make

2016暑假多校联合---Another Meaning

2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”. Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to

2016暑假多校联合---Windows 10

2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can't j

2016暑假多校联合---Substring(后缀数组)

2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings. But ?? thinks that is too easy, he wants to make this problem more interesti

2016暑假多校联合---Joint Stacks (STL)

HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another wor

2016暑假多校联合---Counting Intersections

原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need to count the number of their intersection. The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments

HDU 5860 Death Sequence (递推)

题意:n个人排成一行,从第一个人开始,每个k个人报数,报到数的人被杀死,剩下的人重新排成一行再报数.一共q个询问,每次询问第qi个死的人是谁. 析:是一个约瑟夫的变形,我们要考虑子问题的问题同样编号是0-n-1,如果在某一轮,第 i 个人如果能取模 k 为0,那么这一轮他就会被干掉,如果不是 那么下一轮他就是编号为 i-i/k-1,然后再处理每一轮多少个人被干掉,就OK了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&q

2016暑假多校训练参赛感想

参赛感想 这是第一次参加暑假多校训练,应该也会是人生中最后一次,我真的很庆幸能参加这个训练,和全国几乎所有高校的ACMer一起在一个平台上做题!昨天为止多校已经完全结束,今天看到叉姐的训练感想(叉姐的感想链接),我觉得我也有必要写下自己的训练感想. 人的眼界总是狭窄的,当在自己的学校站在前几名的时候觉得自己还不错,应该会有不错的将来,但是当第一次参加国赛(2015 南阳站)的时候我便被别人实力所震撼,我突然觉得自己在别人的眼里简直就是小学生,菜到不行.别人在5个小时可以AK,而我连最水的题也要想

hdu-5496 Beauty of Sequence(递推)

题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 813    Accepted Submission(s): 379 Problem Description Sequence is beautiful and the beauty of an integer sequence is def