Advertisement
zwliew

dp.py

Apr 9th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.27 KB | None | 0 0
  1. ;compile python
  2. ```
  3. import functools
  4. @functools.lru_cache(None)
  5. def solve(n):
  6.   if n <= 1:
  7.     return 0
  8.   if n == 2:
  9.     return (1/3) ** 2
  10.   return (1/3) * ((1/3) + (2/3) * solve(n - 2)) + (2/3) * solve(n - 1)
  11.  
  12. for i in range(11):
  13.   print(f"{i}: {solve(i)}")
  14. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement