def isqrt(n): x = n y = (x + 1) // 2 while y < x: x = y y = (x + n // x) // 2 return x def a(n): if(n <= 3): return abs(n) root = isqrt(n) return min(a(root) + 1 + n - root**2, a(root+1) + 1 + (root+1)**2 - n) def all_operation_sequences(n): def helper(m): if(m <= 3): return ["i"*m,"d"*m] root = isqrt(m) lo = [x + "s" + "i"*(m - root**2) for x in helper(root)] hi = [x + "s" + "d"*((root+1)**2 - m) for x in helper(root+1)] if(len(lo[0]) == len(hi[0])): return lo + hi if(len(lo[0]) < len(hi[0])): return lo return hi if(n < 0): return ["d"*(-n)] if(n <= 3): return ["i"*n] return helper(n)