给定两个整数[L, R] ,任务是计算[L, R]范围内的Primonacci数的数量。
Primonacci Series:
F(1) = F(2) = 1
F(3) = 3 – F(3 – 2) = F(1) = 1
F(4) = F(4 – 2) + F(4 – 3) = F(2) + F(1) = 1 + 1 = 2
F(5) = F(5 – 2) + F(5 – 3) = F(3) + F(2) = 1 + 1 = 2
…
Nth Primonacci Number, F(N) = F(N – 2) + F(N – 3) + F(N – 5) + …. + F(N – K), where K denotes the nearest prime number smaller than N
例子:
Input: L = 1, R = 10
Output: 5
Explanation:
F(1) = 1
F(2) = 1
F(3) = 1
F(4) = 2
F(5) = 2
F(6) = F(6 – 2) + F(6 – 3) + F(6 – 5) = F(4) + F(3) + F(1) = 2 + 1 + 1 = 4
F(7) = F(7 – 2) + F(7 – 3) + F(7 – 5) = F(5) + F(4) + F(2) = 2 + 2 + 1 = 5
F(8) = F(8 – 2) + F(8 – 3) + F(8 – 5) + F(8 – 7) = F(6) + F(5) + F(3) + F(1) = 4 + 2 + 1 + 1 = 8
Therefore, distinct primonacci numbers are {1, 2, 4, 5, 8}.
Input: L = 6, R = 50
Output: 6
方法:
该问题可以使用动态规划和埃拉托色尼筛法来解决。请按照以下步骤解决问题:
- 使用埃拉托色尼筛法生成所有素数。
- 初始化一个 HashSet 来存储不同的 Primonacci 数。
- 初始化一个数组dp[] ,使得dp[i]存储第i个Primonacci 数。
- 设置dp[1] = dp[2] = 1 。
- 对于每个i ,迭代所有小于 i 的素数p并通过添加dp[i – p]不断更新dp[i ] 。
- 如果dp[i]在[L, R]范围内,则插入到HashSet 中。
- 最后,如果dp[i]超过R ,则打印HashSet的大小。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Stores list of all primes
static vector primes;
int M = 100005;
// Function to find all primes
void sieve()
{
// To mark the prime ones
bool mark[M];
for(int i = 0; i < M; i++)
mark[i] = false;
// Initially all indices as prime
for(int i = 2; i < M; i++)
mark[i] = true;
for(int i = 2; i * i < M; i++)
{
// If i is prime
if (mark[i])
{
// Set all multiples
// of i as non-prime
for(int j = i * i;
j < M; j += i)
mark[j] = false;
}
}
// Adding all primes to a list
for(int i = 2; i < M; i++)
if (mark[i])
primes.push_back(i);
}
// Function to return the count of
// Primonacci Numbers in the range [l, r]
void countPrimonacci(int l, int r)
{
// dp[i] contains ith Primonacci Number
vector dp;
dp.push_back(1);
dp.push_back(1);
int i = 2;
// Stores the Primonacci Numbers
set s;
while (true)
{
int x = 0;
// Iterate over all smaller primes
for(int j = 0; j < primes.size(); j++)
{
int p = primes[j];
if (p >= i)
break;
x += dp[i - p];
}
// If Primonacci number lies
// within the range [L, R]
if (x >= l && x <= r)
s.insert(x);
if (x > r)
break;
dp.push_back(x);
i++;
}
// Count of Primonacci Numbers
cout << s.size();
}
// Driver Code
int main()
{
sieve();
int L = 1, R = 10;
countPrimonacci(L, R);
}
// This code is contributed by bgangwar59
Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG {
// Stores list of all primes
static ArrayList primes;
static int M = 100005;
// Function to find all primes
static void sieve()
{
primes = new ArrayList<>();
// To mark the prime ones
boolean mark[] = new boolean[M];
// Initially all indices as prime
for (int i = 2; i < M; i++)
mark[i] = true;
for (int i = 2; i * i < M; i++) {
// If i is prime
if (mark[i]) {
// Set all multiples
// of i as non-prime
for (int j = i * i;
j < M; j += i)
mark[j] = false;
}
}
// Adding all primes to a list
for (int i = 2; i < M; i++)
if (mark[i])
primes.add(i);
}
// Function to return the count of
// Primonacci Numbers in the range [l, r]
static void countPrimonacci(int l, int r)
{
// dp[i] contains ith Primonacci Number
ArrayList dp = new ArrayList<>();
dp.add(1);
dp.add(1);
int i = 2;
// Stores the Primonacci Numbers
HashSet s = new HashSet<>();
while (true) {
int x = 0;
// Iterate over all smaller primes
for (int j = 0; j < primes.size();
j++) {
int p = primes.get(j);
if (p >= i)
break;
x += dp.get(i - p);
}
// If Primonacci number lies
// within the range [L, R]
if (x >= l && x <= r)
s.add(x);
if (x > r)
break;
dp.add(x);
i++;
}
// Count of Primonacci Numbers
System.out.println(s.size());
}
// Driver Code
public static void main(String[] args)
{
sieve();
int L = 1, R = 10;
countPrimonacci(L, R);
}
}
Python3
# Python3 implementation of
# the above approach
M = 100005
# Stores list of all primes
primes = []
# Function to find all primes
def sieve():
# To mark the prime ones
mark = [False] * M
# Initially all indices as prime
for i in range(2, M):
mark[i] = True
i = 2
while i * i < M:
# If i is prime
if(mark[i]):
# Set all multiples
# of i as non-prime
j = i * i
while j < M:
mark[j] = False
j += i
i += 1
# Adding all primes to a list
for i in range(2, M):
if(mark[i]):
primes.append(i)
# Function to return the count of
# Primonacci Numbers in the range [l, r]
def countPrimonacci(l, r):
# dp[i] contains ith Primonacci Number
dp = []
dp.append(1)
dp.append(1)
i = 2
# Stores the Primonacci Numbers
s = set()
while(True):
x = 0
# Iterate over all smaller primes
for j in range(len(primes)):
p = primes[j]
if(p >= i):
break
x += dp[i - p]
# If Primonacci number lies
# within the range [L, R]
if(x >= l and x <= r):
s.add(x)
if(x > r):
break
dp.append(x)
i += 1
# Count of Primonacci Numbers
print(len(s))
# Driver Code
if __name__ == '__main__':
sieve()
L, R = 1, 10
countPrimonacci(L, R)
# This code is contributed by Shivam Singh
C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Stores list of all primes
static List primes;
static int M = 100005;
// Function to find all primes
static void sieve()
{
primes = new List();
// To mark the prime ones
bool[] mark = new bool[M];
// Initially all indices as prime
for (int i = 2; i < M; i++)
mark[i] = true;
for (int i = 2; i * i < M; i++)
{
// If i is prime
if (mark[i])
{
// Set all multiples
// of i as non-prime
for (int j = i * i; j < M; j += i)
mark[j] = false;
}
}
// Adding all primes to a list
for (int i = 2; i < M; i++)
if (mark[i])
primes.Add(i);
}
// Function to return the count of
// Primonacci Numbers in the range [l, r]
static void countPrimonacci(int l, int r)
{
// dp[i] contains ith Primonacci Number
List dp = new List();
dp.Add(1);
dp.Add(1);
int i = 2;
// Stores the Primonacci Numbers
HashSet s = new HashSet();
while (true)
{
int x = 0;
// Iterate over all smaller primes
for (int j = 0; j < primes.Count; j++)
{
int p = primes[j];
if (p >= i)
break;
x += dp[i - p];
}
// If Primonacci number lies
// within the range [L, R]
if (x >= l && x <= r)
s.Add(x);
if (x > r)
break;
dp.Add(x);
i++;
}
// Count of Primonacci Numbers
Console.WriteLine(s.Count);
}
// Driver Code
public static void Main(String[] args)
{
sieve();
int L = 1, R = 10;
countPrimonacci(L, R);
}
}
// This code is contributed by shikhasingrajput
Javascript
5
时间复杂度: O(N * P),其中 P 是直到 R 的素数的数量
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。