Guest User

othercriteria

a guest
Apr 2nd, 2009
420
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×