
English: 
hey guys
this is ritvik chatterjee i just
graduated from school this year and will
be studying computer science in
university in about a month
welcome to our channel learn competitive
programming with codechef
if you're interested in competitive
programming and want to learn and master
data structures and algorithms
this is a one-stop destination for you
every week we'll be posting codechef
problem explanations
conceptual videos on various programming
paradigms and also conduct live problem
solving sessions
before we actually get started here's a
reminder to hit the subscribe button if
you haven't yet already
great now that you've subscribed let's
get started
this is a video editorial for the second
problem from INOI 2020
3xN tiling the problem code
for this
is INOI2002 in this problem we're given an

English: 
hey guys
this is ritvik chatterjee i just
graduated from school this year and will
be studying computer science in
university in about a month
welcome to our channel learn competitive
programming with codechef
if you're interested in competitive
programming and want to learn and master
data structures and algorithms
this is a one-stop destination for you
every week we'll be posting codechef
problem explanations
conceptual videos on various programming
paradigms and also conduct live problem
solving sessions
before we actually get started here's a
reminder to hit the subscribe button if
you haven't yet already
great now that you've subscribed let's
get started
this is a video editorial for the second
problem from INOI 2020
3xN tiling the problem code
for this
is INOI2002 in this problem we're given an

English: 
n times
m grid where n is less than equal to 3
and m is less than equal to 10 to the
power of 6.
we're also given an infinite number of
three types of tiles
1x3 tiles 2x3
tiles
and 3x1 tiles we're meant to
count the number of ways to tile the n
times m
grid such that there are no overlapping
tiles the entire grid gets styled
and all tiles remain within the grid
let's try and figure out what happens if
n is equal to one
if we try placing a 3x1 tile
it won't fit
as it has three rows and our grid has
just one row
similarly if we try placing a 2x2 tile
it won't fit as it has two rows and our
grid has just one row
we can see that a 1x3 tile
fits in the grid perfectly
it occupies three consecutive columns in
the only row in the grid
given that we only fill grids of this
type with 1x3 tiles

English: 
n times
m grid where n is less than equal to 3
and m is less than equal to 10 to the
power of 6.
we're also given an infinite number of
three types of tiles
1x3 tiles 2x3
tiles
and 3x1 tiles we're meant to
count the number of ways to tile the n
times m
grid such that there are no overlapping
tiles the entire grid gets styled
and all tiles remain within the grid
let's try and figure out what happens if
n is equal to one
if we try placing a 3x1 tile
it won't fit
as it has three rows and our grid has
just one row
similarly if we try placing a 2x2 tile
it won't fit as it has two rows and our
grid has just one row
we can see that a 1x3 tile
fits in the grid perfectly
it occupies three consecutive columns in
the only row in the grid
given that we only fill grids of this
type with 1x3 tiles

English: 
and one such tile fills up three
consecutive columns we can only fill up
the grid in one way
if m is a multiple of three otherwise
there's all there'll always be at least
one
unoccupied column which we can't fill up
as the tiles can't overlap
for example out here m is equal to 7
which isn't a multiple of 3.
so we can't fill this grid as there's
one empty cell left
however if m is equal to 6 it could be
filled up completely
now let's take a look at what happens
when n is equal to 2.
if we try placing a 3x1 tile it
won't fit in the grid as the grid has
just 2 rows
while the tile has three rows a square
fits perfectly
leaving m minus two columns behind it if
it's placed at the very end
if we use a 1x3 tile to fill
the top right corner
we are forced to use a 1x3 tile
below it as well
we can fit in 2 1x3 tiles as well
leaving us with m minus 3 columns behind
it

English: 
and one such tile fills up three
consecutive columns we can only fill up
the grid in one way
if m is a multiple of three otherwise
there's all there'll always be at least
one
unoccupied column which we can't fill up
as the tiles can't overlap
for example out here m is equal to 7
which isn't a multiple of 3.
so we can't fill this grid as there's
one empty cell left
however if m is equal to 6 it could be
filled up completely
now let's take a look at what happens
when n is equal to 2.
if we try placing a 3x1 tile it
won't fit in the grid as the grid has
just 2 rows
while the tile has three rows a square
fits perfectly
leaving m minus two columns behind it if
it's placed at the very end
if we use a 1x3 tile to fill
the top right corner
we are forced to use a 1x3 tile
below it as well
we can fit in 2 1x3 tiles as well
leaving us with m minus 3 columns behind
it

