Poj 1012 Joseph-make sure the bad guys are cleared first

Source: Internet
Author: User

Q: The same Joseph problem, but now there are K good guys and K bad guys. Determine a step. Yes, all the bad guys are cleared before the first good guys are cleared.

Question:

1. The order in which the bad guys are cleared does not matter. Just know whether the next one is a good guy or a bad guy. 2. since there are always k good guys, each time a bad guy is removed, the number of bad guys is-1, so the total number of queues is-1 each time, but good guys are always the first K 3. the "relative" serial number of the next person to be cleared in the queue is S = (S + s-1) % rest4. as long as S> = K, the next person to be cleared is the bad guy.

5. next let's talk about the value range of M: Let's take a look at the remaining k + 1 people, that is, there is another unexecuted bad guy, so the end of this round must be in the last bad guy, so where is the start point? This requires the end position of K + 2 people. However, the end position of K + 2 people must be K + 2 people or k + 1 people, in this case, there are two sequence situations: GGGG ..... gggxb or GGGG ...... gggbx (X indicates the person who exits in the round with K + 2 people) therefore, the starting position of the round of K + 1 may be the first position or the position of K + 1. The limitation of M may be: T (k + 1) or T (k + 1) + 1; t> = 1; if every m traversal times out, to avoid timeout, you need to set a table and limit the range of M.

# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; bool Joseph PHUs (int K, int m) {// when processing, the default number is 0, 1, 2, · n-1; // each time a person is removed, the remaining person will be re-numbered; // because a person is removed from the previous round, if his number is T, then the number of all> T is reduced by 1; // The number of the next report should start from S + 1, however, due to the re-serial number, we start from s, that is, S, S + 1, S + 2 · S + (S-1) int n = K * 2, s = 0, rest; For (INT I = 1; I <= K; I ++) {rest = n-I + 1; S = (S + m-1) % rest; if (S <k) return false;} return true;} int num [15] = {0, 2, 7, 5, 30,169,441,187 2, 7632,174 0, 93313,459 901, 1358657,250 4881}; int main () {int K, I; memset (Num, 0, sizeof (Num); While (scanf ("% d", & K) & K) {If (Num [k]) {printf ("% d \ n", num [k]); Continue ;}for (I = k + 1 ;; I + = (k + 1) {If (Joseph PHUs (K, I) {num [k] = I; printf ("% d \ n", I ); goto end;} else if (Joseph PHUs (K, I + 1) {num [k] = I + 1; printf ("% d \ n", I + 1 ); goto end ;}}end :;} return 0 ;}

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.