"""Erdős 542: A ⊆ {1..n}、どの2元も lcm > n（＝n 以下の各 m を割る A の元は高々1つ）。
max Σ_{a∈A} 1/a を整数計画で正確に求める。和 > 1 の集合が {2,3,5}・{3,4,5,7,11} 以外に出れば V2 の反例。
解は lcm 条件と和を Fraction で確かめ直す。
使い方: python exact_max.py n1 n2 [step]
# no-test: 探索用
"""
import sys, math
from fractions import Fraction as Fr
import numpy as np
from scipy.optimize import milp, LinearConstraint, Bounds
from scipy.sparse import lil_matrix


def solve(n):
    A = lil_matrix((n, n))
    for a in range(1, n + 1):
        for m in range(a, n + 1, a):
            A[m - 1, a - 1] = 1
    c = -np.array([1 / a for a in range(1, n + 1)])
    if "--no1" in sys.argv:
        c[0] = 1.0  # --no1: 1 を選ぶと損になるので使われない
    res = milp(c, constraints=LinearConstraint(A.tocsr(), -np.inf, 1), integrality=np.ones(n),
               bounds=Bounds(0, 1), options={"mip_rel_gap": 0, "time_limit": 600})
    S = [a for a in range(1, n + 1) if res.x is not None and res.x[a - 1] > 0.5]
    ok = all(math.lcm(x, y) > n for i, x in enumerate(S) for y in S[i + 1:])
    return S, sum(Fr(1, a) for a in S), ok, res.status


args = [x for x in sys.argv[1:] if not x.startswith("--")]
n1, n2 = int(args[0]), int(args[1])
st = int(args[2]) if len(args) > 2 else 1
for n in range(n1, n2 + 1, st):
    S, s, ok, status = solve(n)
    flag = "  <-- >1" if s > 1 else ""
    small = [a for a in S if a <= n // 4]
    print(f"n={n:5d} max={float(s):.6f} ok={ok} status={status} |A|={len(S)} small={small[:12]}{flag}", flush=True)
