Created
February 28, 2012 15:26
-
-
Save majiang/1933126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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