📌  相关文章
📜  可以从 K 开始形成的 N 大小数组的计数,使得每个元素都可以被下一个整除

📅  最后修改于: 2022-05-13 01:56:04.356000             🧑  作者: Mango

可以从 K 开始形成的 N 大小数组的计数,使得每个元素都可以被下一个整除

给定两个整数NK ,任务是找到大小为N的不同数组的数量,这些数组的第一个元素为K ,使得除了最后一个元素之外的每个元素都可以被数组中的下一个元素整除。由于计数可能非常大,因此打印模10 9 + 7

例子:

方法:给定的问题可以通过使用数论和组合来解决。数组的第一个元素是K ,那么下一个数组元素是K的因子之一。请按照以下步骤解决给定的问题:

  • 初始化一个变量,比如res1 ,它存储所形成的数组的结果计数。
  • 找到数字K的所有素因数的幂,并对每个素因数P执行以下步骤:
    • P在值K中出现的次数。让那个 count 成为count
    • (N – count + 1) C count给出了保留P的一个因素的可能方法的数量。
    • 将值(N – count + 1) C count与值res相乘。
  • 完成上述步骤后,打印res的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
int MOD = 1000000007;
 
// Find the value of x raised to the
// yth power modulo MOD
long modPow(long x, long y)
{
    // Stores the value of x^y
    long r = 1, a = x;
 
    // Iterate until y is positive
    while (y > 0) {
        if ((y & 1) == 1) {
            r = (r * a) % MOD;
        }
        a = (a * a) % MOD;
        y /= 2;
    }
 
    return r;
}
// Function to perform the Modular
// Multiplicative Inverse using the
// Fermat's little theorem
long modInverse(long x)
{
    return modPow(x, MOD - 2);
}
 
// Modular division x / y, find
// modular multiplicative inverse
// of y and multiply by x
long modDivision(long p, long q)
{
    return (p * modInverse(q)) % MOD;
}
 
// Function to find Binomial Coefficient
// C(n, k) in O(k) time
long C(long n, int k)
{
    // Base Case
    if (k > n) {
        return 0;
    }
    long p = 1, q = 1;
 
    for (int i = 1; i <= k; i++) {
 
        // Update the value of p and q
        q = (q * i) % MOD;
        p = (p * (n - i + 1)) % MOD;
    }
 
    return modDivision(p, q);
}
 
// Function to find the count of arrays
// having K as the first element satisfying
// the given criteria
int countArrays(int N, int K)
{
    // Stores the resultant count of arrays
    long res = 1;
 
    // Find the factorization of K
    for (int p = 2; p <= K / p; p++) {
 
        int c = 0;
 
        // Stores the count of the exponent
        // of the currentprime factor
        while (K % p == 0) {
            K /= p;
            c++;
        }
        res = (res * C(N - 1 + c, c))
              % MOD;
    }
    if (N > 1) {
 
        // N is one last prime factor,
        // for c = 1 -> C(N-1+1, 1) = N
        res = (res * N) % MOD;
    }
 
    // Return the total count
    return res;
}
 
