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