📌  相关文章
📜  给定长度的序列,其中每个元素都大于或等于前一个元素的两倍

📅  最后修改于: 2021-09-16 11:13:53             🧑  作者: Mango

给定两个整数 m & n,找出长度为 n 的可能序列的数量,使得每个下一个元素都大于或等于前一个元素的两倍但小于或等于 m。

例子 :

Input : m = 10, n = 4
Output : 4
There should be n elements and value of last
element should be at-most m.
The sequences are {1, 2, 4, 8}, {1, 2, 4, 9},
                 {1, 2, 4, 10}, {1, 2, 5, 10}

Input : m = 5, n = 2
Output : 6
The sequences are {1, 2}, {1, 3}, {1, 4},
                  {1, 5}, {2, 4}, {2, 5}

根据给定条件,序列的第 n 个值最多为 m。第 n 个元素可能有两种情况:

  1. 如果是m,则第(n-1)个元素至多是m/2。我们对 m/2 和 n-1 重复。
  2. 如果不是m,那么它最多是m-1。我们对 (m-1) 和 n 重复。

序列总数为包含m的序列数与不包含m的序列数之和。因此,原始的寻找长度为 n 且最大值为 m 的序列数的问题可以细分为独立的子问题,即寻找长度为 n 且最大值为 m-1 的序列数和长度为 n-1 的序列数为最大值为 m/ 2.

C++
// C++ program to count total number
// of special sequences of length n where
#include 
using namespace std;
 
// Recursive function to find the number of special
// sequences
int getTotalNumberOfSequences(int m, int n)
{
     
    // A special sequence cannot exist if length
    // n is more than the maximum value m.
    if (m < n)
        return 0;
  
    // If n is 0, found an empty special sequence
    if (n == 0)
        return 1;
  
    // There can be two possibilities : (1) Reduce
    // last element value (2) Consider last element
    // as m and reduce number of terms
    return getTotalNumberOfSequences(m - 1, n) +
           getTotalNumberOfSequences(m / 2, n - 1);
}  
 
// Driver code
int main()
{
    int m = 10;
    int n = 4;
    cout << "Total number of possible sequences "
         << getTotalNumberOfSequences(m, n);
    return 0;
}
 
// This code is contributed by shivanisinghss2110


Java
// Java program to count total number
// of special sequences of length n where
class Sequences
{
    // Recursive function to find the number of special
    // sequences
    static int  getTotalNumberOfSequences(int m, int n)
    {
        // A special sequence cannot exist if length
        // n is more than the maximum value m.
        if(m < n)
            return 0;
      
        // If n is 0, found an empty special sequence
        if(n == 0)
            return 1;
      
        // There can be two possibilities : (1) Reduce
        // last element value (2) Consider last element
        // as m and reduce number of terms
        return getTotalNumberOfSequences (m-1, n) +
               getTotalNumberOfSequences (m/2, n-1);
    }  
     
    // main function
    public static void main (String[] args)
    {
        int m = 10;
        int n = 4;
        System.out.println("Total number of possible sequences "+
                       getTotalNumberOfSequences(m, n));
    }
}


Python3
#Python3 program to count total number of
#special sequences of length n where
#Recursive function to find the number of
# special sequences
def getTotalNumberOfSequences(m,n):
 
    #A special sequence cannot exist if length
    #n is more than the maximum value m.
    if m


C#
// C# program to count total number
// of special sequences of length n
// where every element is more than
// or equal to twice of previous
using System;
 
class GFG
{
    // Recursive function to find
    // the number of special sequences
    static int getTotalNumberOfSequences(int m, int n)
    {
        // A special sequence cannot exist if length
        // n is more than the maximum value m.
        if(m < n)
            return 0;
     
        // If n is 0, found an empty special sequence
        if(n == 0)
            return 1;
     
        // There can be two possibilities : (1) Reduce
        // last element value (2) Consider last element
        // as m and reduce number of terms
        return getTotalNumberOfSequences (m-1, n) +
               getTotalNumberOfSequences (m/2, n-1);
    }
     