English: 
if it's placed at the very end keeping
this in mind
let's try to come up with the recursive
dp solution
as placing a single tile on this grid
leaves a part of the grid that's left to
be tiled
if we know the number of ways there are
to tile the remaining part
we can know how many ways there are to
tile the entire grid
therefore let dp[i] be the number of
ways to tile the grid till the ith
column
if we place a 2x2 square we can
see that
we have m minus 2 columns left to fill
hence dp[i]
depends on dp[i-2] as that's the
number of ways to tell the remaining
grid
if we place 2 1x3 tiles the number
of ways to tell the remaining grid
is dp[i-3]. hence we arrive at
dp[i]
is equal to dp[i-2] plus dp[i-3]
now let's look at a 3xm grid it's
a bit different this time as there are
ways to completely fill the ith column

English: 
if it's placed at the very end keeping
this in mind
let's try to come up with the recursive
dp solution
as placing a single tile on this grid
leaves a part of the grid that's left to
be tiled
if we know the number of ways there are
to tile the remaining part
we can know how many ways there are to
tile the entire grid
therefore let dp[i] be the number of
ways to tile the grid till the ith
column
if we place a 2x2 square we can
see that
we have m minus 2 columns left to fill
hence dp[i]
depends on dp[i-2] as that's the
number of ways to tell the remaining
grid
if we place 2 1x3 tiles the number
of ways to tell the remaining grid
is dp[i-3]. hence we arrive at
dp[i]
is equal to dp[i-2] plus dp[i-3]
now let's look at a 3xm grid it's
a bit different this time as there are
ways to completely fill the ith column

English: 
without completely filling the columns
before it while only considering the
tiles being used to fill the ith
column
let's look at an example let's place a 2x2
square at the bottom this forces us to
place a 1x3 tile above it
we arrive at a state where the corner
cell in a grid is unfilled
so we can fill it up hence we need to
know the number of ways to fill the rest
of the grid
this ends up being the number of ways to
fill the grid such that the last columns
corner cell isn't filled
while calculating the number of ways to
arrive at this state
if we place a 2x2 square at the
bottom we see that we arrive at a state
where the corner and the center cell in
the last column in a grid
is unfilled while only one corner is
filled
hence we need to know the total number
of ways to fill the grid such that only
one of the corners are filled
let dp[i][1] be similar to our
earlier dp definition
where it denotes the answer to the ith
column if the ith column is completely
filled let dp[i][2] be the number of

English: 
without completely filling the columns
before it while only considering the
tiles being used to fill the ith
column
let's look at an example let's place a 2x2
square at the bottom this forces us to
place a 1x3 tile above it
we arrive at a state where the corner
cell in a grid is unfilled
so we can fill it up hence we need to
know the number of ways to fill the rest
of the grid
this ends up being the number of ways to
fill the grid such that the last columns
corner cell isn't filled
while calculating the number of ways to
arrive at this state
if we place a 2x2 square at the
bottom we see that we arrive at a state
where the corner and the center cell in
the last column in a grid
is unfilled while only one corner is
filled
hence we need to know the total number
of ways to fill the grid such that only
one of the corners are filled
let dp[i][1] be similar to our
earlier dp definition
where it denotes the answer to the ith
column if the ith column is completely
filled let dp[i][2] be the number of

English: 
ways to fill a grid with
i minus 1 full columns and the ith
column having a missing corner
which means that the corner remains
unfilled let dp[i][3] be
the number of ways to fill a grid with i
minus one full columns
and the ith column having only one
corner filled with the other two cells
in the comma column remaining unfilled
let's figure out what dp[i][1] is
first
we can place a 3x1 cell
which completely covers the ith column
the number of ways to make this is dp[i-1][1]
if we use a 1x3 tile to fill
the top right corner
we can use two 1x3 tiles
below it as well so
we can place three 1x3 cells
too which completely covers all columns
from i minus two to i
hence the number of ways to make this is
dp[i-3][1]
alternatively we can place a 2x2
square below the 1x3
tile
hence we can place a combination of a
1x3 tile and a 2x2

