How many sub-sequences of unique elements can be possible?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 20:56:40

Try this:

import numpy as np
mod = 1000000007
n, k = map(int, input().split())
a = list(map(int, input().split()))
fre = [0]*10000
A = []
for i in range(0, n):
    fre[a[i]] += 1
for i in range(0, 9001):
    if fre[i] > 0:
        A.append(fre[i])   
kk = min( len( A ), k ) + 1
S = np.zeros( kk, dtype=int );   S[0] = 1
for a in A:
   S[1:kk] = (S[1:kk] + (a * S[0:kk-1])% mod) % mod
ans = 0
for s in S:
    ans = ((ans + s) % mod)
print(ans)

This program return all sub-sequences (only for count) that have unique elements.

try this:

import itertools 

def findsubsets(s, n): 
    return list(itertools.combinations(s, n))

my_list = [2, 3, 3, 7, 5]
list_len = 0

for i in range(1,len(my_list)):
    list_len += len(set(findsubsets(my_list, i)))

print(list_len)

output:

22

edit: remove permutations with same numbers from list:

import itertools 

def findsubsets(s, n): 
    return list(list(x) for x in itertools.combinations(s, n))

my_list = [2, 3, 7, 5, 3]
list_len = 0

for i in range(1,len(my_list)):
    list_len += len(set(tuple(sorted(i)) for i in findsubsets(my_list, i)))

print(list_len)

output:

22
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!