POJ 1012: Joseph
Joseph
Time Limit:1000 MS |
|
Memory Limit:10000 K |
Total Submissions:50068 |
|
Accepted:19020 |
Description
The Joseph's problem is notoriously known. for those who are not familiar with the original problem: from among n people, numbered 1, 2 ,..., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. for example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. in the circle the first k are good guys and the last k bad guys. you have to determine such minimal m that all the bad guys will be executed before the first good guy.
Input
The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 <k <14.
Output
The output file will consist of separate lines containing m corresponding to k in the input file.
Sample Input
340
Sample Output
530
At a very young age, Joseph's problem was that a group of people (n) formed a table, marked from 1 to n, and then came with a number m, the number of m users is eliminated every time. From the next person, the number of m users is counted, and the number of m users is eliminated.
There are n good people and n bad people. Good guys are marked from 1 to n, and bad guys are marked from n + 1 to 2 * n. Find a m for the question and eliminate all the bad guys.
The key to this question is not to tangle with the labels of the bad guys. No matter how many people are left, the labels of the good guys are always from 1 to n, and the labels of the bad guys are always behind. To eliminate a bad guy, you only need to reduce the remaining number by 1. The remaining bad guys should fill in the old ones and wear their labels. So for example
6 people: 1 2 3 4 5 6
M = 5
For the first time, we counted 5 places from 1 and eliminated 5 places. The remaining 1, 2, 3, 4, and 5 places (6 moved forward to one place and put on 5 clothes. In this way, good people would still mark 1 2, 3, bad guy number 4 5. 5 remaining persons)
For the second time, 5 digits are counted from 5, 4 digits are eliminated, and the remaining 1 2 3 4 digits are eliminated (good guys 1 2 3, bad guys 4)
In the third round, the game starts to count 5 places from 4, 4 is eliminated, and the remaining 1 2 3 is eliminated.
Why don't we get stuck with the bad guys? It's not easy to get a formula. If you don't care about the bad guys, the formula I get is
Kill_num = (kill_num + m-1) % rest
So I recorded a kill vector. As long as the number of discarded labels is greater than n or equal to 0, it meets the standards, I threw it in. When will the number of kill users be equal to n, it indicates that the m found is correct. Otherwise, m ++ is used to find another m.
(Find m) code:
# Include
# Include
# Include
# Include
# Include
Using namespace std; int people [50]; vector
Kill; int main () {int n, k = 0; while (cin> n) {int result = n + 1, rest = 2 * n, kill_num = 1; int n2 = 2 * n; memset (people, 0, sizeof (people); kill. clear (); while (1) {if (kill. size () = n) break; if (result + kill_num-1) % rest = 0) {kill_num = rest; rest --; kill. push_back (rest);} else if (result + kill_num-1) % rest <= n) {kill_num = 1; kill. clear (); rest = n2; result ++;} else {kill_num = (result + kill_num-1) % rest; rest --; kill. push_back (kill_num) ;}} cout <
Code for final table creation:
#include
using namespace std;int main(){int result[16];int n;result[1] = 2;result[2] = 7;result[3] = 5;result[4] = 30;result[5] = 169;result[6] = 441;result[7] = 1872;result[8] = 7632;result[9] = 1740;result[10] = 93313;result[11] = 459901;result[12] = 1358657;result[13] = 2504881;result[14] = 13482720;while(cin>>n && n){cout<