English: 
ways to fill a grid with
i minus 1 full columns and the ith
column having a missing corner
which means that the corner remains
unfilled let dp[i][3] be
the number of ways to fill a grid with i
minus one full columns
and the ith column having only one
corner filled with the other two cells
in the comma column remaining unfilled
let's figure out what dp[i][1] is
first
we can place a 3x1 cell
which completely covers the ith column
the number of ways to make this is dp[i-1][1]
if we use a 1x3 tile to fill
the top right corner
we can use two 1x3 tiles
below it as well so
we can place three 1x3 cells
too which completely covers all columns
from i minus two to i
hence the number of ways to make this is
dp[i-3][1]
alternatively we can place a 2x2
square below the 1x3
tile
hence we can place a combination of a
1x3 tile and a 2x2

English: 
square tile
this combination only works if
everything before the i minus second
column is filled
and the i minus second column only has
one empty corner cell
this is equal to dp[i-2][2].
note that the square can either be
placed on the top or at the bottom
so there are two ways of placing this
hence we arrive at dp[i][1]
is equal to dp[i-1][1] plus
dp[i-3][1]
plus 2 times dp[i-2][2].
let's figure out what dp[i][2] is
we can place a 2x2 tile this
completely covers two rows in the ith
column and the i minus first column
while the ith column only has two rows
to cover the i minus first column has
three
hence we're going to end up with the
corner piece to fill in the i minus
first row
and now everything before the i minus
first row has to be filled as well
this is the same as dp[i-1][3]

English: 
square tile
this combination only works if
everything before the i minus second
column is filled
and the i minus second column only has
one empty corner cell
this is equal to dp[i-2][2].
note that the square can either be
placed on the top or at the bottom
so there are two ways of placing this
hence we arrive at dp[i][1]
is equal to dp[i-1][1] plus
dp[i-3][1]
plus 2 times dp[i-2][2].
let's figure out what dp[i][2] is
we can place a 2x2 tile this
completely covers two rows in the ith
column and the i minus first column
while the ith column only has two rows
to cover the i minus first column has
three
hence we're going to end up with the
corner piece to fill in the i minus
first row
and now everything before the i minus
first row has to be filled as well
this is the same as dp[i-1][3]

English: 
alternatively if we use two 1x3 tiles to fill the last column
we're forced to place another one times
three tile above
it so we can place three one times three
tiles
this fills up all available cells from
the i minus second to the ith columns
but the i minus third column only has a
one cell filled corner
hence we need to know the number of ways
to tile till the i minus fourth column
along with the center and a corner cell
in the i minus third column
this is equivalent to dp[i-3][2]
hence we arrive at dp[i][2] is equal
to dp[i-1][3]
plus dp[i-3][2]
let's look at dp[i][3]. the only way
to fill only
one cell in a column is using a one
times three tile
above which we can place either two one
times three tiles or a two times two
square tile
so we can fill it with a square and a
one times three tile

English: 
alternatively if we use two 1x3 tiles to fill the last column
we're forced to place another one times
three tile above
it so we can place three one times three
tiles
this fills up all available cells from
the i minus second to the ith columns
but the i minus third column only has a
one cell filled corner
hence we need to know the number of ways
to tile till the i minus fourth column
along with the center and a corner cell
in the i minus third column
this is equivalent to dp[i-3][2]
hence we arrive at dp[i][2] is equal
to dp[i-1][3]
plus dp[i-3][2]
let's look at dp[i][3]. the only way
to fill only
one cell in a column is using a one
times three tile
above which we can place either two one
times three tiles or a two times two
square tile
so we can fill it with a square and a
one times three tile

English: 
this fills up all available cells
completely from the i minus second to
the ith columns
the number of ways to tile the rest is
dp[i-3][1].
we can also place 3 1x3 tiles
this leaves the corner cell in the i
minus third column plus everything
before the i minus third column left to
tile
this is equivalent to dp[i-3][3]
hence we arrive at dp[i][3] is equal
to dp[i-3][1]
plus dp[i-3][3]
right so let's take a look at the code
now out here we have t test cases so we
just go through each test case
individually
and we clear our dp arrays every time
and as we know it's a dp problem
therefore we need to keep dp arrays
k is equal to number of rows and n is
equal to the number of columns
so if we have just one row as i
mentioned before if the number of
columns is a multiple of three

