📌  相关文章
📜  Array 的子序列计数,乘积的最后一位为 K

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

Array 的子序列计数,乘积的最后一位为 K

给定一个大小为N的数组A[]和一个数字K 。求子序列元素乘积的最后一位为K的数组的子序列数。

例子:

方法:上述问题可以使用基于以下思想的递归来解决:

插图:

请按照以下步骤解决问题:

  • 基本情况如果p 等于 K返回1 ,否则返回0
  • 有两种选择,要么采用该元素,要么不采用。
    • 如果我们取元素,那么乘积p 会更新为 p*a[n-1] ,因为我们只是关心最后一位数字,所以我们可以将 p 更新为 p*a[n- 1 ] 的最后一位数字p 变为 p*a[n-1]%10
    • 其他选项是不采用元素,因此不要更新 p 并执行 n-1,以寻找其他可能性。
  • 由于我们需要此类子序列的总数,因此我们返回上述两个调用的总和。
  • 一个边缘情况是如果 K=1,那么我们将得到一个额外的子序列,它是一个空子序列,因为我们最初取 p=1,所以在一个空子序列中我们得到 p==k 并返回 1 .因此,当 K==1 时,我们从答案中减去 1。

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

高效方法:由于递归具有指数时间复杂度,因此可以使用动态编程进一步优化上述递归方法为此,我们将构建一个查找表并记住递归代码。

下面是实现 上述方法

C++
// C++ program for Count the number of
// sub-sequences of an  array where the last digit
// of the product of the subsequence is K.
#include 
using namespace std;
 
int dp[1001][10];
// recursive utility function
int countSubsequencesUtil(int n, int k, int p,
                          vector& a)
{
    // Base case
    if (n == 0) {
        if (p == k)
            return 1;
        else
            return 0;
    }
 
    // return pre calculated value from
    // look-up table
    if (dp[n][p] != -1)
        return dp[n][p];
 
    // return the total number obtained through
    // option1 and option2
    return dp[n][p]
           = countSubsequencesUtil(n - 1, k,
                                   (p * a[n - 1]) % 10, a)
             + countSubsequencesUtil(n - 1, k, p, a);
}
// Function to  Count the number of subsequences
int countSubsequences(vector& a, int k)
{
    // initialize the table with -1
    memset(dp, -1, sizeof(dp));
 
    int n = a.size();
    int ans = countSubsequencesUtil(n, k, 1, a);
 
    // if k is 1 return 1 less
    if (k == 1)
        return ans - 1;
 
    return ans;
}
 
// Driver Code
int main()
{
    vector a = { 2, 3, 4, 2 };
    int k = 2;
 
    cout << countSubsequences(a, k);
 
    return 0;
}


Java
// Java program to count the number of subsequences
// of an array where the last digit of the product
// of the subsequence is K
import java.io.*;
import java.util.*;
 
public class GFG {
 
  static int[][] dp = new int[1001][10];
 
  // recursive utility function
  static int countSubsequencesUtil(int n, int k, int p,
                                   int[] a)
  {
    // base case
    if (n == 0) {
      if (p == k)
        return 1;
      return 0;
    }
 
    // return pre calculated value from
    // look up table
    if (dp[n][p] != -1)
      return dp[n][p];
 
    // return the total no. obtained through
    // option1 and option2
    return dp[n][p]
      = countSubsequencesUtil(n - 1, k,
                              (p * a[n - 1]) % 10, a)
      + countSubsequencesUtil(n - 1, k, p, a);
  }
 
  // function to count the number of subsequences
  static int countSubsequences(int[] a, int k)
  {
 
    // initializing all elements of table with -1
    for (int i = 0; i <= 1000; i++) {
      for (int j = 0; j < 10; j++) {
        dp[i][j] = -1;
      }
    }
 
    int n = a.length;
    int ans = countSubsequencesUtil(n, k, 1, a);
 
    // if k is 1 return 1 less
    if (k == 1)
      return ans - 1;
 
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] a = { 2, 3, 4, 2 };
    int k = 2;
    System.out.print(countSubsequences(a, k));
  }
}
 
// This code is contributed by phasing17


Python3
# Python implementation of above approach
dp = [[-1]*10]*1001
 
# recursive utility function
def countSubsequencesUtil(n, k, p, a) :
     
    # Base case
    if (n == 0) :
        if (p == k):
            return 1
        else:
            return 0
     
    # return pre calculated value from
    # look-up table
    if (dp[n][p] != -1):
        return dp[n][p]
 
    # return the total number obtained through
    # option1 and option2
    dp[n][p] = (countSubsequencesUtil(n - 1, k,(p * a[n - 1]) % 10, a) +
                    countSubsequencesUtil(n - 1, k, p, a));
    return (dp[n][p]);
 
# Function to  Count the number of subsequences
def countSubsequences(a, k) :
 
    n = len(a)
    ans = countSubsequencesUtil(n, k, 1, a)
 
    # if k is 1 return 1 less
    if (k == 1) :
        return (ans - 1)
 
    return ans + 1
 
# Driver Code
a = [ 2, 3, 4, 2 ]
k = 2
 
print(countSubsequences(a, k))
 
# This code is contributed by sanjoy_62.


C#
// C# program for Count the number of
// sub-sequences of an  array where the last digit
// of the product of the subsequence is K.
using System;
class GFG {
 
    static int[, ] dp = new int[1001, 10];
    // recursive utility function
    static int countSubsequencesUtil(int n, int k, int p,
                                     int[] a)
    {
        // Base case
        if (n == 0) {
            if (p == k)
                return 1;
            else
                return 0;
        }
 
        // return pre calculated value from
        // look-up table
        if (dp[n, p] != -1)
            return dp[n, p];
 
        // return the total number obtained through
        // option1 and option2
        return dp[n, p]
            = countSubsequencesUtil(n - 1, k,
                                    (p * a[n - 1]) % 10, a)
              + countSubsequencesUtil(n - 1, k, p, a);
    }
    // Function to  Count the number of subsequences
    static int countSubsequences(int[] a, int k)
    {
        // initialize the table with -1
        for (int i = 0; i < 1001; i++) {
            for (int j = 0; j < 10; j++) {
                dp[i, j] = -1;
            }
        }
 
        int n = a.Length;
        int ans = countSubsequencesUtil(n, k, 1, a);
 
        // if k is 1 return 1 less
        if (k == 1)
            return ans - 1;
 
        return ans;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] a = { 2, 3, 4, 2 };
        int k = 2;
 
        Console.Write(countSubsequences(a, k));
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
4

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