// Driver Code
int main()
{
    int N = 3, K = 5;
    cout << countArrays(N, K);
 
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
    public static int MOD = 1000000007;
 
    // Find the value of x raised to the
    // yth power modulo MOD
    public static long modPow(long x, long y)
    {
       
        // Stores the value of x^y
        long r = 1, a = x;
 
        // Iterate until y is positive
        while (y > 0) {
            if ((y & 1) == 1) {
                r = (r * a) % MOD;
            }
            a = (a * a) % MOD;
            y /= 2;
        }
 
        return r;
    }
 
    // Function to perform the Modular
    // Multiplicative Inverse using the
    // Fermat's little theorem
    public static long modInverse(long x) {
        return modPow(x, MOD - 2);
    }
 
    // Modular division x / y, find
    // modular multiplicative inverse
    // of y and multiply by x
    public static long modDivision(long p, long q) {
        return (p * modInverse(q)) % MOD;
    }
 
    // Function to find Binomial Coefficient
    // C(n, k) in O(k) time
    public static long C(long n, int k) {
        // Base Case
        if (k > n) {
            return 0;
        }
        long p = 1, q = 1;
 
        for (int i = 1; i <= k; i++) {
 
            // Update the value of p and q
            q = (q * i) % MOD;
            p = (p * (n - i + 1)) % MOD;
        }
 
        return modDivision(p, q);
    }
 
    // Function to find the count of arrays
    // having K as the first element satisfying
    // the given criteria
    public static long countArrays(int N, int K) {
        // Stores the resultant count of arrays
        long res = 1;
 
        // Find the factorization of K
        for (int p = 2; p <= K / p; p++) {
 
            int c = 0;
 
            // Stores the count of the exponent
            // of the currentprime factor
            while (K % p == 0) {
                K /= p;
                c++;
            }
            res = (res * C(N - 1 + c, c)) % MOD;
        }
        if (N > 1) {
 
            // N is one last prime factor,
            // for c = 1 -> C(N-1+1, 1) = N
            res = (res * N) % MOD;
        }
 
        // Return the total count
        return res;
    }
 
    // Driver Code
    public static void main(String args[]) {
        int N = 3, K = 5;
        System.out.println(countArrays(N, K));
 
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# python 3 program for the above approach
MOD = 1000000007
 
from math import sqrt
 
# Find the value of x raised to the
# yth power modulo MOD
def modPow(x, y):
   
    # Stores the value of x^y
    r = 1
    a = x
 
    # Iterate until y is positive
    while(y > 0):
        if ((y & 1) == 1):
            r = (r * a) % MOD
        a = (a * a) % MOD
        y /= 2
 
    return r
# Function to perform the Modular
# Multiplicative Inverse using the
# Fermat's little theorem
def modInverse(x):
    return modPow(x, MOD - 2)
 
# Modular division x / y, find
# modular multiplicative inverse
# of y and multiply by x
def modDivision(p, q):
    return (p * modInverse(q)) % MOD
 
# Function to find Binomial Coefficient
# C(n, k) in O(k) time
def C(n, k):
    # Base Case
    if (k > n):
        return 0
 
    p = 1
    q = 1
 
    for i in range(1,k+1,1):
 
        # Update the value of p and q
        q = (q * i) % MOD
        p = (p * (n - i + 1)) % MOD
 
    return modDivision(p, q)
 
# Function to find the count of arrays
# having K as the first element satisfying
# the given criteria
def countArrays(N, K):
    # Stores the resultant count of arrays
    res = 1
 
    # Find the factorization of K
    for p in range(2,int(sqrt(K)),1):
        c = 0
 
        # Stores the count of the exponent
        # of the currentprime factor
        while (K % p == 0):
            K /= p
            c += 1
        res = (res * C(N - 1 + c, c)) % MOD
    if (N > 1):
 
        # N is one last prime factor,
        # for c = 1 -> C(N-1+1, 1) = N
        res = (res * N) % MOD
 
    # Return the total count
    return res
 
# Driver Code
if __name__ == '__main__':
    N = 3
    K = 5
    print(countArrays(N, K))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
public class GFG
{   
      public static int MOD = 1000000007;
 
    // Find the value of x raised to the
    // yth power modulo MOD
    public static long modPow(long x, long y)
    {
       
        // Stores the value of x^y
        long r = 1, a = x;
 
        // Iterate until y is positive
        while (y > 0) {
            if ((y & 1) == 1) {
                r = (r * a) % MOD;
            }
            a = (a * a) % MOD;
            y /= 2;
        }
 
        return r;
    }
 
    // Function to perform the Modular
    // Multiplicative Inverse using the
    // Fermat's little theorem
    public static long modInverse(long x) {
        return modPow(x, MOD - 2);
    }
 
    // Modular division x / y, find
    // modular multiplicative inverse
    // of y and multiply by x
    public static long modDivision(long p, long q) {
        return (p * modInverse(q)) % MOD;
    }
 
    // Function to find Binomial Coefficient
    // C(n, k) in O(k) time
    public static long C(long n, int k) {
        // Base Case
        if (k > n) {
            return 0;
        }
        long p = 1, q = 1;
 
        for (int i = 1; i <= k; i++) {
 
            // Update the value of p and q
            q = (q * i) % MOD;
            p = (p * (n - i + 1)) % MOD;
        }
 
        return modDivision(p, q);
    }
 
    // Function to find the count of arrays
    // having K as the first element satisfying
    // the given criteria
    public static long countArrays(int N, int K) {
        // Stores the resultant count of arrays
        long res = 1;
 
        // Find the factorization of K
        for (int p = 2; p <= K / p; p++) {
 
            int c = 0;
 
            // Stores the count of the exponent
            // of the currentprime factor
            while (K % p == 0) {
                K /= p;
                c++;
            }
            res = (res * C(N - 1 + c, c)) % MOD;
        }
        if (N > 1) {
 
            // N is one last prime factor,
            // for c = 1 -> C(N-1+1, 1) = N
            res = (res * N) % MOD;
        }
 
        // Return the total count
        return res;
    }
 
    // Driver Code
    static public void Main (){
 
        int N = 3, K = 5;
        Console.WriteLine(countArrays(N, K));
    }
}
 
// This code is contributed by Dharanendra L V.


Javascript


输出:
3

时间复杂度: O(sqrt(N))
辅助空间: O(1)