Hoj: 1017 Joseph's Problem II

Source: Internet
Author: User

This is another form of Joseph's transformation. There are 2 * k people in total. First, K are good people, and then K are bad people. They want to kill bad people for the first K times, that is, before killing the first good person, all the bad guys should be killed. Therefore, we need to find the minimum m value that meets this condition;

According to Joseph's recursive model, we can find that our recursion is from the last person to the original number of people, so we can reverse the order, which is equivalent to killing K bad guys at last, then kill the good guys. In this way, the start position of the Prophet (the first person to kill) is used in recursion, so that the iteration can be performed. Whether the good guys kill the bad guys is used to determine whether the M is suitable, if K times later, kill K bad guys, It means this m is suitable.

Reference: http://www.cnblogs.com/wuzhibin/archive/2012/02/17/2356532.html

# Include <cstdio> # include <iostream> # include <stdlib. h> # include <memory. h> # include <math. h> # include <string. h> using namespace STD; int A [19]; int main () {int K, M, ans; A [10] = 93313; A [11] = 459901; A [12] = 1358657; A [13] = 2504881; A [14] = 13482720; while (CIN> K) {If (k = 0) break; else if (k> = 10) cout <A [k] <Endl; else {for (M = 1; m ++) {for (ANS = 0; ans <K; ans ++) {Int J = ans; For (INT I = k + 1; I <= 2 * k; I ++) j = (J + M) % I; If (j> = k) break;} If (ANS = k) {cout <m <Endl; break ;}}} return 0 ;}

Because there is a time limit on OJ, it is better to calculate all the values greater than 10... This...

 

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.