Lego Blocks

  • + 0 comments

    My Python3 solution following @rdgpcampos explanation. Please see @rdgpcampos for their thorough and clear explanation. I Used p to keep track of Permutations , c for Correct Cases and tl for Total cases. Overall the solution is same as the one shown by @rdgpcampos. And the main Function that is Mandatory to finish in time is the pow() function.

    def legoBlocks(n, m):
        # Write your code here
        p=[0,1,2,4,8]+[-1 for x in range(m)]
        c=[0,1]+[-1 for x in range(m)]
        tl=[0,1]+[-1 for x in range(m+5)]
        for i in range(5,m+1):
            p[i]=p[i-1]+p[i-2]+p[i-3]+p[i-4]
            tl[i]=pow(p[i],n,1000000007)
        for i in range(2,5):
            tl[i]=pow(p[i],n,1000000007)
        for i in range(2,m+1):
            c[i]=tl[i]
            for j in range(i):
                c[i]-=c[j]*tl[i-j]
            c[i]=c[i]%(1000000007)
        return c[m]