[POJ1012] Joseph

Source: Internet
Author: User
Tags exit in

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 50596 Accepted: 19239
DescriptionThe Joseph ' s problem is notoriously known. For those who is familiar with the original Problem:from among n people, numbered 1, 2, ..., n, standing in circle Every mth is going to being executed and only the last remaining person would be saved. Joseph is smart enough to choose the position of the last remaining person, thus saving he life to give us the message a Bout the incident. For example if n = 6 and M = 5 then the people would be executed in the order 5, 4, 6, 2, 3 and 1 would be saved.

Suppose that there is k good guys and k bad guys. In the circle the first k is good guys and the last k bad guys. You had to determine such minimal m so all the bad guys would be executed before the first good guy.

Inputthe input file consists of separate lines containing K. The last line in the input file contains 0. You can suppose that 0 < K < 14.OutputThe output file would consist of separate lines containing m corresponding to K In the input file. Sample Input
340
Sample Output
530
Sourcecentral Europe 1995THINKING

The subject is Joseph Ring deformation first introduced the Joseph recursive formula, with n individuals (0,..., n-1), several m, then the first round out of the man f (i) = (f (i-1) +m-1)% (n-i+1), f (0) = 0;

F (i) indicates the person to exit in the current subsequence (the current sequence number is 0~ (n-i));

Take an example and say: k=4,m=30;

F (0) = 0;

F (1) = (f (0) +30-1)%8=5; Sequence (0,1,2,3,4,5,6,7) of 5

F (2) = (f (1) +30-1)%7=6; Sequence (0,1,2,3,4,6,7) of 7

F (3) = (f (2) +30-1)%6=5; Sequence (0,1,2,3,4,6) of 6

F (4) = (f (3) +30-1)%5=4; Sequence (0,1,2,3,4) of 4

  assuming the present remainder I (i<=n), obviously this round m to hang (since always starting from 1 number). After this round, the remainder is: 1 2 3 ... m-1 m + 1 ... i, we will map the number starting from m+1 to 1, then m+2 corresponds to 2, n corresponds to i-m, 1 corresponds into I-m + 1 m-1 corresponds to i-1, then the problem now becomes known i-1 individuals to cycle off m, to find out the number of people. assuming that the number of the last person to go out under the i-1 individual cycle is calculated X0, then it is in the N person ordinal x1= (x0+ m-1)% n + 1, the initial x0=1, iterative X0 and X1 can be calculated.

Let's talk about the value range of M: we look at the only k+1 personal situation, that is, the bad guys and one is not executed, then in this round the end of the position must be the last villain, then where is the starting position? This will need to find the k+2 personal end position, however the k+2 person's end position must be the k+2 person or the k+1 person, so there are two sequence situations:gggg ... GGGXB or gggg ... GGGBX (X means the person who has k+2 the exit of the individual) so there are two possible starting positions for the k+1 individual, namely the first position or the k+1 position, the limited m  There are two possible types:t (k+1) or t (k+1) +1; t>=1; if traversing each m must time out, avoiding timeouts requires a table and a limit of M range.

ConstJoseph:Array[0.. -] ofLongint= (0,2,7,5, -,169,441,1872,7632,1740,93313,459901,1358657,2504881,1245064);varX:longint;begin     whileTrue Do    beginreadln (x); ifx=0  ThenHalt;    Writeln (Joseph[x]); End;End.
View Code

[POJ1012] Joseph

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.