English: 
this fills up all available cells
completely from the i minus second to
the ith columns
the number of ways to tile the rest is
dp[i-3][1].
we can also place 3 1x3 tiles
this leaves the corner cell in the i
minus third column plus everything
before the i minus third column left to
tile
this is equivalent to dp[i-3][3]
hence we arrive at dp[i][3] is equal
to dp[i-3][1]
plus dp[i-3][3]
right so let's take a look at the code
now out here we have t test cases so we
just go through each test case
individually
and we clear our dp arrays every time
and as we know it's a dp problem
therefore we need to keep dp arrays
k is equal to number of rows and n is
equal to the number of columns
so if we have just one row as i
mentioned before if the number of
columns is a multiple of three

English: 
then there's only one way to tile it
using only
1x3 tiles otherwise there's
there are no way to tile there's no way
to tile it therefore the answer is zero
then if the number of rows is equal to
two
we have we arrived at this recurrence
solution
which is dp2[i] is equal to dp2[i-2]
plus dp2[i-3]
and our base cases in this case is dp2[2] is equal to 1 because we can use
just
1 2x2 squared and dp2[3] is
equal to 1
as we can use uh 2 1x3 tiles
so we go all the way up until n and our
final answer is dp2 of n
now we're going to when that we have
three rows
our our recurrence relations is just
what i
explained in the video but the base case
out here
is dp of zero of one is equal to one and

English: 
then there's only one way to tile it
using only
1x3 tiles otherwise there's
there are no way to tile there's no way
to tile it therefore the answer is zero
then if the number of rows is equal to
two
we have we arrived at this recurrence
solution
which is dp2[i] is equal to dp2[i-2]
plus dp2[i-3]
and our base cases in this case is dp2[2] is equal to 1 because we can use
just
1 2x2 squared and dp2[3] is
equal to 1
as we can use uh 2 1x3 tiles
so we go all the way up until n and our
final answer is dp2 of n
now we're going to when that we have
three rows
our our recurrence relations is just
what i
explained in the video but the base case
out here
is dp of zero of one is equal to one and

English: 
the reason behind this
is dp of zero is essentially everything
before the first column
and that's when we have absolutely
nothing so the number of
ways of tiling nothing is equal to just
one way using no tiles
therefore dp of zero of one is equal to
one
and then we go all the way from one up
to n
oops we go all the way from 1 up to n
and basically out here we
we if we check if i is greater than
equal to 2
to go back by minus 2 and i is greater
than equal
to 3 to go back by minus 3 to make sure
we don't accidentally end up
um accessing any negative indices
and yeah the recurrence relations are
exactly what i mentioned in the video
and every time we do a calculation we
mod it you as you might find that the
total number weighs more than 1
10 to the power of 9 plus 7 and then our
final answer is just dp of n
of 1 as that's the number of ways of
tiling it till the
end and its column such that everything

English: 
the reason behind this
is dp of zero is essentially everything
before the first column
and that's when we have absolutely
nothing so the number of
ways of tiling nothing is equal to just
one way using no tiles
therefore dp of zero of one is equal to
one
and then we go all the way from one up
to n
oops we go all the way from 1 up to n
and basically out here we
we if we check if i is greater than
equal to 2
to go back by minus 2 and i is greater
than equal
to 3 to go back by minus 3 to make sure
we don't accidentally end up
um accessing any negative indices
and yeah the recurrence relations are
exactly what i mentioned in the video
and every time we do a calculation we
mod it you as you might find that the
total number weighs more than 1
10 to the power of 9 plus 7 and then our
final answer is just dp of n
of 1 as that's the number of ways of
tiling it till the
end and its column such that everything

English: 
in the nth column is completely tile
all the rows in the nth column are
completely tiled as tiled as well
so yeah there's not much to it if you've
understood the recurrence relations it's
just implementing what i mentioned in
the video
that's it for this one like the video if
you enjoyed it
to stay notified of our upcoming uploads
subscribe to our channel and press the
bell
you can also stay updated via our
telegram group
find the link in the description below
in case of any feedback or doubts
leave it in the comments also feel free
to suggest what problems we should be
picking up next
i'll see you in the next one
one

English: 
in the nth column is completely tile
all the rows in the nth column are
completely tiled as tiled as well
so yeah there's not much to it if you've
understood the recurrence relations it's
just implementing what i mentioned in
the video
that's it for this one like the video if
you enjoyed it
to stay notified of our upcoming uploads
subscribe to our channel and press the
bell
you can also stay updated via our
telegram group
find the link in the description below
in case of any feedback or doubts
leave it in the comments also feel free
to suggest what problems we should be
picking up next
i'll see you in the next one
one
