Let the operator □ be defined for all positive integers the following way:
Evaluate 5 □ 5 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Awesome! Hands down, the neatest solution to the problem!
Nice! Clear and simple, and easy to understand.
Log in to reply
Thanks. I looked at your solution, and that's what you were saying. I think this way makes it clearer for others to understand how to approach the problem.
Read Kevin Chung's solution to understand how to solve this particular problem.
The general solution to the recurrence relation is a □ b = ( a a + b ) − ( a − 1 a + b − 2 ) , which we can prove by induction and checking all the 1 □ n , n □ 1 base cases. However, this would make the formula seem like magic.
This solution explains how pascal's triangle can be applied to solve this recurrence relation problem type and obtain the formula.
Consider the following operator ⊕ .
By setting it up in a triangle-form, we get the following:
0 + 1 = 1 0 + 1 = 1 0 + 1 = 1 1 + 2 = 3 1 1 + 1 = 2 1 + 0 = 1 2 + 1 = 3 1 + 0 = 1 1 + 0 = 1
Thus, we see that the 1 helps us to generate the Pascal's triangle, and we obtain that each term is of the form ( a a + b ) .
How do we adapt it to the scenario in the question? The trick is to find suitable places seed 1 that would lead us towards the desired result. It's not currently obvious what we could do, so let's analyze the given scenario further:
6 5 4 ? 3 ? 2 ? ? 1 ? ? 2 ? ? 3 ? 4 ? 5 6
The issue here is that the sides of the triangle are not 1, and so we cannot find the 1 to seed the pattern. However, notice that the sides are identical to the 2nd row of Pascal's triangle, so that suggests that we could backtrack to find our 1 . Let's work with the left side first.
1 1 1 4 1 3 1 2 ? 1 ? 2 ? 3 4
This looks great! We've found a potential seeding point. Now, let's observe what happens if we tried to do the same on the right.
1 1 1 4 1 3 1 2 ? 1 ? 1 2 ? 1 3 1 4 1 1
As you can see, if we tried to duplicate the same naively, we end up with the 1 having an incorrect value. It should have been 1 + 1 = 2 , but instead we have it as 1 (and that affects all the subsequent values). How can we fix this? Well, let's add a seed of − 1 at that spot!
With the following seed:
1 − 1 1
We will obtain:
0 + 1 = 1 0 + 1 = 1 0 + 1 = 1 1 + 3 = 4 0 + 1 = 1 1 + 2 = 3 1 1 + 1 = 2 3 + 4 = 7 1 + 1 + ( − 1 ) = 1 2 + 2 = 4 1 1 + 1 = 2 4 + 3 = 7 1 + 0 = 1 2 + 1 = 3 1 + 0 = 1 3 + 1 = 4 1 + 0 = 1 1 + 0 = 1
Let the
−
1
seed be in position
(
1
,
1
)
, since that is what the
□
operator desires.
Consider the contribution of these seeds to the entry in
(
a
,
b
)
.
The
1
seed in position
(
1
,
0
)
will contribute
1
×
(
(
a
−
1
)
(
a
−
1
)
+
b
)
.
The
1
seed in position
(
0
,
1
)
will contibute
1
×
(
(
b
−
1
)
a
+
(
b
−
1
)
)
.
The
−
1
seed in position
(
1
,
1
)
will contribute
−
1
×
(
(
a
−
1
)
(
a
−
1
)
+
(
b
−
1
)
)
.
Hence,
a
□
b
=
(
a
−
1
a
+
b
−
1
)
+
(
b
−
1
a
+
b
−
1
)
−
(
a
−
1
a
+
b
−
2
)
.
Note: We could have used a different seed of
0 1 − 1 0
which leads to the equivalent (but nicer looking) formula ( a a + b ) − ( a − 1 a + b − 2 ) .
Give this idea a try and see what you get. For example, how can we solve:
Now I've got some idea. Looking at it now, it should have been obvious I should have compared the values with Pascal triangle.
In Rectangular Grid Walk, the number of ways an object can run through an a × b grid going only two directions is ( b a + b ) .
As the definition says a □ b = a □ ( b − 1 ) + ( a − 1 ) □ b (when a,b >1) we can think of a 5 × 5 rectangular grid:
When we "shatter" the original 5 □ 5 step by step with the previous rule, and the other two rules: n □ 1 = ( n − 1 ) □ 1 + n and 1 □ n = 1 □ n − 1 + n we see that 5 □ 1 , 4 □ 1 , 3 □ 1 , 2 □ 1 , 1 □ 5 , 1 □ 4 , 1 □ 3 , 1 □ 2 and 1 □ 1 appear during the process as many times as there are possible ways to reach the dot representing them in the rectangular grid above. When this process is continued, each n □ 1 and 1 □ n produces the number n in the final result as they are "shattered" by the rules n □ 1 = ( n − 1 ) □ 1 + n and 1 □ n = 1 □ n − 1 + n . Eventually we get these " n :s" with each value of n and a number of 1 □ 1 :s, and there was the definition 1 □ 1 = 1 .
For each positive integer m and n , the rectangular grid presenting m □ n has the measures m × n in the amount of dots. In general, for each positive integer a when 1 < a ≤ ( m − 2 ) , ( m − a ) □ 1 appears ( a n − 1 + a ) times during the process, and for each positive integer b when 1 < b ≤ ( n − 2 ) , ( n − b ) □ 1 appears ( b m − 1 + b ) times. Each ( m − a ) □ 1 produces an m − a and each ( n − b ) □ 1 produces an n − b in the final result, so the previous values are multiplied with them and then summed together. Additionally, 1 □ 1 is produced ( n − 1 m + n − 2 ) times.
For example, there is one way to produce m □ 1 with using the rules above, and ( 1 n ) ways to produce ( m − 1 ) □ 1 , judging by the formula for rectangular grid walk presented above in the start. Each m □ 1 produces an m when "shattering", and each ( m − 1 ) □ 1 produces an m − 1 when "shattering".
This means m □ n = k = 0 ∑ m − 2 ( ( m − k ) ( k n − 1 + k ) ) + k = 0 ∑ n − 2 ( ( n − k ) ( k m − 1 + k ) ) + ( n − 1 m + n − 2 )
We get
5 □ 5 = 2 ( 1 × 5 + 5 × 4 + ( 2 6 ) × 3 + ( 3 7 ) × 2 ) + ( 4 8 ) × 1 = 2 ( 5 + 2 0 + 4 5 + 7 0 ) + 7 0 = 2 × 1 4 0 + 7 0 = 3 5 0 .
I find it hard to understand what you're trying to express. I understand what you're doing, but maybe showing how to calculate the value directly (similar to the case of tabulating the number of ways to reach ( 5 , 5 ) would make the solution easier to comprehend.
Do you know what the general formula for a □ b is?
Log in to reply
Now I've added the general way of evaluating m □ n , and I've tried to make the solution a bit more clear.
Log in to reply
There is a nicer format for the answer, ( a a + b ) − ( a − 1 a + b − 2 ) . (Edit: I had the wrong formula previously)
Do you see how to arrive at this formula from the problem / setup?
(Of course, we can prove it by induction, but that requires somehow guessing the pattern, which isn't too obvious).
Log in to reply
@Calvin Lin – I haven't really managed to see the way to get that simpler formula.
Here is a straightforward way to run Chung Kevin's idea using Haskell:
1 2 3 4 5 |
|
Then,
f 5 5
gives the desired result.
that's my type of solution! did the same!
Log in to reply
Please tell me you used haskell too!
Log in to reply
Nopes! haven't learned it yet! i used c++11
Problem Loading...
Note Loading...
Set Loading...
Set up the summation on a grid, to ensure that we get the proper numbers.
The first rule tells us the first horizontal row.
The second rule tells us the first vertical row.
The third rule tells us that each cell is obtained by summing up the cell above it and the cell to the left.
Hence, we get