📜  通过平方、加 1 或乘以 2 将 2 转换为 N 的方法计数

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

通过平方、加 1 或乘以 2 将 2 转换为 N 的方法计数

给定一个正数N ,任务是找到从 2 达到N的方法的数量,其中每个操作可以执行以下操作之一:

  • 将 1 添加到当前数字。
  • 将当前数字乘以 2。
  • 将当前数字平方。

例子:

方法:给定的问题可以通过使用动态规划有效地解决。这个想法是使用 DP 数组来计算从 2 到达N所需的方式数。将数组Dp从 2 迭代到N并在每次迭代后执行上述操作以计算到达N所需的方式数,即Dp [i] => Dp[i+1]Dp[i] => Dp[2 * i]Dp[i] => Dp[i * i] 。因此,将在上述所有三种状态下达到Dp[i]的方式数相加。存储在Dp[N]中的值是所需的答案。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
using namespace std;
 
// Function to calculate
// number of ways required
// to reach N from 2
int waysToReach(int N)
{
 
    // Initialize a DP array
    vector Dp(N + 1, 0);
 
    // Initialize a DP[2] by 1
    Dp[2] = 1;
 
    // Iterate the array from 2 to N
    for (int i = 2; i <= N; i++) {
 
        // If i+1 is not out of bounds
        if (i + 1 <= N) {
 
            // Add the number of ways
            Dp[i + 1] += Dp[i];
        }
 
        // If i*2 is not out of bounds
        if (i * 2 <= N) {
 
            // Add the number of ways
            Dp[i * 2] += Dp[i];
        }
 
        // If i*i is not out of bounds
        if (i * i <= N) {
 
            // Add the number of ways
            Dp[i * i] += Dp[i];
        }
    }
 
    // Return the answer
    return Dp[N];
}
 
// Driver code
int main()
{
    int N = 5;
    cout << waysToReach(N);
    return 0;
}
 
    // This code is contributed by rakeshsahni


Java
// Java implementation for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to calculate
    // number of ways required
    // to reach N from 2
    public static int waysToReach(int N)
    {
 
        // Initialize a DP array
        int[] Dp = new int[N + 1];
 
        // Initialize a DP[2] by 1
        Dp[2] = 1;
 
        // Iterate the array from 2 to N
        for (int i = 2; i <= N; i++) {
 
            // If i+1 is not out of bounds
            if (i + 1 <= N) {
 
                // Add the number of ways
                Dp[i + 1] += Dp[i];
            }
 
            // If i*2 is not out of bounds
            if (i * 2 <= N) {
 
                // Add the number of ways
                Dp[i * 2] += Dp[i];
            }
 
            // If i*i is not out of bounds
            if (i * i <= N) {
 
                // Add the number of ways
                Dp[i * i] += Dp[i];
            }
        }
 
        // Return the answer
        return Dp[N];
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int N = 5;
 
        System.out.println(waysToReach(N));
    }
}


Python3
# Python Program to implement
# the above approach
 
# Function to calculate
# number of ways required
# to reach N from 2
def waysToReach(N):
 
    # Initialize a DP array
    Dp = [0] * (N + 1)
 
    # Initialize a DP[2] by 1
    Dp[2] = 1
 
    # Iterate the array from 2 to N
    for i in range(2, N + 1):
 
        # If i+1 is not out of bounds
        if (i + 1 <= N):
            # Add the number of ways
            Dp[i + 1] += Dp[i]
 
        # If i*2 is not out of bounds
        if (i * 2 <= N):
            # Add the number of ways
            Dp[i * 2] += Dp[i]
 
        # If i*i is not out of bounds
        if (i * i <= N):
            # Add the number of ways
            Dp[i * i] += Dp[i]
 
    # Return the answer
    return Dp[N]
 
# Driver code
N = 5
print(waysToReach(N))
 
# This code is contributed by gfgking


C#
// C# program for above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
 
// Function to calculate
// number of ways required
// to reach N from 2
static int waysToReach(int N)
{
 
    // Initialize a DP array
    int []Dp = new int[N+1];
     
    for(int i = 0; i < N+1; i++) {
        Dp[i] = 0;
    }
 
    // Initialize a DP[2] by 1
    Dp[2] = 1;
 
    // Iterate the array from 2 to N
    for (int i = 2; i <= N; i++) {
 
        // If i+1 is not out of bounds
        if (i + 1 <= N) {
 
            // Add the number of ways
            Dp[i + 1] += Dp[i];
        }
 
        // If i*2 is not out of bounds
        if (i * 2 <= N) {
 
            // Add the number of ways
            Dp[i * 2] += Dp[i];
        }
 
        // If i*i is not out of bounds
        if (i * i <= N) {
 
            // Add the number of ways
            Dp[i * i] += Dp[i];
        }
    }
 
    // Return the answer
    return Dp[N];
}
 
// Driver Code
public static void Main()
{
    int N = 5;
 
    Console.Write(waysToReach(N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
3

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