# Matrix Tracing

# Matrix Tracing

+ 4 comments Just thinking through the problem, I realized it's just a sideways Pascal's Triangle. Let's say our word is "ABCDE".

- A B C
- B C D
- C D E

Let's look at each individual spot and how many paths there are to them. The "C" in the top right and bottom left have only one path to them, and the same can be said of the "B"s and "A". So, we can write the grid like:

- 1 1 1
- 1 C D
- 1 D E

Now, looking at the remaining "C", there are two ways to reach it. You can either come in from above, or come in from the left. There is only one way to come in from the top and one way to come in from the left, we can say that there are two ways to reach it:

- 1 1 1
- 1 2 D
- 1 D F

Now, for the right-most D, we can see that there are two ways to come in from the left, and one way to come in from above:

- 1 1 1
- 1 2 3
- 1 D E

Following a similar process for the last two letters, we can map out the number of paths to each spot in the matrix as:

- 1 1 1
- 1 2 3
- 1 3 6

Giving us a final answer of 6.

This pattern should look familiar. It is actually a Pascal's Triangle in a different orientation. With a much larger matrix, we would get (sorry for terrible formatting):

- 1 1 1 1 1 1
- 1 2 3 4 5 6
- 1 3 6 10 15 21
- 1 4 10 20 35 46
- 1 5 15 35 70 116
- 1 6 21 46 116 232

For a Pascal's Triangle, one can simply use a Combination function to find the value for any point in the triangle.

I haven't solved it yet, but for those completely clueless about where to even start, hopefully this helps.

[deleted] + 1 comment **Prerequisites to solve this problem**1.

**fast exponentiation**#define ll long long int #define mod %1000000007 ll fastexpo(ll a, ll n) { ll ret=1; ll b=a; while(n) { if(n&1)ret=(ret*b)mod; //if(n==odd) b=(b*b)mod; n>>=1; // n/=2 } return (ll)ret; }

2.

**X^(-1) mod(p) can be written as X^(p-2) mod(p)**

+ 0 comments If anyone want euclid's extended algo; Here we go;

int mmi(int a, int m){ if(a==1)return 1; int e=m; int f=a; int x= 0; int y= 1; unsigned long long u = 1; int v = 0; int q=0,r=0,c=0,d=0; while(f!=1){ q = e/f; r = e%f; c = x- q*u; d = y - q*v; x = u; y = v; u = c; v = d; e = f; f = r; } return (u+m)%m; }

+ 2 comments I thought about it this way: there is nothing special about it being a word (like "AWAY"), all we're trying to do is travel from the top left corner of a matrix to the bottom right by only moving right (R) or down (D).

If the matrix has 'm' rows and 'n' columns, then any valid path needs 'm-1' moves down and 'n-1' moves to the right, so 1 simple path could look like this: RRRRR....RRDDDDD....DD with 'n-1' Right moves and then 'm-1' Down moves.

How many valid paths are there? Well, each valid path has a total of (n+m-2) moves that can be reordered in (n+m-2)! ways. However, all the R's are identical so we need to remove all arrangements that are just shifting the R's around. Same argument applies to the D's, so the total number of valid paths is (n+m-2)! / (n-1)! / (m-1)! (this is why Pascal's triangle is also a correct way of thinking about the number of valid paths). To calculate this, follow the instructions in the discussion of the Sherlock and Permutations problem.

+ 0 comments This is same as number of ways to reach the bottom-right of matrix starting from the top-left corner. Since the constraints are high so we use a bit of maths here.

*The number of ways for a m n matrix can be calculated using formula :***number_of_ways = (m + n - 2) ! / ((m - 1) ! * (n - 1) !)**Since denominator will be really large we find the multiplicative modulo inverse.

*Multiplicative inverse of a number x w.r.t modulo m is given by :***multiplicative_inverse = pow(x, m - 2)**NOTE : For pow do the fast exponentiation method.

The code is as below :

long long fact(long long n) { if(n <= 1) return 1; return (n % mod * fact(n - 1) % mod) % mod; } long long inv(long long a, long long n) { long long res = 1; while(n) { if(n % 2 == 1) res = (res % mod * a % mod) % mod; a = (a % mod * a % mod) % mod; n = n / 2; } return res; } long long solve(long long n, long long m) { long long num = fact(n + m - 2); long long d1 = (fact(n - 1) % mod * fact(m - 1) % mod) % mod; long long d2 = inv(d1, mod - 2); num = ((num % mod) * (d2 % mod)) % mod; return num; }

Sort 48 Discussions, By:

Please Login in order to post a comment