    // Driver code
    public static void Main ()
    {
        int m = 10;
        int n = 4;
        Console.Write("Total number of possible sequences " +
                           getTotalNumberOfSequences(m, n));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


C++
// C program to count total number of special sequences
// of length N where
#include 
 
// DP based function to find the number of special
// sequences
int  getTotalNumberOfSequences(int m, int n)
{
        // define T and build in bottom manner to store
        // number of special sequences of length n and
        // maximum value m
        int T[m+1][n+1];
        for (int i=0; i


Java
// Efficient java program to count total number
// of special sequences of length n where
class Sequences
{
    // DP based function to find the number of special
    // sequences
    static int  getTotalNumberOfSequences(int m, int n)
    {
            // define T and build in bottom manner to store
            // number of special sequences of length n and
            // maximum value m
            int T[][]=new int[m+1][n+1];
            for (int i=0; i


Python3
#Python3 program to count total number of
#special sequences of length N where
 
#DP based function to find the number
# of special sequence
def getTotalNumberOfSequences(m,n):
 
    #define T and build in bottom manner to store
    #number of special sequences of length n and
    #maximum value m
    T=[[0 for i in range(n+1)] for i in range(m+1)]
    for i in range(m+1):
        for j in range(n+1):
 
            #Base case : If length of sequence is 0
            # or maximum value is 0, there cannot
            #exist any special sequence
            if i==0 or j==0:
                T[i][j]=0
 
            #if length of sequence is more than
            #the maximum value, special sequence
            # cannot exist
            elif i


C#
// Efficient C# program to count total number
// of special sequences of length n where
using System;
class Sequences {
     
    // DP based function to find
    // the number of special
    // sequences
    static int getTotalNumberOfSequences(int m, int n)
    {
         
            // define T and build in
            // bottom manner to store
            // number of special sequences
            // of length n and maximum value m
            int [,]T=new int[m + 1, n + 1];
             
            for (int i = 0; i < m + 1; i++)
            {
                for (int j = 0; j < n + 1; j++)
                {
                     
                    // Base case : If length
                    // of sequence is 0
                    // or maximum value is
                    // 0, there cannot
                    // exist any special
                    // sequence
                    if (i == 0 || j == 0)
                        T[i, j] = 0;
     
                    // if length of sequence
                    // is more than the maximum
                    // value, special sequence
                    // cannot exist
                    else if (i < j)
                        T[i,j] = 0;
     
                    // If length of sequence is 1 then the
                    // number of special sequences is equal
                    // to the maximum value
                    // For example with maximum value 2 and
                    // length 1, there can be 2 special
                    // sequences {1}, {2}
                    else if (j == 1)
                        T[i,j] = i;
     
                    // otherwise calculate
                    else
                        T[i,j] = T[i - 1, j] + T[i / 2, j - 1];
                }
            }
            return T[m,n];
    }
     
    // Driver Code
    public static void Main ()
    {
        int m = 10;
        int n = 4;
        Console.WriteLine("Total number of possible sequences "+
                                getTotalNumberOfSequences(m, n));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出:

Total number of possible sequences 4

请注意,上述函数一次又一次地计算相同的子问题。考虑以下 f(10, 4) 的树。

m=10 和 N=4 的递归树

我们可以使用动态规划来解决这个问题。

C++

// C program to count total number of special sequences
// of length N where
#include 
 
// DP based function to find the number of special
// sequences
int  getTotalNumberOfSequences(int m, int n)
{
        // define T and build in bottom manner to store
        // number of special sequences of length n and
        // maximum value m
        int T[m+1][n+1];
        for (int i=0; i

Java

// Efficient java program to count total number
// of special sequences of length n where
class Sequences
{
    // DP based function to find the number of special
    // sequences
    static int  getTotalNumberOfSequences(int m, int n)
    {
            // define T and build in bottom manner to store
            // number of special sequences of length n and
            // maximum value m
            int T[][]=new int[m+1][n+1];
            for (int i=0; i

蟒蛇3

#Python3 program to count total number of
#special sequences of length N where
 
#DP based function to find the number
# of special sequence
def getTotalNumberOfSequences(m,n):
 
    #define T and build in bottom manner to store
    #number of special sequences of length n and
    #maximum value m
    T=[[0 for i in range(n+1)] for i in range(m+1)]
    for i in range(m+1):
        for j in range(n+1):
 
            #Base case : If length of sequence is 0
            # or maximum value is 0, there cannot
            #exist any special sequence
            if i==0 or j==0:
                T[i][j]=0
 
            #if length of sequence is more than
            #the maximum value, special sequence
            # cannot exist
            elif i

C#

// Efficient C# program to count total number
// of special sequences of length n where
using System;
class Sequences {
     
    // DP based function to find
    // the number of special
    // sequences
    static int getTotalNumberOfSequences(int m, int n)
    {
         
            // define T and build in
            // bottom manner to store
            // number of special sequences
            // of length n and maximum value m
            int [,]T=new int[m + 1, n + 1];
             
            for (int i = 0; i < m + 1; i++)
            {
                for (int j = 0; j < n + 1; j++)
                {
                     
                    // Base case : If length
                    // of sequence is 0
                    // or maximum value is
                    // 0, there cannot
                    // exist any special
                    // sequence
                    if (i == 0 || j == 0)
                        T[i, j] = 0;
     
                    // if length of sequence
                    // is more than the maximum
                    // value, special sequence
                    // cannot exist
                    else if (i < j)
                        T[i,j] = 0;
     
                    // If length of sequence is 1 then the
                    // number of special sequences is equal
                    // to the maximum value
                    // For example with maximum value 2 and
                    // length 1, there can be 2 special
                    // sequences {1}, {2}
                    else if (j == 1)
                        T[i,j] = i;
     
                    // otherwise calculate
                    else
                        T[i,j] = T[i - 1, j] + T[i / 2, j - 1];
                }
            }
            return T[m,n];
    }
     
    // Driver Code
    public static void Main ()
    {
        int m = 10;
        int n = 4;
        Console.WriteLine("Total number of possible sequences "+
                                getTotalNumberOfSequences(m, n));
    }
}
 
// This code is contributed by anuj_67.

PHP


Javascript


输出:

4

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

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