📌  相关文章
📜  与斐波那契数之差至少为 K 的数字计数

📅  最后修改于: 2021-09-03 03:52:19             🧑  作者: Mango

先决条件:二分搜索
给定两个正整数NK ,任务是计算满足以下条件的所有数字:
如果数字是num

  • 数量≤ N。
  • abs(num – count) ≥ K其中count是最多num的斐波那契数的计数。

例子:

观察:在仔细观察,这是数字的差异,斐波那契数的计数高达这个数字是特定K A单调递增函数的函数。此外,如果数字X是有效数字,那么X + 1也将是有效数字。
证明:

方法:因此,从上面的观察来看,想法是使用散列来预先计算并存储斐波那契节点直到最大值,并使用前缀和数组概念创建一个前缀数组,其中每个索引存储小于 ‘ 的斐波那契数i’ 使检查变得简单有效(在 O(1) 时间内)。
现在,我们可以使用二分查找来找到最小有效数字X ,因为范围 [ X , N ] 中的所有数字都是有效的。因此,答案是N – X + 1
下面是上述方法的实现:

C++
// C++ program to find the count
// of numbers whose difference with
// Fibonacci count upto them is atleast K
 
#include 
using namespace std;
 
const int MAX = 1000005;
 
// fibUpto[i] denotes the count of
// fibonacci numbers upto i
int fibUpto[MAX + 1];
 
// Function to compute all the Fibonacci
// numbers and update fibUpto array
void compute(int sz)
{
    bool isFib[sz + 1];
    memset(isFib, false, sizeof(isFib));
 
    // Store the first two Fibonacci numbers
    int prev = 0, curr = 1;
    isFib[prev] = isFib[curr] = true;
 
    // Compute the Fibonacci numbers
    // and store them in isFib array
    while (curr <= sz) {
        int temp = curr + prev;
        isFib[temp] = true;
        prev = curr;
        curr = temp;
    }
 
    // Compute fibUpto array
    fibUpto[0] = 1;
    for (int i = 1; i <= sz; i++) {
        fibUpto[i] = fibUpto[i - 1];
        if (isFib[i])
            fibUpto[i]++;
    }
}
 
