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