0, 1, …, n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字。求出这个圆圈里剩下的最后一个数字。
def LastRemaining(n, m):
if n < 1 or m < 1:
return -1
result = 0
for _ in range(2, n + 1):
result = (result + m) % n
return result
if __name__ == '__main__':
assert LastRemaining(0, 5) == -1
assert LastRemaining(5, 0) == -1
print(LastRemaining(6, 2))
print(LastRemaining(6, 7))
print(LastRemaining(4000, 997))