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.
大意就是:給出k個好孩子和k個壞孩子,好孩子在前,壞孩子在后。然后圍成一圈,從第一個開始報數,報到m的退出,求最小的的m滿足前k個出局的孩子都是壞孩子。
對于約瑟夫環問題大家的第一影響應該是模擬,數組和鏈表都行,但是這里如果用不加優化的模擬的話,超時是不可避免的,對于k=13,m是200多萬,一個一個枚舉,會死人的。。。不過用數組模擬時有個優化,就是m對剩下的人數先取模,也就是說剩下的人數的整數倍對最后的結果不影響。這里可以剪掉很多。還有就是得先把這13個結果算出來,不然還是會超時,因為case居多。。。
下面給出官方的代碼(建議先自己想)
官方