Children's games (the last remaining number in the circle)--Joseph question

Source: Internet
Author: User

Every year children's Day, Nowcoder will prepare some small gifts to visit the orphanage children, this year is also the case. HF as the senior veteran of the Nowcoder, naturally also prepared some small games. Among them, there is a game like this: first, let the children surround a big circle. Then, he randomly assigned a number m, so that the number of children numbered 0 began to count. Every time the child shouted to M to sing a song, and then can be in the Gift box arbitrary selection of gifts, and no longer back to the circle, from his next child, continue to 0...m-1 count off .... Go on like this .... Until the last child left, can not perform, and get Nowcoder valuable "Detective Conan" Collector's Edition (limited number of seats Oh!!) ^_^). Please try to think, which one of the children will get the present?


There is one thing in common with either a linked list or an array implementation: to simulate the entire game process, not only is the program more annoying, but the time complexity is as high as O (nm), and when the n,m is very large (for example, millions, tens of thousands), there is almost no way to produce results in a short period of time. We noticed that the original problem was merely to ask for the serial number of the last winner, rather than to mock the whole process. Therefore, if we want to pursue efficiency, we must break the routine and implement a mathematical strategy.

In order to discuss the convenience, the problem is changed slightly, does not affect the original intention:
Problem description: N Person (number 0~ (n-1)), starting from 0 count, reporting (M-1) exit, the remainder continues to count off from 0. The winner's number.

We know that the first person (the number must be m%n-1) after the dequeue, the rest of the n-1 individuals formed a new Joseph Ring (starting with the person numbered k=m%n):
K k+1 k+2 ... n-2, n-1, 0, 1, 2, ... k-2 and reported 0 from K.
Now let's do a conversion of their numbers:

After the transformation has completely become the (n-1) personal count of the sub-problem, if we know the solution of this sub-problem: for example, X is the final winner, then according to the above table to turn this x back is not exactly the solution of n personal situation?! The formula to change back is very simple, I believe everyone can push out: X ' = (x+k)%n

How to Know (n-1) The solution of the problem of personal count off? Yes, as long as you Know (n-2) the individual's solution. (n-2) A personal solution? Of course, the first thing to ask (n-3)----This is obviously a backward problem! Okay, here's the idea, here's the recursive formula:

Make f[i] means I personally play the game reported M exit the last winner's number, the final result is naturally f[n]

Recursive formulas
f[i]= (f[i-1]+m)%i; (i>1)

With this formula, all we have to do is calculate the value of F[i] from the 1-n order, and the final result is f[n]. Because real life numbers always start from 1, we output f[n]+1
Because it is a step-by-step recursive and does not need to save each f[i], the program is also exceptionally simple:
1 classSolution {2  Public:3     intLastremaining_solution (unsignedintN, unsignedintm)4     {5         if(n==0|| m==0)return-1;6         intR=0;7          for(intI=2; i<=n;i++){8R= (r+m)%i;9         }Ten         returnR; One     } A};

Children's games (the last remaining number in the circle)--Joseph question

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: 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.