Skip to content

Instantly share code, notes, and snippets.

@majiang
Created February 28, 2012 15:26
Show Gist options
  • Save majiang/1933126 to your computer and use it in GitHub Desktop.
Save majiang/1933126 to your computer and use it in GitHub Desktop.
class distinct_ball_memo(dict):
def __missing__(self, key):
m, n = key
if n == 0:
return 1
self[key] = m * self[(m, n-1)] + self[(m+1, n-1)]
return self[key]
def distinct_ball(n):
return distinct_ball_memo()[(0, n)]
def test_distinct_ball():
for i in range(1, 21):
print i, distinct_ball(i)
if __name__ == '__main__':
test_distinct_ball()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment