• + 0 comments

    really don't know, why this is rated hard or is in number theory - boils downs to simple dp.

    import heapq as hq
    from collections import defaultdict
    import bisect as bs
    
    DMAX = 18
    
    work = [(1,i) for i in range(10)]
    # strange = defaultdict(set)
    strange = set()
    hq.heapify(work)
    
    while work:
        d,n = hq.heappop(work)
        if d>DMAX: break
        # strange[d].add(n)
        strange.add(n)
        for k in range(max(2,d),DMAX+1): # exclude d=1 and n=0
            if len(str(n*k)) == k:
                hq.heappush(work,(k,n*k))
    
    strange = sorted(strange)
    
    def solve(a,b):
        return bs.bisect_right(strange,b) - bs.bisect_left(strange,a)