Lego Blocks

  • + 0 comments

    in javascript:

    function legoBlocks(n, m) {
        // Write your code here
        const A = 1000000007n;
        const r = new Array(m+1).fill(0n);
        const a = new Array(m+1).fill(0n);
        
        a[0] = 1n;
        for (let j = 1; j <= m; j++) {
            a[j] += (j-1>=0) ? a[j-1] : 0n;
            a[j] += (j-2>=0) ? a[j-2] : 0n;
            a[j] += (j-3>=0) ? a[j-3] : 0n;
            a[j] += (j-4>=0) ? a[j-4] : 0n;
        }
        
        for (let j = 1; j <= m; j++) {
            const n1 = a[j] % A;
            const sum = n1 ** BigInt(n);
            a[j] = sum % A;
        }
        
        r[1] = 1n;
        for (let j = 2; j <= m; j++) {
            r[j] = a[j];
            for (let k = 1; k < j; k++) {
                const n1 = r[k] * a[j-k];
                r[j] -= n1;
            }
            // ATTENTION! in javascript, when the left operand of bigint is negative
            // need to add  +A to be positive, different from python for example
            r[j] = r[j] % A + A; 
        }
    
        return r[m] % A;
    }