Skip to content

Instantly share code, notes, and snippets.

@zhpmatrix
Created May 11, 2017 02:20
Show Gist options
  • Save zhpmatrix/c49f76b3d23a7c59ca35bdb8dcb37a3f to your computer and use it in GitHub Desktop.
Save zhpmatrix/c49f76b3d23a7c59ca35bdb8dcb37a3f to your computer and use it in GitHub Desktop.
how to decompose problems?
#################################################################
#desc: find the number of ways to divide series into diff sets
#author: zhpmatrix
#date: 2017-05-11
#################################################################
from itertools import combinations
def c(n,k):
return len([val for val in combinations(range(n),k)])
n_max = 1000
k_max = 1000
mem = [[-1] * k_max for i in xrange(n_max + 1)]
def group(n,k):
if k == 0:
return 1 if n == 0 else 0
#if mem[n][k] > 0:
# return mem[n][k]
res = 0
for i in xrange(1, n - (k-1) + 1):
res += group(n-i,k-1)*c(n,i)
#mem[n][k] = res
return res
if __name__ == '__main__':
print group(4,3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment