"""Exact Taylor coefficient c_alpha of 1/e_k(1 - z), computed on the box [0, alpha] with Fractions."""
import itertools, sys
from fractions import Fraction
from math import comb

def coeff_box(n, k, alpha):
    N = comb(n, k)
    terms = [((-1) ** (j + 1) * comb(n - j, k - j), T) for j in range(1, k + 1) for T in itertools.combinations(range(n), j)]
    c = {}
    for a in sorted(itertools.product(*(range(x + 1) for x in alpha)), key=sum):
        s = Fraction(1 if not any(a) else 0)
        for w, T in terms:
            b = tuple(a[l] - (l in T) for l in range(n))
            if min(b) >= 0:
                s += w * c[b]
        c[a] = s / N
    return c

if __name__ == "__main__":
    n, k = int(sys.argv[1]), int(sys.argv[2])
    alpha = tuple(int(x) for x in sys.argv[3].split(","))
    c = coeff_box(n, k, alpha)
    v = c[alpha]
    print(f"n={n} k={k} alpha={alpha}: c = {v}  (~{float(v):.6e})")
    neg = [a for a, x in c.items() if x < 0]
    print("negative coefficients inside the box:", sorted(neg, key=lambda a: (sum(a), a))[:5], "count", len(neg))
