Lego Blocks

  • + 1 comment

    Python

    def legoBlocks(n, m):
        f = [1]
        g = [1]
        h = [1]
        for i in range(1, m + 1):
            sumhg = 0
            for j in range(1, i):
                sumhg += h[j] * g[i-j] % (10**9+7)
            f.append(sum(f[-4:]) % (10**9+7))    
            g.append(f[i] ** n % (10**9+7))
            h.append((g[i] - sumhg) % (10**9+7))
        return h[-1] % (10**9+7)