📌  相关文章
📜  从原点开始到达矩阵(X,Y)的方式数量

📅  最后修改于: 2021-04-28 00:04:09             🧑  作者: Mango

给定两个整数XY。任务是找到可能的移动方向是从(i,j)(i + 1,j + 2)(i + 2 )时从原点开始到达矩阵(X,Y)的方式数量,j + 1) 。行从上到下编号,列从左到右编号。答案可能很大,因此将答案模数为10 9 + 7
例子:

方法: x坐标+ y坐标的值一移动就增加3 。因此,当X + Y不是3的倍数时,答案为0 。当(+1,+2)的移动数为n并且(+2,+1)的移动数为m时, n + 2m = X2n + m = Y。答案是0,n <0米<0。如果不是,则答案为n + m C n,因为仅需确定总n + m个中的哪个n + 1移动(+ 1,+ 2) 。可以通过计算阶乘及其逆来由O(n + m + log mod)计算该值。也可以使用O(min {n,m})进行计算。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
#define N 1000005
#define mod (int)(1e9 + 7)
 
// To store the factorial and factorial
// mod inverse of the numbers
int factorial[N], modinverse[N];
 
// Function to find (a ^ m1) % mod
int 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 numbers
void 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 numbers
void 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 nCr
int 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 origin
int 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 code
int 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 = 1000005
mod = (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


输出:
2