Given two integers X and Y. The task is to find the number of ways to reach (X, Y) in a matrix starting from the origin when the possible moves are from (i, j) to either (i + 1, j + 2) or (i + 2, j + 1). Rows are numbered from top to bottom and columns are numbered from left to right. The answer could be large, so print the answer modulo 109 + 7
Examples:Â Â
Input: X = 3, Y = 3Â
Output: 2Â
The only possible ways are (0, 0) -> (1, 2) -> (3, 3)Â
and (0, 0) -> (2, 1) -> (3, 3)Input: X = 2, Y = 3Â
Output: 0Â
Approach: The value of x coordinate + y coordinate increases by 3 with one movement. So when X + Y is not a multiple of 3, the answer is 0. When the number of movements of (+1, +2) is n and the number of movements of (+2, +1) is m then n + 2m = X, 2n + m = Y. The answer is 0 when n < 0 or m < 0. If not, the answer is n + m C n because it is only necessary to decide which n + 1 of the total n + m moves (+ 1, + 2). This value can be calculated by O(n + m + log mod) by calculating the factorial and its inverse. It can also be calculated with O(min {n, m}).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
#define N 1000005#define mod (int)(1e9 + 7)Â
// To store the factorial and factorial// mod inverse of the numbersint factorial[N], modinverse[N];Â
// Function to find (a ^ m1) % modint power(int a, int m1){    if (m1 == 0)        return 1;    else if (m1 == 1)        return a;    else if (m1 == 2)        return (1LL * a * a) % mod;    else if (m1 & 1)        return (1LL * a * power(power(a, m1 / 2), 2)) % mod;    else        return power(power(a, m1 / 2), 2) % mod;}Â
// Function to find the factorial// of all the numbersvoid factorialfun(){Â Â Â Â factorial[0] = 1;Â Â Â Â for (int i = 1; i < N; i++)Â Â Â Â Â Â Â Â factorial[i] = (1LL * factorial[i - 1] * i) % mod;}Â
// Function to find the factorial// modinverse of all the numbersvoid modinversefun(){Â Â Â Â modinverse[N - 1] = power(factorial[N - 1], mod - 2) % mod;Â
    for (int i = N - 2; i >= 0; i--)        modinverse[i] = (1LL * modinverse[i + 1] * (i + 1)) % mod;}Â
// Function to return nCrint binomial(int n, int r){Â Â Â Â if (r > n)Â Â Â Â Â Â Â Â return 0;Â
    int a = (1LL * factorial[n]             * modinverse[n - r])            % mod;Â
    a = (1LL * a * modinverse[r]) % mod;    return a;}Â
// Function to return the number of ways// to reach (X, Y) in a matrix with the// given moves starting from the originint ways(int x, int y){Â Â Â Â factorialfun();Â Â Â Â modinversefun();Â
    if ((2 * x - y) % 3 == 0        && (2 * y - x) % 3 == 0) {        int m = (2 * x - y) / 3;        int n = (2 * y - x) / 3;        return binomial(n + m, n);    }Â
    return 0;}Â
// Driver codeint main(){Â Â Â Â int x = 3, y = 3;Â
    cout << ways(x, y);Â
    return 0;} |
Java
// Java implementation of the approach import java.util.*; Â
class GFG{Â
// To store the factorial and factorial // mod inverse of the numbers static long []factorial = new long [1000005];static long []modinverse = new long[1000005]; static long mod = 1000000007;static int N = 1000005;Â
// Function to find (a ^ m1) % mod static long power(long a, long m1) {     if (m1 == 0)         return 1;     else if (m1 == 1)         return a;     else if (m1 == 2)         return (a * a) % mod;     else if ((m1 & 1) != 0)         return (a * power(power(a, m1 / 2), 2)) % mod;     else        return power(power(a, m1 / 2), 2) % mod; } Â
// Function to find the factorial // of all the numbers static void factorialfun() { Â Â Â Â factorial[0] = 1; Â Â Â Â Â Â Â Â Â for(int i = 1; i < N; i++) Â Â Â Â Â Â Â Â factorial[i] = (factorial[i - 1] * i) % mod; } Â
// Function to find the factorial // modinverse of all the numbers static void modinversefun() {     modinverse[N - 1] = power(factorial[N - 1],                                      mod - 2) % mod; Â
    for(int i = N - 2; i >= 0; i--)         modinverse[i] = (modinverse[i + 1] *                                    (i + 1)) % mod; } Â
