Skip to content

Instantly share code, notes, and snippets.

@lucemia
Created July 10, 2015 20:14
Show Gist options
  • Save lucemia/49d836deded9d29c6934 to your computer and use it in GitHub Desktop.
Save lucemia/49d836deded9d29c6934 to your computer and use it in GitHub Desktop.
euler 502
store = {}
def g(w, h, odd=True):
if w <= 0 or h <= 0:
if odd: return 0
return 1
if not odd:
return (h+1)**w - g(w, h)
if (w, h) in store:
return store[(w, h)]
total = 0
total = g(w-1, h)
for i in range(w):
total += g(i+1, h-1, False) * g(w-i-2, h, False)
total += g(i+1, h-1, True) * g(w-i-2, h, True)
store[(w,h)] = total
return total
def F(w, h):
v = g(w, h-1) - g(w, h-2)
return v
assert F(4, 2) == 10
assert F(13, 10) == 3729050610636
assert F(10, 13) == 37959702514
assert F(100, 100) % 1000000007 == 841913936
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment