📌  相关文章
📜  相邻数字的按位与等于 0 的 N 位数字的计数

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

给定一个正整数N ,任务是计算N位数字的数量,使得相邻数字的按位 AND 等于 0。

例子:

朴素方法:解决给定问题的最简单方法是迭代所有可能的 N 位数字,并计算相邻数字的按位 AND 为0 的那些数字。检查完所有数字后,打印count的值作为结果。

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

高效方法:上述方法也可以使用动态规划进行优化,因为上述问题具有重叠子问题和最优子结构。子问题可以利用记忆化被存储在DP [] []表,其中DP [位] [上]存储从数字答案位置,直到结束时,当所选择的前面的数字是一个先前。请按照以下步骤解决问题:

  • 通过执行以下步骤定义递归函数,例如countOfNumbers(digit, prev)
    • 如果digit的值等于N + 1,则在形成有效的N 位数字时返回1
    • 如果已经计算了状态dp[digit][prev]的结果,则返回此状态dp[digit][prev]
    • 如果当前数字1 ,则可以放置[1, 9] 中的任何数字。如果N = 1 ,则也可以放置0
    • 否则,遍历从i = 0 到 i = 9 的所有数字,并检查条件((i & prev) == 0) 是否有效,并相应地将满足‘i’值放在当前位置。
    • 进行有效放置后,递归调用索引(digit + 1)countOfNumbers函数。
    • 返回所有可能的有效数字位置的总和作为答案。
  • 打印函数countOfNumbers(1, 0, N)返回的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
int dp[100][10];
 
// Function to calculate count of 'N' digit
// numbers such that bitwise AND of adjacent
// digits is 0.
int countOfNumbers(int digit, int prev, int n)
{
    // If digit = n + 1, a valid
    // n-digit number has been formed
    if (digit == n + 1) {
        return 1;
    }
 
    // If the state has
    // already been computed
    int& val = dp[digit][prev];
    if (val != -1) {
        return val;
    }
    val = 0;
 
    // If current position is 1,
    // then any digit from [1-9] can be placed.
    // If n = 1, 0 can be also placed.
    if (digit == 1) {
        for (int i = (n == 1 ? 0 : 1); i <= 9; ++i) {
            val += countOfNumbers(digit + 1, i, n);
        }
    }
 
    // For remaining positions,
    // any digit from [0-9] can be placed
    // after checking the conditions.
    else {
        for (int i = 0; i <= 9; ++i) {
 
            // Check if bitwise AND
            // of current digit and
            // previous digit is 0.
            if ((i & prev) == 0) {
                val += countOfNumbers(digit + 1, i, n);
            }
        }
    }
    // Return answer
    return val;
}
 
// Driver code
int main()
{
    // Initialize dp array with -1.
    memset(dp, -1, sizeof dp);
 
    // Given Input
    int N = 3;
 
    // Function call
    cout << countOfNumbers(1, 0, N) << endl;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
 
static int dp[][] = new int[100][10];
 
// Function to calculate count of 'N' digit
// numbers such that bitwise AND of adjacent
// digits is 0.
static int countOfNumbers(int digit, int prev, int n)
{
     
    // If digit = n + 1, a valid
    // n-digit number has been formed
    if (digit == n + 1)
    {
        return 1;
    }
 
    // If the state has
    // already been computed
    int val = dp[digit][prev];
     
    if (val != -1)
    {
        return val;
    }
    val = 0;
 
    // If current position is 1,
    // then any digit from [1-9] can be placed.
    // If n = 1, 0 can be also placed.
    if (digit == 1)
    {
        for(int i = (n == 1 ? 0 : 1); i <= 9; ++i)
        {
            val += countOfNumbers(digit + 1, i, n);
        }
    }
 
    // For remaining positions,
    // any digit from [0-9] can be placed
    // after checking the conditions.
    else
    {
        for(int i = 0; i <= 9; ++i)
        {
             
            // Check if bitwise AND
            // of current digit and
            // previous digit is 0.
            if ((i & prev) == 0)
            {
                val += countOfNumbers(digit + 1, i, n);
            }
        }
    }
     
    // Return answer
    return val;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Initializing dp array with -1.
    for(int i = 0; i < 100; i++)
    {
        for(int j = 0; j < 10; j++)
        {
            dp[i][j] = -1;
        }
    }
 
    // Given Input
    int N = 3;
 
    // Function call
    System.out.println(countOfNumbers(1, 0, N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
dp = [[-1 for i in range(10)]
          for j in range(100)]
 
val = 0
 
# Function to calculate count of 'N' digit
# numbers such that bitwise AND of adjacent
# digits is 0.
def countOfNumbers(digit, prev, n):
     
    global val
    global dp
     
    # If digit = n + 1, a valid
    # n-digit number has been formed
    if (digit == n + 1):
        return 1
 
    # If the state has
    # already been computed
    val = dp[digit][prev]
    if (val != -1):
        return val
         
    val = 0
 
    # If current position is 1,
    # then any digit from [1-9] can be placed.
    # If n = 1, 0 can be also placed.
    if (digit == 1):
        i = 0 if n == 1 else 1
         
        while (i <= 9):
            val += countOfNumbers(digit + 1, i, n)
            i += 1
 
    # For remaining positions,
    # any digit from [0-9] can be placed
    # after checking the conditions.
    else:
        for i in range(10):
             
            # Check if bitwise AND
            # of current digit and
            # previous digit is 0.
            if ((i & prev) == 0):
                val += countOfNumbers(digit + 1, i, n)
 
    # Return answer
    return val
 
# Driver code
if __name__ == '__main__':
     
    # Given Input
    N = 3
 
    # Function call
    print(countOfNumbers(1, 0, N))
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG
{
 
  static int[,] dp = new int[100, 10];
 
  // Function to calculate count of 'N' digit
  // numbers such that bitwise AND of adjacent
  // digits is 0.
  static int countOfNumbers(int digit, int prev, int n)
  {
 
    // If digit = n + 1, a valid
    // n-digit number has been formed
    if (digit == n + 1)
    {
      return 1;
    }
 
    // If the state has
    // already been computed
    int val = dp[digit, prev];
 
    if (val != -1)
    {
      return val;
    }
    val = 0;
 
    // If current position is 1,
    // then any digit from [1-9] can be placed.
    // If n = 1, 0 can be also placed.
    if (digit == 1)
    {
      for(int i = (n == 1 ? 0 : 1); i <= 9; ++i)
      {
        val += countOfNumbers(digit + 1, i, n);
      }
    }
 
    // For remaining positions,
    // any digit from [0-9] can be placed
    // after checking the conditions.
    else
    {
      for(int i = 0; i <= 9; ++i)
      {
 
        // Check if bitwise AND
        // of current digit and
        // previous digit is 0.
        if ((i & prev) == 0)
        {
          val += countOfNumbers(digit + 1, i, n);
        }
      }
    }
 
    // Return answer
    return val;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
 
    // Initializing dp array with -1.
    for(int i = 0; i < 100; i++)
    {
      for(int j = 0; j < 10; j++)
      {
        dp[i, j] = -1;
      }
    }
 
    // Given Input
    int N = 3;
 
    // Function call
    Console.WriteLine(countOfNumbers(1, 0, N));
  }
}
 
// This code is contributed by avijitmondal1998.


Javascript


输出
264

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程