在 [1, N] 范围内长度为 K 的序列的计数,其中每个元素都是其前一个元素的倍数
给定两个整数N和K,任务是从范围[1, N]中找到K个元素的序列计数,其中每个元素都是前一个元素的倍数。
例子:
Input: N = 4, K = 3
Output: 13
Explanation: The sequences that can be made from the integers 1, 2, 3, 4 having 3 elements are: {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {1, 1, 2}, {1, 2, 2}, {1, 2, 4}, {1, 1, 3}, {1, 3, 3}, {1, 1, 4}, {1, 4, 4}, {2, 2, 4}, and {2, 4, 4}.
Input: N = 9, K = 5
Output: 111
方法:给定的问题可以使用带有记忆的递归来解决。请按照以下步骤解决问题:
- 创建一个 2D 数组dp[][]存储记忆状态,其中dp[i][j]表示长度为i的序列的计数,其中j作为第一个元素。
- 创建一个递归函数countSequenceUtil() ,它将序列的长度和起始元素作为参数,将下一个元素设置为当前元素的倍数,并递归调用剩余的序列。
- 将计算状态的答案存储在dp[][]数组中,如果某些状态的值已经计算,则返回它。
- 创建一个函数countSequence() ,它遍历序列中所有可能的起始元素,并调用递归函数来计算具有该起始元素的K个元素的序列。
- 保持变量ans中每个起始元素的计算计数总和,这是所需的值。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Initialize the dp matrix
int static dp[1001][1001];
// Function to find the count of sequences of K
// elements with first element as m where every
// element is a multiple of the previous one
int countSequenceUtil(int k, int m, int n)
{
// Base case
if (k == 1) {
return 1;
}
// If the value already exists
// in the DP then return it
if (dp[k][m] != -1) {
return dp[k][m];
}
// Variable to store the count
int res = 0;
for (int i = 1; i <= (n / m); i++) {
// Recursive Call
res += countSequenceUtil(k - 1,
m * i, n);
}
// Store the calculated
// answer and return it
return dp[k][m] = res;
}
// Function to find count of sequences of K
// elements in the range [1, n] where every
// element is a multiple of the previous one
int countSequence(int N, int K)
{
// Initializing all values
// of dp with -1
memset(dp, -1, sizeof(dp));
// Variable to store
// the total count
int ans = 0;
// Iterate from 1 to N
for (int i = 1; i <= N; i++) {
ans += countSequenceUtil(K, i, N);
}
// Return ans
return ans;
}
// Driver Code
int main()
{
int N = 9;
int K = 5;
cout << countSequence(N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Initialize the dp matrix
static int dp[][] = new int[1001][1001];
// Function to find the count of sequences of K
// elements with first element as m where every
// element is a multiple of the previous one
static int countSequenceUtil(int k, int m, int n)
{
// Base case
if (k == 1) {
return 1;
}
// If the value already exists
// in the DP then return it
if (dp[k][m] != -1) {
return dp[k][m];
}
// Variable to store the count
int res = 0;
for (int i = 1; i <= (n / m); i++) {
// Recursive Call
res += countSequenceUtil(k - 1,
m * i, n);
}
// Store the calculated
// answer and return it
return dp[k][m] = res;
}
// Function to find count of sequences of K
// elements in the range [1, n] where every
// element is a multiple of the previous one
static int countSequence(int N, int K)
{
// Initializing all values
// of dp with -1
for(int i=0;i
Python3
# Python implementation for the above approach
# Initialize the dp matrix
dp = [[-1 for i in range(1001)] for j in range(1001)]
# Function to find the count of sequences of K
# elements with first element as m where every
# element is a multiple of the previous one
def countSequenceUtil(k, m, n):
# Base case
if (k == 1):
return 1
# If the value already exists
# in the DP then return it
if (dp[k][m] != -1):
return dp[k][m]
# Variable to store the count
res = 0
for i in range(1, (n // m) + 1):
# Recursive Call
res += countSequenceUtil(k - 1,
m * i, n)
# Store the calculated
# answer and return it
dp[k][m] = res
return dp[k][m]
# Function to find count of sequences of K
# elements in the range [1, n] where every
# element is a multiple of the previous one
def countSequence(N, K):
# Variable to store
# the total count
ans = 0
# Iterate from 1 to N
for i in range(1, N + 1):
ans += countSequenceUtil(K, i, N)
# Return ans
return ans
# Driver Code
N = 9
K = 5
print(countSequence(N, K))
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG {
// Initialize the dp matrix
static int[, ] dp = new int[1001, 1001];
// Function to find the count of sequences of K
// elements with first element as m where every
// element is a multiple of the previous one
static int countSequenceUtil(int k, int m, int n)
{
// Base case
if (k == 1) {
return 1;
}
// If the value already exists
// in the DP then return it
if (dp[k, m] != -1) {
return dp[k, m];
}
// Variable to store the count
int res = 0;
for (int i = 1; i <= (n / m); i++) {
// Recursive Call
res += countSequenceUtil(k - 1, m * i, n);
}
// Store the calculated
// answer and return it
return dp[k, m] = res;
}
// Function to find count of sequences of K
// elements in the range [1, n] where every
// element is a multiple of the previous one
static int countSequence(int N, int K)
{
// Initializing all values
// of dp with -1
for (int i = 0; i < dp.GetLength(0); i++) {
for (int j = 0; j < dp.GetLength(1); j++) {
dp[i, j] = -1;
}
}
// Variable to store
// the total count
int ans = 0;
// Iterate from 1 to N
for (int i = 1; i <= N; i++) {
ans += countSequenceUtil(K, i, N);
}
// Return ans
return ans;
}
// Driver Code
public static void Main(string[] args)
{
int N = 9;
int K = 5;
Console.WriteLine(countSequence(N, K));
}
}
// This code is contributed by ukasp.
Javascript
输出
111
时间复杂度: O(N*K*log N)
辅助空间: O(N*K)