"""v2_certify.py が書いた双対の証明書（v2_certs_K{K}.json）を、それだけを読んで確かめる（標準ライブラリだけ）。
各 n について: すべての a（n/K < a ≤ n）で ∑_{k ≤ n/a} y_{ka} ≥ 1/a、y ≥ 0、∑_m y_m < 1。
これが成り立つ n では、元がすべて n/K より大きい lcm-separated な集合は ∑1/a ≤ ∑ y < 1（弱双対）。
使い方: python3 check_certs.py v2_certs_K40.json
# no-test: 証明用の検算
"""
import sys, json
from fractions import Fraction as Fr

d = json.load(open(sys.argv[1]))
K = d["K"]
ok_n, bad = 0, []
for ns, cert in d["certificates"].items():
    n = int(ns)
    y = {int(m): Fr(v) for m, v in cert.items()}
    if any(v < 0 or m < 1 or m > n for m, v in y.items()):
        bad.append((n, "y の範囲")); continue
    tot = sum(y.values())
    if tot >= 1:
        continue      # この n は証明書では片付かない（全探索の側で扱う）
    good = all(sum(y.get(k * a, Fr(0)) for k in range(1, n // a + 1)) >= Fr(1, a)
               for a in range(1, n + 1) if K * a > n)
    if good: ok_n += 1
    else: bad.append((n, "条件"))
print(f"K={K}: 証明書で ∑y < 1 を確かめた n: {ok_n} 個、不合格: {bad}")
small = sorted(int(n) for n, c in d["certificates"].items() if sum(Fr(v) for v in c.values()) >= 1)
print(f"証明書の和が 1 以上の n（全探索で扱う）: {small}")