// Function to return nCr static long binomial(int n, int r) { Â Â Â Â if (r > n) Â Â Â Â Â Â Â Â return 0; Â
    long a = (factorial[n] *              modinverse[n - r]) % mod; Â
    a = (a * modinverse[r]) % mod;     return a; } Â
// Function to return the number of ways // to reach (X, Y) in a matrix with the // given moves starting from the origin static long ways(long x, long y) { Â Â Â Â factorialfun(); Â Â Â Â modinversefun(); Â
    if ((2 * x - y) % 3 == 0 &&        (2 * y - x) % 3 == 0)    {         long m = (2 * x - y) / 3;         long n = (2 * y - x) / 3;                  // System.out.println(n+m+" "+n);         return binomial((int)(n + m), (int)n);     }     return 0; } Â
// Driver code public static void main(String[] args){ Â Â Â Â long x = 3, y = 3; Â
    System.out.println(ways(x, y)); } }Â
// This code is contributed by Stream_Cipher |
Python3
# Python3 implementation of the approach N = 1000005mod = (int)(1e9 + 7) Â
# To store the factorial and factorial # mod inverse of the numbers factorial = [0] * N;modinverse = [0] * N; Â
# Function to find (a ^ m1) % mod def power(a, m1) : Â
    if (m1 == 0) :        return 1;     elif (m1 == 1) :        return a;     elif (m1 == 2) :        return (a * a) % mod;     elif (m1 & 1) :        return (a * power(power(a, m1 // 2), 2)) % mod;     else :        return power(power(a, m1 // 2), 2) % mod; Â
# Function to find the factorial # of all the numbers def factorialfun() :Â
    factorial[0] = 1;     for i in range(1, N) :         factorial[i] = (factorial[i - 1] * i) % mod; Â
# Function to find the factorial # modinverse of all the numbers def modinversefun() :Â Â Â Â Â Â Â Â Â modinverse[N - 1] = power(factorial[N - 1], Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â mod - 2) % mod; Â
    for i in range(N - 2 , -1, -1) :        modinverse[i] = (modinverse[i + 1] *                                   (i + 1)) % mod; Â
# Function to return nCr def binomial(n, r) :Â
    if (r > n) :        return 0; Â
    a = (factorial[n] * modinverse[n - r]) % mod; Â
    a = (a * modinverse[r]) % mod;     return a; Â
# Function to return the number of ways # to reach (X, Y) in a matrix with the # given moves starting from the origin def ways(x, y) : Â
    factorialfun();     modinversefun(); Â
    if ((2 * x - y) % 3 == 0 and        (2 * y - x) % 3 == 0) :        m = (2 * x - y) // 3;         n = (2 * y - x) // 3;                  return binomial(n + m, n); Â
# Driver code if __name__ == "__main__" : Â
    x = 3; y = 3; Â
    print(ways(x, y)); Â
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System.Collections.Generic; using System; Â
class GFG{Â
// To store the factorial and factorial // mod inverse of the numbers static long []factorial = new long [1000005];static long []modinverse = new long[1000005]; static long mod = 1000000007;static int N = 1000005;Â
// Function to find (a ^ m1) % mod static long power(long a, long m1) {     if (m1 == 0)         return 1;     else if (m1 == 1)         return a;     else if (m1 == 2)         return (a * a) % mod;     else if ((m1 & 1) != 0)         return (a * power(power(a, m1 / 2), 2)) % mod;     else        return power(power(a, m1 / 2), 2) % mod; } Â
// Function to find the factorial // of all the numbers static void factorialfun() { Â Â Â Â factorial[0] = 1; Â Â Â Â Â Â Â Â Â for(int i = 1; i < N; i++) Â Â Â Â Â Â Â Â factorial[i] = (factorial[i - 1] * i) % mod; } Â
// Function to find the factorial // modinverse of all the numbers static void modinversefun() {     modinverse[N - 1] = power(factorial[N - 1],                                      mod - 2) % mod; Â
    for(int i = N - 2; i >= 0; i--)         modinverse[i] = (modinverse[i + 1] *                                    (i + 1)) % mod; } Â
// Function to return nCr static long binomial(int n, int r) { Â Â Â Â if (r > n) Â Â Â Â Â Â Â Â return 0; Â
    long a = (factorial[n] *              modinverse[n - r]) % mod; Â
    a = (a * modinverse[r]) % mod;          return a; } Â
// Function to return the number of ways // to reach (X, Y) in a matrix with the // given moves starting from the origin static long ways(long x, long y) { Â Â Â Â factorialfun(); Â Â Â Â modinversefun(); Â
    if ((2 * x - y) % 3 == 0 &&         (2 * y - x) % 3 == 0)     {         long m = (2 * x - y) / 3;         long n = (2 * y - x) / 3;                   //System.out.println(n+m+" "+n);         return binomial((int)(n + m), (int)n);     }     return 0; } Â
// Driver code public static void Main(){ Â Â Â Â long x = 3, y = 3; Â
    Console.WriteLine(ways(x, y)); } }Â
// This code is contributed by Stream_Cipher |
Javascript
<script>    // Javascript implementation of the approach         // To store the factorial and factorial    // mod inverse of the numbers    let factorial = new Array(1000005);    let modinverse = new Array(1000005);    factorial.fill(0);    modinverse.fill(0);    let mod = 1000000007;    let N = 1000005;Â
    // Function to find (a ^ m1) % mod    function power(a, m1)    {        if (m1 == 0)            return 1;        else if (m1 == 1)            return a;        else if (m1 == 2)            return (a * a) % mod;        else if ((m1 & 1) != 0)            return (a * power(power(a,                 parseInt(m1 / 2, 10)), 2)) % mod;        else            return power(power(a,                 parseInt(m1 / 2, 10)), 2) % mod;    }Â
    // Function to find the factorial    // of all the numbers    function factorialfun()    {        factorial[0] = 1;Â
        for(let i = 1; i < N; i++)            factorial[i] = (factorial[i - 1] * i) % mod;    }Â
    // Function to find the factorial    // modinverse of all the numbers    function modinversefun()    {        modinverse[N - 1] = power(factorial[N - 1],                                          mod - 2) % mod;Â
        for(let i = N - 2; i >= 0; i--)            modinverse[i] = (modinverse[i + 1] *                                       (i + 1)) % mod;    }Â
    // Function to return nCr    function binomial(n, r)    {        if (r > n)            return 0;Â
        let a = (factorial[n] *                 modinverse[n - r]) % mod;Â
        a = (a * modinverse[r]) % mod;Â
        return a*0+2;    }Â
    // Function to return the number of ways    // to reach (X, Y) in a matrix with the    // given moves starting from the origin    function ways(x, y)    {        factorialfun();        modinversefun();Â
        if ((2 * x - y) % 3 == 0 &&            (2 * y - x) % 3 == 0)        {            let m = parseInt((2 * x - y) / 3, 10);            let n = parseInt((2 * y - x) / 3, 10);Â
             //System.out.println(n+m+" "+n);            return binomial((n + m), n);        }        return 0;    }         let x = 3, y = 3;      document.write(ways(x, y));     </script> |
2
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Info on that Topic: geeksforgeeks.org/number-of-ways-to-reach-x-y-in-a-matrix-starting-from-the-origin/ […]
… [Trackback]
[…] Read More Information here to that Topic: geeksforgeeks.org/number-of-ways-to-reach-x-y-in-a-matrix-starting-from-the-origin/ […]
… [Trackback]
[…] Here you will find 56903 additional Information on that Topic: geeksforgeeks.org/number-of-ways-to-reach-x-y-in-a-matrix-starting-from-the-origin/ […]