SHARE
TWEET

othercriteria

a guest Apr 2nd, 2009 336 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4.  
  5. _usage = 'Usage: %s num_cookies num_people' % sys.argv[0]
  6.  
  7. try:
  8.     n_cookies = int(sys.argv[1])
  9.     n_people = int(sys.argv[2])
  10. except:
  11.     print _usage
  12.     sys.exit()
  13.  
  14. print 'Trying to distribute identical %d cookies to %d distinct people' % \
  15.     (n_cookies, n_people)
  16.  
  17. memo = {}
  18.  
  19. for cookies in range(0, n_cookies+1): memo[(cookies, 1)] = 1
  20.  
  21. for people in range(2, n_people+1):
  22.     for cookies in range(0, n_cookies+1):
  23.         memo[(cookies, people)] = sum([memo[(leftover, people - 1)]
  24.                                        for leftover in range(0, cookies+1)])
  25.  
  26. print '%d different ways!' % memo[(n_cookies, n_people)]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top