"""Exact limiting constants for proper multiplicative cubes.

The first method sums pyramid volumes by face size. The independent small-k
check interpolates the lattice-point counting polynomial of the projected
integer permutohedron. Its top coefficient is the coordinate volume.
"""
from fractions import Fraction
from functools import cache
from itertools import combinations_with_replacement
from math import comb, factorial, prod

@cache
def volume(k):
    if k == 1:
        return Fraction(1)
    total = 2**k-1
    result = Fraction()
    for j in range(1,k):
        gap = Fraction(j*total,k)-(2**j-1)
        facet = volume(j) * 2**(j*(k-j-1)) * volume(k-j)
        result += comb(k,j)*gap*facet
    return result/(k-1)

def lattice_count(k,t):
    """Count sorted coordinates and multiply by the size of the permutation orbit."""
    total = t*(2**k-1)
    result = 0
    for prefix in combinations_with_replacement(range(t,t*2**(k-1)+1),k-1):
        x = prefix+(total-sum(prefix),)
        if prefix and x[-1]<prefix[-1]:
            continue
        if any(sum(x[:j])<t*(2**j-1) for j in range(1,k)):
            continue
        result += factorial(k)//prod(factorial(x.count(v)) for v in set(x))
    return result

def check(k):
    n = k-1
    leading = sum((-1)**(n-i)*comb(n,i)*lattice_count(k,i) for i in range(n+1))
    assert volume(k) == Fraction(leading,factorial(n))

if __name__=='__main__':
    for k in range(1,8):
        m=2**k-1
        if k<=5:
            check(k)
        print(k, volume(k)/(factorial(k)*m**(k-1)))
