# 将序列划分成m份
def generate_all_equal(S, m, k):
for i in range(k, len(S)):
L = []
if m == S[i]:
yield (S[i], )
elif m > S[i]:
L.append(S[i])
for j in generate_all_equal(S, m - S[i], i + 1):
if sum(L + list(j)) == m:
yield tuple(L) + j
def Perm(A, k, n):
if n == 0:
yield []
for i in range(k, len(A)):
L = [A[i]]
for j in Perm(A, i + 1, n - 1):
yield L + j
def divide_s(S, m):
if sum(S) % m != 0:
return []
else:
d_sum = sum(S) / m
S_sort = list(sorted(S))
List = list(set((generate_all_equal(S, d_sum, 0))))
if len(List) < m:
return []
List = [tuple(j) for j in set([tuple(list(sorted(list(i)))) for i in List])]
L = list(Perm(List, 0, m))
all_set = []
for i in L:
T = []
for j in i:
T.extend(j)
if list(sorted(T)) == S_sort:
if list(sorted(i)) not in all_set:
all_set.append(list(sorted(i)))
if all_set == []:
return []
else:
return all_set
def main():
S = [6, 1, 3, 7, 4, 4, 5, 4, 1, 1]
print("S 3 :")
print(divide_s(S, 5))
for i in divide_s(S, 3):
print(i)
print("S 4 :")
for i in divide_s(S, 4):
print(i)
print("S 5 :")
for i in divide_s(S, 5):
print(i)
if __name__ == "__main__":
main()