本帖最后由 阴阳神万物主 于 2020-1-1 14:02 编辑
我理解错的不是字典序,而是:你原来不是要把那些数字先按顺序组成一个数字,再看后来这个数字的排号,是直接看这个排列的排号。
我一开始理解的:
结果你想要的:
既然如此就这么整:yh = [[1]]
def solve(n:'可能包含重复数字的排列')->'编号':
def a(n,m):#m<=n
res = 1
for i in range(n,n-m,-1):
res *= i
return res
def c(n,m):#m<=n
res = 1
for i in range(n,m,-1):
res *= i
for i in range(n-m,1,-1):
res //= i
return res
def cy(n,m):
global yh
while len(yh) <= n:
ad = [1]+[yh[-1][x]+(yh[-1][x+1] if x+1 < len(yh[-1]) else 0)for x in range(len(yh[-1]))]
yh.append(ad)
return yh[n][m]
d = n#[str(x) for x in n]
b = dict([(each,d.count(each)) for each in set(d)])
res = 1
le = len(d)
for i in range(le):
dlt = 0
le -= 1
for j in b:
if(b[j] and j<d[i]):
each = 0
b[j] -= 1
an = 0
for n in b:
if b[n] > 1:
each = each*c(le,b[n]) if each else c(le,b[n])
le -= b[n]
dlt -= b[n]
elif b[n]:
an += 1
each = each*a(an,an) if each else a(an,an)
le -= dlt
dlt = 0
res += each
b[j] += 1
b[d[i]] -= 1
return res
if __name__ == '__main__':
'''
print('示例1 输出:',solve([1,4,2,2]))
print('示例2 输出:',solve([1,6,5,3,1]))
print('自测 6 输出:',solve([1,2,2,1,1]))
print('自测 30 输出:',solve([3,2,2,1,1]))'''
#print('自测大数据,全排列数为c(2000,1000) 输出:',solve([1,2]*1000))
|