有人可以解释为什么此代码适用于
pos= 64684571
或
pos= 64684573
但不是
pos= 64684572
import numba
import numpy as np
def choose(n, k):
if n < k:
return 0
if n == k:
return 1
delta = imax = 0
if k < n-k:
delta = n-k
imax = k
else:
delta = k
imax = n-k
ans = delta + 1
temp = imax+1
for i in range(2, temp):
ans = (ans * (delta + i)) // i
return ans
def largestV(a, b, x):
v = numba.int64(a-1)
while choose(v, b) > x:
v -= 1
return v
def calculateMth(n, k, pos):
m = pos
a = n
b = k
x = (choose(a, b) - 1) - m
result = np.zeros(k)
for i in range(k):
result[i] = int(largestV(a, b, x))
x = x - choose(result[i], b)
a = result[i]
b -= 1
for i in range(k):
result[i] = (n-1) - result[i]
return result
if __name__ == "__main__":
n = 200
k = 4
pos= 64684572
result = calculateMth(n,k,pos)
print (result)