Given two integers N, M denoting N×M chessboard, the task is to count the number of ways a knight can reach (N, M) starting from (0, 0). Since the answer can be very large, print the answer modulo 109+7.
Example:
Input: N =3, M= 3
Output: 2
Explanation:
Two ways to reach (3, 3) form (0, 0) are as follows:
(0, 0) ? (1, 2) ? (3, 3)
(0, 0) ? (2, 1) ? (3, 3)
Input: N=4, M=3
Output: 0
Explanation: No possible way exists to reach (4, 3) form (0, 0).
Approach: Idea here is to observe the pattern that each move increments the value of the x-coordinate + value of y-coordinate by 3. Follow the steps below to solve the problem.
- If (N + M) is not divisible by 3 then no possible path exists.
- If (N + M) % 3==0 then count the number of moves of type (+1, +2) i.e, X and count the number of moves of type (+2, +1) i.e, Y.
- Find the equation of the type (+1, +2) i.e. X + 2Y = N
- Find the equation of the type (+2, +1) i.e. 2X + Y = M
- Find the calculated values of X and Y, if X < 0 or Y < 0, then no possible path exists.
- Otherwise, calculate (X+Y)CY.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
const int Mod = 1e9 + 7;
int power( int X, int Y, int Mod)
{
if (Y == 0)
return 1;
int p = power(X, Y / 2, Mod) % Mod;
p = (p * p) % Mod;
if (Y & 1) {
p = (X * p) % Mod;
}
return p;
}
int Inversefactorial( int N)
{
if (N <= 0)
return 1;
int fact = 1;
for ( int i = 1; i <= N; i++) {
fact = (fact * i) % Mod;
}
return power(fact, Mod - 2, Mod);
}
int factorial( int N)
{
if (N <= 0)
return 1;
int fact = 1;
for ( int i = 1; i <= N; i++) {
fact = (fact * i) % Mod;
}
return fact;
}
int nck( int N, int K)
{
int factN = factorial(N);
int inv = Inversefactorial(K);
int invFact = Inversefactorial(N - K);
return (((factN * inv) % Mod) * invFact) % Mod;
}
int TotalWaYs( int N, int M)
{
if ((N + M) % 3 != 0)
return 0;
int X = N - (N + M) / 3;
int Y = M - (N + M) / 3;
if (X < 0 || Y < 0)
return 0;
return nck(X + Y, Y);
}
int main()
{
int N = 3, M = 3;
cout << TotalWaYs(N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int Mod = ( int ) (1e9 + 7 );
static int power( int X, int Y, int Mod)
{
if (Y == 0 )
return 1 ;
int p = power(X, Y / 2 , Mod) % Mod;
p = (p * p) % Mod;
if ((Y & 1 ) != 0 )
{
p = (X * p) % Mod;
}
return p;
}
static int Inversefactorial( int N)
{
if (N <= 0 )
return 1 ;
int fact = 1 ;
for ( int i = 1 ; i <= N; i++)
{
fact = (fact * i) % Mod;
}
return power(fact, Mod - 2 , Mod);
}
static int factorial( int N)
{
if (N <= 0 )
return 1 ;
int fact = 1 ;
for ( int i = 1 ; i <= N; i++)
{
fact = (fact * i) % Mod;
}
return fact;
}
static int nck( int N, int K)
{
int factN = factorial(N);
int inv = Inversefactorial(K);
int invFact = Inversefactorial(N - K);
return (((factN * inv) % Mod) * invFact) % Mod;
}
static int TotalWaYs( int N, int M)
{
if (((N + M) % 3 )!= 0 )
return 0 ;
int X = N - (N + M) / 3 ;
int Y = M - (N + M) / 3 ;
if (X < 0 || Y < 0 )
return 0 ;
return nck(X + Y, Y);
}
public static void main(String[] args)
{
int N = 3 , M = 3 ;
System.out.print(TotalWaYs(N, M));
}
}
|
Python3
Mod = int ( 1e9 + 7 )
def power(X, Y, Mod):
if Y = = 0 :
return 1
p = power(X, Y / / 2 , Mod) % Mod
p = (p * p) % Mod
if Y & 1 :
p = (X * p) % Mod
return p
def Inversefactorial(N):
if N < = 0 :
return 1
fact = 1
for i in range ( 1 , N + 1 ):
fact = (fact * i) % Mod
return power(fact, Mod - 2 , Mod)
def factorial(N):
if N < = 0 :
return 1
fact = 1
for i in range ( 1 , N + 1 ):
fact = (fact * i) % Mod
return fact
def nck(N, K):
factN = factorial(N)
inv = Inversefactorial(K)
invFact = Inversefactorial(N - K)
return (((factN * inv) % Mod) * invFact) % Mod
def TotalWays(N, M):
if (N + M) % 3 ! = 0 :
return 0
X = N - (N + M) / / 3
Y = M - (N + M) / / 3
if X < 0 or Y < 0 :
return 0
return nck(X + Y, Y)
N, M = 3 , 3
print (TotalWays(N, M))
|
C#
using System;
class GFG{
static int Mod = ( int )(1e9 + 7);
static int power( int X, int Y, int Mod)
{
if (Y == 0)
return 1;
int p = power(X, Y / 2, Mod) % Mod;
p = (p * p) % Mod;
if ((Y & 1) != 0)
{
p = (X * p) % Mod;
}
return p;
}
static int Inversefactorial( int N)
{
if (N <= 0)
return 1;
int fact = 1;
for ( int i = 1; i <= N; i++)
{
fact = (fact * i) % Mod;
}
return power(fact, Mod - 2, Mod);
}
static int factorial( int N)
{
if (N <= 0)
return 1;
int fact = 1;
for ( int i = 1; i <= N; i++)
{
fact = (fact * i) % Mod;
}
return fact;
}
static int nck( int N, int K)
{
int factN = factorial(N);
int inv = Inversefactorial(K);
int invFact = Inversefactorial(N - K);
return (((factN * inv) % Mod) * invFact) % Mod;
}
static int TotalWaYs( int N, int M)
{
if (((N + M) % 3 ) != 0)
return 0;
int X = N - (N + M) / 3;
int Y = M - (N + M) / 3;
if (X < 0 || Y < 0)
return 0;
return nck(X + Y, Y);
}
public static void Main(String[] args)
{
int N = 3, M = 3;
Console.Write(TotalWaYs(N, M));
}
}
|
Javascript
<script>
var Mod = 1000000007;
function power(X, Y, Mod)
{
if (Y == 0)
return 1;
var p = power(X, Y / 2, Mod) % Mod;
p = (p * p) % Mod;
if (Y & 1) {
p = (X * p) % Mod;
}
return p;
}
function Inversefactorial(N)
{
if (N <= 0)
return 1;
var fact = 1;
for ( var i = 1; i <= N; i++) {
fact = (fact * i) % Mod;
}
return power(fact, Mod - 2, Mod);
}
function factorial(N)
{
if (N <= 0)
return 1;
var fact = 1;
for ( var i = 1; i <= N; i++) {
fact = (fact * i) % Mod;
}
return fact;
}
function nck( N, K)
{
var factN = factorial(N);
var inv = Inversefactorial(K);
var invFact = Inversefactorial(N - K);
return (((factN * inv) % Mod) * invFact) % Mod;
}
function TotalWaYs(N, M)
{
if ((N + M) % 3 != 0)
return 0;
var X = N - (N + M) / 3;
var Y = M - (N + M) / 3;
if (X < 0 || Y < 0)
return 0;
return nck(X + Y, Y);
}
var N = 3, M = 3;
document.write( TotalWaYs(N, M));
</script>
|
Time Complexity: O(X + Y + log(mod)).
Auxiliary Space: O(1)