// Function to return the count
// of valid numbers
int countOfNumbers(int N, int K)
{
 
    // Compute fibUpto array
    compute(N);
 
    // Binary search to find the minimum
    // number that follows the condition
    int low = 1, high = N, ans = 0;
    while (low <= high) {
        int mid = (low + high) >> 1;
 
        // Check if the number is
        // valid, try to reduce it
        if (mid - fibUpto[mid] >= K) {
            ans = mid;
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
 
    // Ans is the minimum valid number
    return (ans ? N - ans + 1 : 0);
}
 
// Driver Code
int main()
{
    int N = 10, K = 3;
 
    cout << countOfNumbers(N, K);
}


Java
// Java program to find the count
// of numbers whose difference with
// Fibonacci count upto them is atleast K
import java.util.*;
 
class GFG{
  
static int MAX = 1000005;
  
// fibUpto[i] denotes the count of
// fibonacci numbers upto i
static int []fibUpto = new int[MAX + 1];
  
// Function to compute all the Fibonacci
// numbers and update fibUpto array
static void compute(int sz)
{
    boolean []isFib = new boolean[sz + 1];
  
    // Store the first two Fibonacci numbers
    int prev = 0, curr = 1;
    isFib[prev] = isFib[curr] = true;
  
    // Compute the Fibonacci numbers
    // and store them in isFib array
    while (curr <= sz) {
        int temp = curr + prev;
        if(temp <= sz)
            isFib[temp] = true;
        prev = curr;
        curr = temp;
    }
  
    // Compute fibUpto array
    fibUpto[0] = 1;
    for (int i = 1; i <= sz; i++) {
        fibUpto[i] = fibUpto[i - 1];
        if (isFib[i])
            fibUpto[i]++;
    }
}
  
// Function to return the count
// of valid numbers
static int countOfNumbers(int N, int K)
{
  
    // Compute fibUpto array
    compute(N);
  
    // Binary search to find the minimum
    // number that follows the condition
    int low = 1, high = N, ans = 0;
    while (low <= high) {
        int mid = (low + high) >> 1;
  
        // Check if the number is
        // valid, try to reduce it
        if (mid - fibUpto[mid] >= K) {
            ans = mid;
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
  
    // Ans is the minimum valid number
    return (ans>0 ? N - ans + 1 : 0);
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 10, K = 3;
  
    System.out.print(countOfNumbers(N, K));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python 3 program to find the count
# of numbers whose difference with
# Fibonacci count upto them is atleast K
 
MAX = 1000005
 
# fibUpto[i] denotes the count of
# fibonacci numbers upto i
fibUpto = [0]*(MAX + 1)
 
# Function to compute all the Fibonacci
# numbers and update fibUpto array
def compute(sz):
 
    isFib = [False]*(sz + 1)
    # Store the first two Fibonacci numbers
    prev = 0
    curr = 1
    isFib[prev] = True
    isFib[curr] = True
 
    # Compute the Fibonacci numbers
    # and store them in isFib array
    while (curr <=sz):
        temp = curr + prev
        if(temp<=sz):
            isFib[temp] = True
        prev = curr
        curr = temp
 
    # Compute fibUpto array
    fibUpto[0] = 1
    for i in range( 1,sz+1):
        fibUpto[i] = fibUpto[i - 1]
        if (isFib[i]):
            fibUpto[i]+=1
 
# Function to return the count
# of valid numbers
def countOfNumbers(N, K):
 
    # Compute fibUpto array
    compute(N)
 
    # Binary search to find the minimum
    # number that follows the condition
    low , high, ans = 1, N, 0
    while (low <= high):
        mid = (low + high) >> 1
 
        # Check if the number is
        # valid, try to reduce it
        if (mid - fibUpto[mid] >= K):
            ans = mid
            high = mid - 1
        else:
            low = mid + 1
 
    # Ans is the minimum valid number
    if(ans):
        return (N - ans + 1)
    return 0
     
# Driver Code
if __name__ == "__main__":
     
    N = 10
    K = 3
 
    print(countOfNumbers(N, K))
 
# This code is contributed by chitranayal


C#
// C# program to find the count
// of numbers whose difference with
// Fibonacci count upto them is atleast K
using System;
  
class GFG{
   
static int MAX = 1000005;
   
// fibUpto[i] denotes the count of
// fibonacci numbers upto i
static int []fibUpto = new int[MAX + 1];
   
// Function to compute all the Fibonacci
// numbers and update fibUpto array
static void compute(int sz)
{
    bool []isFib = new bool[sz + 1];
   
    // Store the first two Fibonacci numbers
    int prev = 0, curr = 1;
    isFib[prev] = isFib[curr] = true;
   
    // Compute the Fibonacci numbers
    // and store them in isFib array
    while (curr <= sz) {
        int temp = curr + prev;
        if(temp <= sz)
            isFib[temp] = true;
        prev = curr;
        curr = temp;
    }
   
    // Compute fibUpto array
    fibUpto[0] = 1;
    for (int i = 1; i <= sz; i++) {
        fibUpto[i] = fibUpto[i - 1];
        if (isFib[i])
            fibUpto[i]++;
    }
}
   
// Function to return the count
// of valid numbers
static int countOfNumbers(int N, int K)
{
   
    // Compute fibUpto array
    compute(N);
   
    // Binary search to find the minimum
    // number that follows the condition
    int low = 1, high = N, ans = 0;
    while (low <= high) {
        int mid = (low + high) >> 1;
   
        // Check if the number is
        // valid, try to reduce it
        if (mid - fibUpto[mid] >= K) {
            ans = mid;
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
   
    // Ans is the minimum valid number
    return (ans>0 ? N - ans + 1 : 0);
}
   
// Driver Code
public static void Main()
{
    int N = 10, K = 3;
   
    Console.WriteLine(countOfNumbers(N, K));
}
}
  
// This code is contributed by Mohitkumar29


Javascript


输出:
2

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live