Lego Blocks
Problem Statement :
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height n and width m. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example n = 2 m = 3 The height is 2 and the width is 3. Here are some configurations: image These are not all of the valid permutations. There are 9 valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo (10^9 + 7) Input Format The first line contains the number of test cases t. Each of the next t lines contains two space-separated integers n and m. Constraints 1 <= t <= 100 1 <= n,m <= 1000
Solution :
Solution in C :
In C++ :
#include <iostream>
using namespace std;
#define MOD 1000000007
typedef unsigned long long ull;
ull dp[1001]; // with rep
ull dpr1[1001]; // with rep
ull dpr2[1001]; // with rep
ull poww ( ull x, int n){
if(n==0) return 1;
ull t = poww(x, n/2);
t = t * t;
if(t>=MOD) t%= MOD;
if(n&1){
t = t * x;
if(t>=MOD) t%= MOD;
}
return t;
}
void pre(){
dp[1] = 1; dp[2] = 2; dp[3] = 4; dp[4] = 8;
for(int i=5;i<=1000;i++){
dp[i] = dp[i-1] + dp[i-2] + dp[i-3] + dp[i-4];
if(dp[i] >= MOD)
dp[i] %= MOD;
}
}
ull calc(int N, int M){
for(int i=1;i<=N;i++){
dpr1[i] = poww(dp[i], M);
}
for(int i=1;i<=N;i++){
dpr2[i] = dpr1[i];
for(int k=1;k<i;k++){
ull x = dpr2[k];
x = x * dpr1[i-k];
if(x >= MOD)
x %= MOD;
dpr2[i] -= x;
if(dpr2[i] >= MOD)
dpr2[i] += MOD;
}
}
return dpr2[N];
}
int main(){
pre();
int T;
int N,M;
cin >> T;
while(T--){
cin >> N >> M;
cout << calc(M,N) << endl;
//cout << dp[N][M] << endl;
}
}
In Java :
import java.util.*;
public class Solution {
int md=1000000007;
int[][] ways = new int[1001][1001];
int[][] waysRestrict = new int[1001][1001];
public Solution(){
for(int[] w : ways) Arrays.fill(w, -1);
for(int[] w : waysRestrict) Arrays.fill(w, -1);
}
public int solve(int n, int m)
{
if (ways[n][m] != -1) return ways[n][m];
long ans;
if (m==1) ans = 1;
else if (n==1){
if (m<=4) {
ans = 2*solve(1,m-1);
}
else {
ans = solve(1,m-1);
ans = (ans + solve(1,m-2))%md;
ans = (ans + solve(1,m-3))%md;
ans = (ans + solve(1,m-4))%md;
}
}
else{
ans = 1; int one = solve(1,m);
for (int i=0; i<n; i++) ans = (ans * one)%md;
}
ways[n][m] = (int)ans;
return ways[n][m];
}
public int solveRestrict(int n, int m)
{
if (waysRestrict[n][m] != -1) return waysRestrict[n][m];
long ans;
if (m==1) ans = 1;
// else if (n==1) ans = solve(n,m);
else {
ans = solve(n,m);
for (int i=1; i<m; i++) {
long a = solve(n,i);
a = (a*solveRestrict(n,m-i))%md;
ans -= a;
if (ans<0) ans+=md;
}
}
waysRestrict[n][m] = (int)ans;
return waysRestrict[n][m];
}
public static void main (String[] args) {
Solution o = new Solution();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i=0; i<n; i++) {
int a, b;
a = sc.nextInt(); b = sc.nextInt();
System.out.println(o.solveRestrict(a,b));
}
sc.close();
}
}
In C :
#include<stdio.h>
unsigned long long int call(int,int);
unsigned long long int f[1001];
unsigned long long int to[1001][1001];
unsigned long long int res[1001][1001];
int main()
{
int h,w,i,j,t;
unsigned long long int r=0;
f[1]=1;
f[2]=2;
f[3]=4;
f[4]=8;
for(i=1;i<=1000;i++)
{
for(j=1;j<=1000;j++)
{
res[i][j]=0;
}
}
for(i=5;i<=10000;i++)
{
f[i]=((f[i-4]+f[i-3])%1000000007+(f[i-2]+f[i-1])%1000000007)%1000000007;
}
for(j=1;j<=1000;j++)
{
to[1][j]=f[j];
}
for(i=2;i<=1000;i++)
{
for(j=1;j<=1000;j++)
{
to[i][j]=(to[i-1][j]*f[j])%1000000007;
}
}
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&h,&w);
//printf("f[%d] = %llu\n",w,f[w]);
r=call(h,w);
printf("%llu\n",r);
}
return 0;
}
unsigned long long int call(int h,int w)
{
int i=0;
unsigned long long int r=0;
if(h==1)
{
if(w>4)
return 0;
else
return 1;
}
else if(res[h][w]!=0)
{
return res[h][w];
}
else
{
for(i=1;i<=w-1;i++)
{
r=(r+(call(h,i)*to[h][w-i])%1000000007)%1000000007;
}
if(to[h][w]>r)
r=to[h][w]-r;
else
{
r=r-to[h][w];
r=1000000007-r;
}
res[h][w]=r;
return res[h][w];
}
}
In Python3 :
m = 1000000007
dp1 = [1,1,2,4]
for i in range(4,1001):
dp1.append((dp1[-1]+dp1[-2]+dp1[-3]+dp1[-4])%m)
def f(h,w):
dp2 = []
dp3 = [pow(dp1[i],h,m) for i in range(w+1)]
for i in range(w+1):
tmp = dp3[i]
for j in range(1,i):
tmp = (tmp - dp2[j]*dp3[i-j])%m
dp2.append(tmp)
return dp2[-1]
for e in range(int(input())):
print(f(*list(map(int,input().split()))))
View More Similar Problems
Reverse a doubly linked list
This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.
View Solution →Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →Tree: Height of a Binary Tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary
View Solution →Tree : Top View
Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.
View Solution →