📌  相关文章
📜  计算可能的 N 位数字,使每个数字连续出现的次数不超过给定的次数

📅  最后修改于: 2021-09-17 07:09:39             🧑  作者: Mango

给定一个整数N和一个数组maxDigit[] ,任务是计算所有不同的N位数字,以使数字i出现的次数不超过maxDigit[i]次。由于计数可能非常大,将其打印为模10 9 + 7

例子:

朴素的方法:最简单的方法是迭代所有N位数字并计算满足给定条件的那些数字。检查所有数字后,打印总计数模10 9 + 7

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

高效的方法:为了优化上述方法,思想是使用数字动态规划的概念。这个问题的 DP 状态解释如下:

  • 在 Digit-DP 中,想法是通过在每个位置放置一个数字[0, 9]从左到右构建一个数字。因此,要跟踪当前位置,就需要有一个位置状态。此状态将具有从0(N – 1) 的可能值。
  • 根据问题,数字i出现的次数不能超过maxDigit[i]连续次数,因此请跟踪先前填充的数字。因此,需要先前的状态。此状态将具有从09 的可能值。
  • 需要一个状态计数,它将提供一个数字可以连续出现的次数。此状态将具有从1maxDigit[i] 的可能值。

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

  • 第一个位置可以有任何数字,没有任何限制。
  • 从第二个位置开始,跟踪先前填充的数字及其可以连续出现的给定计数。
  • 如果相同的数字出现在下一个位置,则减少其计数,如果此计数为零,则只需在下一次递归调用中忽略此数字。
  • 如果下一个位置出现不同的数字,则根据maxDigit[] 中的给定值更新其计数。
  • 在上述每个递归调用中,当生成结果数时,然后增加该数的计数。
  • 完成上述步骤后,打印总计的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Macros for modulus
#define MOD 1000000007
 
// DP array for memoization
int dp[5005][12][12];
 
// Utility function to count N digit
// numbers with digit i not appearing
// more than max_digit[i] consecutively
int findCountUtil(int N, int maxDigit[],
                  int position = 0,
                  int previous = 0,
                  int count = 1)
{
   
    // If number with N digits
    // is generated
    if (position == N) {
        return 1;
    }
 
    // Create a reference variable
    int& ans = dp[position][previous][count];
 
    // Check if the current state is
    // already computed before
    if (ans != -1) {
        return ans;
    }
 
    // Initialize ans as zero
    ans = 0;
 
    for (int i = 0; i <= 9; ++i) {
 
        // Check if count of previous
        // digit has reached zero or not
        if (count == 0 && previous != i) {
 
            // Fill current position
            // only with digits that
            // are unequal to previous digit
            ans = (ans
                   + (findCountUtil(N, maxDigit,
                                    position + 1, i,
                                    maxDigit[i] - 1))
                         % MOD)
                  % MOD;
        }
 
        else if (count != 0) {
 
            // If by placing the same digit
            // as previous on the current
            // position, decrement count by 1
 
            // Else set the value of count
            // for this new digit
            // accordingly from max_digit[]
            ans = (ans
                   + (findCountUtil(
                         N, maxDigit, position + 1, i,
                         (previous == i && position != 0)
                             ? count - 1
                             : maxDigit[i] - 1))
                         % MOD)
                  % MOD;
        }
    }
    return ans;
}
 
// Function to count N digit numbers
// with digit i not appearing more
// than max_digit[i] consecutive times
void findCount(int N, int maxDigit[])
{
   
    // Stores the final count
    int ans = findCountUtil(N, maxDigit);
 
    // Print the total count
    cout << ans;
}
 
// Driver Code
int main()
{
    int N = 2;
    int maxDigit[10] = { 1, 1, 1, 1, 1,
                         1, 1, 1, 1, 1 };
 
    // Initialize the dp array with -1
    memset(dp, -1, sizeof(dp));
 
    // Function Call
    findCount(N, maxDigit);
    return 0;
}


Java
// Java program for the above approach 
import java.util.*;
 
