project-euler/191_prize_strings.py

27 lines
725 B
Python

#!/usr/bin/env python3
from math import factorial
def bin(n, k):
return factorial(n) // (factorial(n-k) * factorial(k))
def late(n):
return 3**n - 2**n - 2**(n-1) * n
def absent2(n):
if n < 3:
return 0
middle = (n-3) * 2**(n-4) - sum(2**(n-4-i) * absent(i) for i in range(n-3))
left = sum(2**(n-4-i) * (i * 2**(i-1) - absent2(i)) for i in range(n-3))
right = (n-3) * 2**(n-4) + sum((n-3-i) * 2**(n-4-i) * (2**(i-1) - absent(i-1)) for i in range(1, n-3))
return right + left + middle
def absent(n):
if n < 3:
return 0
return 2**(n-3) + (n-3) * 2**(n-4) - sum(2**(n-4-i) * absent(i) for i in range(n-3))
n = 30
print(int(3**n - late(n) - absent(n) - absent2(n)))