📌  相关文章
📜  计算骑士达成目标的所有可能方式

📅  最后修改于: 2021-04-22 08:25:27             🧑  作者: Mango

给定两个整数NM表示N×M棋盘,任务是计算骑士从(0,0 )开始可以达到(N,M )的方式。由于答案可能非常大,因此请以10 9 +7为模数打印答案。

例子:

方法:这里的想法是观察每个移动将x坐标的值+ y坐标增加3的模式。请按照以下步骤解决问题。

  1. 如果(N + M)不能被3整除,则不存在可能的路径。
  2. 如果(N + M)%3 == 0,则计算类型为(+1,+2)的移动数,即X,并计算类型为(+2,+1)的移动数,即Y。
  3. 找出类型(+1,+2)的方程,即X + 2Y = N
  4. 查找类型(+2,+1)的方程,即2X + Y = M
  5. 找到XY的计算值,如果X <0Y <0 ,则不存在可能的路径。
  6. 否则,计算( 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


输出:
2

时间复杂度: O(X + Y + log(mod))。
辅助空间: O(1)