Hdu5643-king ' s Game

Source: Internet
Author: User

Bestcoder on the topic, directly posted on the site and the problem. It's a great question.

Problem description
In order to remember the history, the King prepares to play Joseph Games in the gap of the parade. It called N (1≤n≤5000) A soldier, the counter-clockwise into a circle, sequentially labeled 1,2,3...N.
First round the first person starting from 1, reporting 1 stop and report 1 of the person out.
The next person from the last round out of the first people began to count from 1, reporting 2 stopped and reported 2 of the person out.
The third round of people from the last round out of the next person began to count from 1, reporting 3 stopped and reported 3 of the person out.
N-1 the next person from the last round out of the person began to count off from 1, reporting n-1 stopped and report to n-1 the person out.
The last remaining person is a survivor, what is the number of this person?
Enter a description
The first line is an integer representing the number of test groups: T (0<t≤5000).
Each group contains a row of data, containing an integer n, which indicates that the n person is surrounded by a circle.
Output description
A total of T-lines. For each set of data, output the number of survivors.
Input sample
2
2
3
Output sample
2
2
Hint
For the first set of data, the person who starts reporting 1 is the person who marked 1 and the survivor is number 2nd.
For the second set of data, the person who starts reporting 1, or 1, exits. Then 2nd, 1, 3rd, report 2, 2 of the person is 3rd exit. The survivor is number 2nd.

Exercises

A variant of the

Joseph problem, however the topic is all bluffing, is a simple recursion. Although I know someone will play the table ...
Let's see how naked Joseph Played: n Individuals, deleted every k.
Since we only care about the last person to be deleted and do not care about the final process, we do not need or be able to simulate the whole process. We use recursion to solve. Assume that there are n individuals around the ring, labeled [0,n-1] starting from 0 The benefit is easy to take the mold, each number of K-person kill one case, the last surviving person's number is f[n].
We have f[1]=0, which doesn't need to be explained.
then consider the general situation F[n], the first person to kill the number is k-1, after killing only n-1 individuals, then we re-numbering!
The original number k is now number No. 0, which is the difference between the numbers 3 we just need to know now n-1 personal situation finally is who survived also knew N person situation is who survives. Fortunately f[n-1] has been calculated that the f[n] is on the basis of f[n-1] and add a K can not forget always to take the mold.

So the recursive formula is: f[i]={0 i=1 (f[i-1] + k) MoD I Other
This problem is only used in the original Joseph question to add one dimension, because in turn 1,2,3...n-1 personal deletion, so with f[i][j] to represent I individuals, in turn j,j+1...j+i-1 individual survivor label.
According to the re-marking method, the first j-1 out, starting from J new round, starting from the j+1, the remaining i-1 individuals, there are recursive formulas:
f[i][j]={0 I=1 (f[i-1][j+1] + j) MoD I Other
The answer is f[n][1]+1 (transfer the label to [1,n]) and the problem is easily resolved.
Complexity: Preprocessing O (n^2), Query O (1), Total complexity O (n^2). Because the array can be scrolled and the constants are too small, N gives 5000.

Code:

#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int N = 5010;int f[2][ N]; F[I][J] means I individuals j,j+1...j+i−1 individual deletion of the Survivor label//f[i][j]= (F[I-1][J+1]+J)%iint Ans[n];int Main () {    f[0][0] = 0;    for (int i = 0; i < ++i)        f[1][i] = 0;   For 1 people, the survivor is always 0 for    (int i = 1; i <=; ++i) {for        (int j = 1; J <=; ++j) {            f[i%2][j] = (f[(i- 1)%2][j + 1] + j)% i;        }        Ans[i] = f[i%2][1];    }    int t;    scanf ("%d", &t);    while (t--) {        int n;        scanf ("%d", &n);        printf ("%d\n", Ans[n] + 1);    }    return 0;}

Hdu5643-king ' s Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.