"""Sum-difference problem III (AlphaEvolve problem 44). A set U of non-negative integers with min U = 0 gives the lower bound C >= 1 + log(|U-U| / |U+U|) / log(2 max U + 1) (AlphaEvolve's compute_lower_bound). Upstream checked |U-U| <= 2 max U + 1, which always holds when U is inside [0, max U]; we check the hypotheses (integers, distinct, min 0) instead. """ import math import numpy as np from run import Invalid, int_list def check(inst, ans): u = ans.get("U") if not isinstance(u, list) or not (2 <= len(u) <= inst["max_size"]): raise Invalid(f"U must be a list of 2..{inst['max_size']} integers") int_list(u, None, 0, inst["max_value"], "U") if min(u) != 0: raise Invalid("U must contain 0 and be non-negative") if len(set(u)) != len(u): raise Invalid("U must consist of distinct integers") x = np.array(sorted(u), dtype=np.int64) m = int(x[-1]) # bitmap sweeps with a buffer of m+1 bytes: U-U is symmetric, so count non-negative differences; # U+U lies in [0, 2m], handled as two windows [0, m] and [m+1, 2m] (x sorted -> contiguous slices) buf = np.zeros(m + 1, dtype=bool) for i, v in enumerate(x): buf[v - x[:i + 1]] = True d = 2 * int(buf.sum()) - 1 s = 0 for lo in (0, m + 1): buf[:] = False for v in x: t = v + x a, b = np.searchsorted(t, lo), np.searchsorted(t, lo + m, side="right") buf[t[a:b] - lo] = True s += int(buf.sum()) del buf return 1.0 + math.log(d / s) / math.log(2 * m + 1), {"size": len(u), "max": m, "diffset": d, "sumset": s}