Joseph Ring algorithm

Source: Internet
Author: User

First of all, the problem: n individuals (numbered 0-n-1) in a circle, starting from 1 to count, reporting m of the people out, and then from the people of the next person, starting from 1, starting off, report m of the people out, to find the last surviving the original number of the person.

It is not difficult to implement if you are simply simulating the entire game. What I learned today is another algorithm. For example, after the first round, assuming the elimination of the person number is k-1, Count is M, then the next n-1 should be such a sort of X ' (x), X ' indicates the number of the previous round, X represents the number of the round. So the current situation should be such that K (0), k+1 (1), k+2 (2) 、......、 k-2 (n-2), we can draw such a formula X ' = (x+k)%n because the last survivor his final number must be 0, then there is this launch his last round number ... Until the first number is released.

Joseph Ring algorithm

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.