We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
constmod=BigInt(10**9+7);constbOne=BigInt(1);functionpower(base,exp){letresult=bOne;while(exp>0){if(exp&bOne)//odd numberresult=(result*base)%mod;exp=exp>>bOne;//divide by 2base=(base*base)%mod;}returnresult;}functionlegoBlocks(n,m){n=BigInt(n);constrowDP=[0,bOne,BigInt(2),BigInt(4),BigInt(8)];for(leti=5;i<=m;i++){rowDP.push(rowDP[i-1]+rowDP[i-2]+rowDP[i-3]+rowDP[i-4]);}consttotalBlocks=[0];for(leti=1;i<=m;i++){totalBlocks.push(power(rowDP[i],n));}conststableBlocks=[0,totalBlocks[1]];for(leti=2;i<=m;i++){letsb=totalBlocks[i];for(letj=1;j<i;j++){sb=(sb-(stableBlocks[j]*totalBlocks[i-j])%mod+mod)%mod;}stableBlocks.push(sb);}returnstableBlocks[m];}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lego Blocks
You are viewing a single comment's thread. Return to all comments →
js