class GFG{
         
// Macros for modulus
static int MOD = 1000000007;
 
// DP array for memoization
static int dp[][][] = new int[5005][12][12];
 
// Utility function to count N digit
// numbers with digit i not appearing
// more than max_digit[i] consecutively
static int findCountUtil(int N, int maxDigit[],
                         int position,
                         int previous,
                         int count)
{
     
    // If number with N digits
    // is generated
    if (position == N)
    {
        return 1;
    }
 
    // Create a reference variable
    int ans = dp[position][previous][count];
 
    // Check if the current state is
    // already computed before
    if (ans != -1)
    {
        return ans;
    }
 
    // Initialize ans as zero
    ans = 0;
 
    for(int i = 0; i <= 9; ++i)
    {
         
        // Check if count of previous
        // digit has reached zero or not
        if (count == 0 && previous != i)
        {
             
            // Fill current position
            // only with digits that
            // are unequal to previous digit
            ans = (ans + (findCountUtil(
                  N, maxDigit, position + 1, i,
                  maxDigit[i] - 1)) % MOD) % MOD;
        }
 
        else if (count != 0)
        {
             
            // If by placing the same digit
            // as previous on the current
            // position, decrement count by 1
 
            // Else set the value of count
            // for this new digit
            // accordingly from max_digit[]
            ans = (ans + (findCountUtil(
                  N, maxDigit, position + 1, i,
                  (previous == i && position != 0) ?
                  count - 1 : maxDigit[i] - 1)) % MOD) % MOD;
        }
    }
     
    return ans;
}
 
// Function to count N digit numbers
// with digit i not appearing more
// than max_digit[i] consecutive times
static void findCount(int N, int maxDigit[])
{
    int position = 0;
    int previous = 0;
    int count = 1;
     
    // Stores the final count
    int ans = findCountUtil(N, maxDigit, position,
                            previous, count);
 
    // Print the total count
    System.out.println(ans);
}
 
// Driver Code   
public static void main (String[] args)   
{   
    int N = 2;
    int[] maxDigit = { 1, 1, 1, 1, 1,
                       1, 1, 1, 1, 1 };
 
    // Initialize the dp array with -1
    // Fill each row with -1. 
    for(int[][] row : dp)
    {
        for(int[] rowColumn : row)
        {
            Arrays.fill(rowColumn, -1);
        }
    }
     
    // Function Call
    findCount(N, maxDigit);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
# Macros for modulus
 
# DP array for memoization
dp = [[[ -1 for i in range(5005)] for i in range(12) ] for i in range(12)]
 
# Utility function to count N digit
# numbers with digit i not appearing
# more than max_digit[i] consecutively
def findCountUtil(N, maxDigit, position ,previous ,count):
    global dp
     
    # If number with N digits
    # is generated
    if (position == N):
        return 1
 
    # Create a reference variable
    ans = dp[position][previous][count]
 
    # Check if the current state is
    # already computed before
    if (ans != -1):
        return ans
 
    # Initialize ans as zero
    ans = 0
    for i in range(10):
 
        # Check if count of previous
        # digit has reached zero or not
        if (count == 0 and previous != i):
 
            # Fill current position
            # only with digits that
            # are unequal to previous digit
            ans = (ans + (findCountUtil(N, maxDigit, position + 1, i, maxDigit[i] - 1)) % 1000000007)% 1000000007
        elif (count != 0):
 
            # If by placing the same digit
            # as previous on the current
            # position, decrement count by 1
 
            # Else set the value of count
            # for this new digit
            # accordingly from max_digit[]
            ans = (ans + (findCountUtil(N, maxDigit, position + 1, i, count - 1 if (previous == i and position != 0) else maxDigit[i] - 1)) % 1000000007)% 1000000007
 
    dp[position][previous][count] = ans
    return ans
 
# Function to count N digit numbers
# with digit i not appearing more
# than max_digit[i] consecutive times
def findCount(N, maxDigit):
     
    # Stores the final count
    ans = findCountUtil(N, maxDigit, 0, 0, 1)
 
    # Print the total count
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    N = 2
    maxDigit = [1, 1, 1, 1, 1,1, 1, 1, 1, 1]
 
    # Function Call
    findCount(N, maxDigit)
 
    # This code is contributed by mohit kumar 29


C#
// C# program for the above approach 
using System;
using System.Collections.Generic;
 
 
using System;
using System.Collections.Generic;
public class GFG{
         
// Macros for modulus
static int MOD = 1000000007;
 
// DP array for memoization
static int [,,]dp = new int[5005, 12, 12];
 
// Utility function to count N digit
// numbers with digit i not appearing
// more than max_digit[i] consecutively
static int findCountUtil(int N, int []maxDigit,
                         int position,
                         int previous,
                         int count)
{
     
    // If number with N digits
    // is generated
    if (position == N)
    {
        return 1;
    }
 
    // Create a reference variable
    int ans = dp[position, previous, count];
 
    // Check if the current state is
    // already computed before
    if (ans != -1)
    {
        return ans;
    }
 
    // Initialize ans as zero
    ans = 0;
 
    for(int i = 0; i <= 9; ++i)
    {
         
        // Check if count of previous
        // digit has reached zero or not
        if (count == 0 && previous != i)
        {
             
            // Fill current position
            // only with digits that
            // are unequal to previous digit
            ans = (ans + (findCountUtil(
                  N, maxDigit, position + 1, i,
                  maxDigit[i] - 1)) % MOD) % MOD;
        }
 
        else if (count != 0)
        {
             
            // If by placing the same digit
            // as previous on the current
            // position, decrement count by 1
 
            // Else set the value of count
            // for this new digit
            // accordingly from max_digit[]
            ans = (ans + (findCountUtil(
                  N, maxDigit, position + 1, i,
                  (previous == i && position != 0) ?
                  count - 1 : maxDigit[i] - 1)) % MOD) % MOD;
        }
    }   
    return ans;
}
 
// Function to count N digit numbers
// with digit i not appearing more
// than max_digit[i] consecutive times
static void findCount(int N, int []maxDigit)
{
    int position = 0;
    int previous = 0;
    int count = 1;
     
    // Stores the readonly count
    int ans = findCountUtil(N, maxDigit, position,
                            previous, count);
 
    // Print the total count
    Console.WriteLine(ans);
}
 
// Driver Code   
public static void Main(String[] args)   
{   
    int N = 2;
    int[] maxDigit = { 1, 1, 1, 1, 1,
                       1, 1, 1, 1, 1 };
 
    // Initialize the dp array with -1
    // Fill each row with -1. 
    for(int i = 0; i < dp.GetLength(0); i++)
    {
        for (int j = 0; j < dp.GetLength(1); j++)
        {
            for (int k = 0; k < dp.GetLength(2); k++)
                dp[i, j, k] = -1;
        }
    }
     
    // Function Call
    findCount(N, maxDigit);
}
}
 
// This code is contributed by 29AjayKumar


输出:
90

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