#!/usr/bin/env python3 from collections import Counter p=996979 I=[(22716,22810),(397695,397787),(731292,731385)] W=[(45432,45620),(420411,420597),(754008,754195),(795390,795574),(132008,132193),(465605,465791)] def ov(t,lr,LR): a=(lr[0]+t)%p;b=(lr[1]+t)%p;L,R=LR if a<=b: return max(0,min(b,R)-max(a,L)+1) return max(0,R-a+1)+max(0,b-L+1) cand=set() for lr in I: for LR in W: lo=(LR[0]-lr[1])%p w=(LR[1]-LR[0])+(lr[1]-lr[0])+1 for k in range(w): cand.add((lo+k)%p) vals=[] for t in cand: w=sum(ov(t,lr,LR) for lr in I for LR in W) vals.append((w,t)) vals.sort(reverse=True) print('cand',len(cand),'maxw',vals[0][0]) for K in [80,90,95,97,100,110,120,130,140,150,160,180,200]: print(K,sum(1 for w,t in vals if w>=K)) print(vals[:30])