project-euler/493_under_the_rainbow.py

21 lines
551 B
Python
Raw Normal View History

#!/usr/bin/env python3
from fractions import Fraction
def pick(state, depth, numofc, cache={}):
if sum(state) == depth:
return len(state) - state.count(0)
if state in cache:
return cache[state]
r = 0
for c, num in enumerate(state):
nws = sorted(state[:c] + (num + 1,) + state[c+1:])
nws = tuple(nws)
r += Fraction(numofc-num, numofc*len(state) - sum(state)) * \
pick(nws, depth, numofc, cache)
cache[state] = r
return r
r = pick(7*(0,), 20, 10)
print(float(round(r, 9)))