Survivors game

There are n students circle, which is an order id ~ n (No. 1 n next).

Now the No. 1 man gettin number, m the first round report on out, the second round began to count off from the next person out, the report m2 of students out.

And so on, until the last round of check-out person mn-1, the last remaining students.

The output of this number of students.

Input format
common line, comprising two integers n and m.

Output format
output last remaining number of students.

Data range
n≤15, m≤5
Input Sample:
52
sample output:
5'

inn=list(map(int,input().split()))
n,m=inn[0],inn[1]
N=16
def helper(n,m):
    st=[0]*N#初始状态
    p=1#当前一号位置
    r=n#当前这一圈剩余的人
    i=1
    #迭代n
    while i<=n:
    	#当前报到m的i次方,应该被删掉
        k=1#用k表示m的i次方,踢掉的人
        for j in range(1,i+1):
            k=k*m%r#超过一圈之后,求余数
        #当余数为零的时候,表示剩余最后一个人
        if k==0:
            k=r
        #从下一个位置开始,把第k个没有被删掉的人删掉
        while True:
            if not st[p]:
                k-=1
                if not k:
                    st[p] = 1
                     # print(p)
                    break
            p+=1
            if p > n:
                p = 1
            # print(p)
        i +=1
        r -=1   
    return p

print(helper(n,m))


Guess you like

Origin blog.csdn.net/lgy54321/article/details/94455460