"""Candidate-specific Mahler and derivative bounds. Standard Python, integer arithmetic."""
from fractions import Fraction as F
from math import isqrt

def mahler_upper(coeff,rounds=3):
    if len(coeff)==2:return max(map(abs,coeff))
    # Constant-first Graeffe transform E(x)^2-x O(x)^2 squares every root.
    poly=list(reversed(coeff))
    for _ in range(rounds):
        new=[0]*len(poly)
        for parity in (0,1):
            group=poly[parity::2]
            for i,a in enumerate(group):
                for j,b in enumerate(group):new[i+j+parity]+=(1-2*parity)*a*b
        poly=new
    value=sum(x*x for x in poly)
    for _ in range(rounds+1):value=isqrt(value-1)+1
    return value

def bound(laws,coeff,cap):
    from verifier30 import envelope
    cap=F(cap);assert 0<=cap<=1
    k=4 if len(laws)==2 else 5;d=len(coeff)-1
    v=k*envelope(laws)**d*(1+cap)**(k-1)*2**((d-1)*k)*mahler_upper(coeff)**k
    return -(-v.numerator//v.denominator)
