给定两个整数N , M表示N×M棋盘,任务是计算一个骑士可以从(0, 0)开始到达(N, M)的方式的数量。由于答案可能非常大,打印答案模10 9 +7 。
例子:
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).
方法:这里的想法是观察每次移动x坐标值+y坐标值增加3的模式。按照以下步骤解决问题。
- 如果(N + M)不能被3整除,则不存在可能的路径。
- 如果(N + M) % 3==0则计算(+1, +2)类型的移动次数,即X并计算(+2, +1)类型的移动次数,即Y 。
- 找出(+1, +2)类型的方程,即X + 2Y = N
- 找出(+2, +1)类型的方程,即2X + Y = M
- 找到X和Y的计算值,如果X < 0或Y < 0 ,则不存在可能的路径。
- 否则,计算( X+Y) C Y 。
下面是上述方法的实现:
C++14
// C++ Program to implement
// the above approach
#include
using namespace std;
const int Mod = 1e9 + 7;
// Function to return X^Y % Mod
int power(int X, int Y, int Mod)
{
// Base Case
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;
}
// Function to return the
// inverse of factorial of N
int Inversefactorial(int N)
{
// Base case
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);
}
// Function to return factorial
// of n % Mod
int factorial(int N)
{
// Base case
if (N <= 0)
return 1;
int fact = 1;
for (int i = 1; i <= N; i++) {
fact = (fact * i) % Mod;
}
return fact;
}
// Function to return the value
// of n! / (( n- k)! * k!)
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;
}
// Function to return the count of
// ways to reach (n, m) from (0, 0)
int TotalWaYs(int N, int M)
{
// If (N + M) % 3 != 0
if ((N + M) % 3 != 0)
// No possible way exists
return 0;
// Calculate X and Y from the
// equations X + 2Y = N
// and 2X + Y == M
int X = N - (N + M) / 3;
int Y = M - (N + M) / 3;
if (X < 0 || Y < 0)
return 0;
return nck(X + Y, Y);
}
// Driver Code
int main()
{
int N = 3, M = 3;
cout << TotalWaYs(N, M);
return 0;
}
Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
static int Mod = (int) (1e9 + 7);
// Function to return X^Y % Mod
static int power(int X, int Y, int Mod)
{
// Base Case
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;
}
// Function to return the
// inverse of factorial of N
static int Inversefactorial(int N)
{
// Base case
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);
}
// Function to return factorial
// of n % Mod
static int factorial(int N)
{
// Base case
if (N <= 0)
return 1;
int fact = 1;
for (int i = 1; i <= N; i++)
{
fact = (fact * i) % Mod;
}
return fact;
}
// Function to return the value
// of n! / (( n- k)! * k!)
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;
}
// Function to return the count of
// ways to reach (n, m) from (0, 0)
static int TotalWaYs(int N, int M)
{
// If (N + M) % 3 != 0
if (((N + M) % 3 )!= 0)
// No possible way exists
return 0;
// Calculate X and Y from the
// equations X + 2Y = N
// and 2X + Y == M
int X = N - (N + M) / 3;
int Y = M - (N + M) / 3;
if (X < 0 || Y < 0)
return 0;
return nck(X + Y, Y);
}
// Driver Code
public static void main(String[] args)
{
int N = 3, M = 3;
System.out.print(TotalWaYs(N, M));
}
}
// This code is contributed by Rohit_ranjan
Python3
# Python3 program to implement
# above approach
Mod = int(1e9 + 7)
# Function to return X^Y % Mod
def power(X, Y, Mod):
# Base case
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
# Function to return the
# inverse of factorial of N
def Inversefactorial(N):
# Base case
if N <= 0:
return 1
fact = 1
for i in range(1, N + 1):
fact = (fact * i) % Mod
return power(fact, Mod - 2, Mod)
# Function to return factorial
# of n % Mod
def factorial(N):
# Base case
if N <= 0:
return 1
fact = 1
for i in range(1, N + 1):
fact = (fact * i) % Mod
return fact
# Function to return the value
# of n! / (( n- k)! * k!)
def nck(N, K):
factN = factorial(N)
inv = Inversefactorial(K)
invFact = Inversefactorial(N - K)
return (((factN * inv) % Mod) * invFact) % Mod
# Function to return the count of
# ways to reach (n, m) from (0, 0)
def TotalWays(N, M):
# If (N + M) % 3 != 0
if (N + M) % 3 != 0:
# No possible way exists
return 0
# Calculate X and Y from the
# equations X + 2Y = N
# and 2X + Y == M
X = N - (N + M) // 3
Y = M - (N + M) // 3
if X < 0 or Y < 0:
return 0
return nck(X + Y, Y)
# Driver code
N, M = 3, 3
print(TotalWays(N, M))
# This code is contributed by Stuti Pathak
C#
// C# program to implement
// the above approach
using System;
class GFG{
static int Mod = (int)(1e9 + 7);
// Function to return X^Y % Mod
static int power(int X, int Y, int Mod)
{
// Base Case
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;
}
// Function to return the
// inverse of factorial of N
static int Inversefactorial(int N)
{
// Base case
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);
}
// Function to return factorial
// of n % Mod
static int factorial(int N)
{
// Base case
if (N <= 0)
return 1;
int fact = 1;
for(int i = 1; i <= N; i++)
{
fact = (fact * i) % Mod;
}
return fact;
}
// Function to return the value
// of n! / (( n- k)! * k!)
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;
}
// Function to return the count of
// ways to reach (n, m) from (0, 0)
static int TotalWaYs(int N, int M)
{
// If (N + M) % 3 != 0
if (((N + M) % 3 ) != 0)
// No possible way exists
return 0;
// Calculate X and Y from the
// equations X + 2Y = N
// and 2X + Y == M
int X = N - (N + M) / 3;
int Y = M - (N + M) / 3;
if (X < 0 || Y < 0)
return 0;
return nck(X + Y, Y);
}
// Driver Code
public static void Main(String[] args)
{
int N = 3, M = 3;
Console.Write(TotalWaYs(N, M));
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
2
时间复杂度: O(X + Y + log(mod))。
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。