Recuento de pares de Fibonacci que satisfacen la ecuación dada

Dada una array arr[] que contiene Q enteros positivos y dos números A y B , la tarea es encontrar el número de pares ordenados (x, y) para cada número N , en la array arr , tal que: 
 

Ejemplos: 
 

Entrada: arr[] = {15, 25}, A = 1, B = 2 
Salida: 2 1 
Explicación: 
Para 15: Hay 2 pares ordenados (x, y) = (13, 1) & (5, 5) tal que 1*x + 2*y = 15 
Para 25: Solo hay un par ordenado (x, y) = (21, 2) tal que 1*x + 2*y = 25
Entrada: arr[] = {50 , 150}, A = 5, B = 10 
Salida: 1 0 
 

Enfoque ingenuo: El enfoque ingenuo para este problema es: 
 

  • Para cada consulta N en la array, calcule los números de Fibonacci hasta N
  • Compruebe todos los pares posibles, a partir de estos números de Fibonacci, si satisface la condición dada A*x + B*y = N.

Complejidad temporal: O(N 2 )
Enfoque eficiente: 
 

  • Calcule previamente todos los números de Fibonacci y guárdelos en una array.
  • Ahora, itere sobre los números de Fibonacci y para todas las combinaciones posibles, actualice el número de pares ordenados para cada número en el rango [1, max(arr)] y guárdelo en otra array.
  • Ahora, para cada consulta N, el número de pares ordenados se puede responder en tiempo constante.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to find the count of
// Fibonacci pairs (x, y) which
// satisfy the equation Ax+By=N
 
#include <bits/stdc++.h>
#define size 10001
using namespace std;
 
// Array to store the Fibonacci numbers
long long fib[100010];
 
// Array to store the number of ordered pairs
int freq[100010];
 
// Function to find if a number
// is a perfect square
bool isPerfectSquare(int x)
{
    int s = sqrt(x);
    return (s * s == x);
}
 
// Function that returns 1
// if N is non-fibonacci number else 0
int isFibonacci(int n)
{
    // N is Fibonacci if one of
    // 5*n*n + 4 or 5*n*n - 4 or both
    // are perfect square
    if (isPerfectSquare(5 * n * n + 4)
        || isPerfectSquare(5 * n * n - 4))
        return 1;
    return 0;
}
 
// Function to store the fibonacci numbers
// and their frequency in form a * x + b * y
void compute(int a, int b)
{
    // Storing the Fibonacci numbers
    for (int i = 1; i < 100010; i++) {
        fib[i] = isFibonacci(i);
    }
 
    // For loop to find all the possible
    // combinations of the Fibonacci numbers
    for (int x = 1; x < 100010; x++) {
        for (int y = 1; y < size; y++) {
 
            // Finding the number of ordered pairs
            if (fib[x] == 1 && fib[y] == 1
                && a * x + b * y < 100010) {
                freq[a * x + b * y]++;
            }
        }
    }
}
 
// Driver code
int main()
{
    int Q = 2, A = 5, B = 10;
    compute(A, B);
    int arr[Q] = { 50, 150 };
 
    // Find the ordered pair for every query
    for (int i = 0; i < Q; i++) {
        cout << freq[arr[i]] << " ";
    }
 
    return 0;
}

Java

// Java program to find the count of
// Fibonacci pairs (x, y) which
// satisfy the equation Ax+By=N
class GFG{
     
static final int size = 10001;
 
// Array to store the Fibonacci numbers
static long []fib = new long[100010];
  
// Array to store the number of ordered pairs
static int []freq = new int[100010];
  
// Function to find if a number
// is a perfect square
static boolean isPerfectSquare(int x)
{
    int s = (int) Math.sqrt(x);
    return (s * s == x);
}
  
// Function that returns 1
// if N is non-fibonacci number else 0
static int isFibonacci(int n)
{
    // N is Fibonacci if one of
    // 5*n*n + 4 or 5*n*n - 4 or both
    // are perfect square
    if (isPerfectSquare(5 * n * n + 4)
        || isPerfectSquare(5 * n * n - 4))
        return 1;
    return 0;
}
  
// Function to store the fibonacci numbers
// and their frequency in form a * x + b * y
static void compute(int a, int b)
{
    // Storing the Fibonacci numbers
    for (int i = 1; i < 100010; i++) {
        fib[i] = isFibonacci(i);
    }
  
    // For loop to find all the possible
    // combinations of the Fibonacci numbers
    for (int x = 1; x < 100010; x++) {
        for (int y = 1; y < size; y++) {
  
            // Finding the number of ordered pairs
            if (fib[x] == 1 && fib[y] == 1
                && a * x + b * y < 100010) {
                freq[a * x + b * y]++;
            }
        }
    }
}
  
// Driver code
public static void main(String[] args)
{
    int Q = 2, A = 5, B = 10;
    compute(A, B);
    int arr[] = { 50, 150 };
  
    // Find the ordered pair for every query
    for (int i = 0; i < Q; i++) {
        System.out.print(freq[arr[i]]+ " ");
    }
}
}
 
// This code is contributed by PrinciRaj1992

Python 3

# Python program to find the count of
# Fibonacci pairs (x, y) which
# satisfy the equation Ax+By=N
import math
size = 101
 
# Array to store the Fibonacci numbers
fib = [0]*100010
 
# Array to store the number of ordered pairs
freq = [0]*(100010)
 
# Function to find if a number
# is a perfect square
def isPerfectSquare(x):
    s = int(math.sqrt(x))
     
    return (s * s) == x
 
# Function that returns 1
# if N is non-fibonacci number else 0
def isFibonacci(n):
     
    # N is Fibonacci if one of
    # 5*n*n + 4 or 5*n*n - 4 or both
    # are perfect square
    if (isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4)):
        return 1;
    return 0;
 
# Function to store the fibonacci numbers
# and their frequency in form a * x + b * y
def compute( a, b):
 
    # Storing the Fibonacci numbers
    for i in range(1, 100010):
        fib[i] = isFibonacci(i)
 
    # For loop to find all the possible
    # combinations of the Fibonacci numbers
    for x in range(1, 100010):
        for y in range(1, size):
 
            # Finding the number of ordered pairs
            if (fib[x] == 1 and fib[y] == 1 and a * x + b * y < 100010):
                freq[a * x + b * y] += 1
             
# Driver code
 
Q = 2
A = 5
B = 10
compute(A, B);
arr = [ 50, 150 ]
 
# Find the ordered pair for every query
for i in range(Q):
        print(freq[arr[i]], end=" ")
         
# This code is contributed by ANKITKUMAR34

C#

// C# program to find the count of
// Fibonacci pairs (x, y) which
// satisfy the equation Ax+By=N
using System;
 
class GFG{
      
static readonly int size = 10001;
  
// Array to store the Fibonacci numbers
static long []fib = new long[100010];
   
// Array to store the number of ordered pairs
static int []freq = new int[100010];
   
// Function to find if a number
// is a perfect square
static bool isPerfectSquare(int x)
{
    int s = (int) Math.Sqrt(x);
    return (s * s == x);
}
   
// Function that returns 1
// if N is non-fibonacci number else 0
static int isFibonacci(int n)
{
    // N is Fibonacci if one of
    // 5*n*n + 4 or 5*n*n - 4 or both
    // are perfect square
    if (isPerfectSquare(5 * n * n + 4)
        || isPerfectSquare(5 * n * n - 4))
        return 1;
    return 0;
}
   
// Function to store the fibonacci numbers
// and their frequency in form a * x + b * y
static void compute(int a, int b)
{
    // Storing the Fibonacci numbers
    for (int i = 1; i < 100010; i++) {
        fib[i] = isFibonacci(i);
    }
   
    // For loop to find all the possible
    // combinations of the Fibonacci numbers
    for (int x = 1; x < 100010; x++) {
        for (int y = 1; y < size; y++) {
   
            // Finding the number of ordered pairs
            if (fib[x] == 1 && fib[y] == 1
                && a * x + b * y < 100010) {
                freq[a * x + b * y]++;
            }
        }
    }
}
   
// Driver code
public static void Main(String[] args)
{
    int Q = 2, A = 5, B = 10;
    compute(A, B);
    int []arr = { 50, 150 };
   
    // Find the ordered pair for every query
    for (int i = 0; i < Q; i++) {
        Console.Write(freq[arr[i]]+ " ");
    }
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
 
// Javascript program to find the count of
// Fibonacci pairs (x, y) which
// satisfy the equation Ax+By=N
var size = 10001
 
// Array to store the Fibonacci numbers
var fib = Array(100010).fill(0);
 
// Array to store the number of ordered pairs
var freq = Array(100010).fill(0);
 
// Function to find if a number
// is a perfect square
function isPerfectSquare(x)
{
    var s = parseInt(Math.sqrt(x));
    return (s * s == x);
}
 
// Function that returns 1
// if N is non-fibonacci number else 0
function isFibonacci(n)
{
 
    // N is Fibonacci if one of
    // 5*n*n + 4 or 5*n*n - 4 or both
    // are perfect square
    if (isPerfectSquare(5 * n * n + 4)
        || isPerfectSquare(5 * n * n - 4))
        return 1;
    return 0;
}
 
// Function to store the fibonacci numbers
// and their frequency in form a * x + b * y
function compute(a, b)
{
 
    // Storing the Fibonacci numbers
    for (var i = 1; i < 100010; i++)
    {
        fib[i] = isFibonacci(i);
    }
 
    // For loop to find all the possible
    // combinations of the Fibonacci numbers
    for (var x = 1; x < 100010; x++)
    {
        for (var y = 1; y < size; y++)
        {
 
            // Finding the number of ordered pairs
            if (fib[x] == 1 && fib[y] == 1
                && a * x + b * y < 100010)
            {
                freq[a * x + b * y]++;
            }
        }
    }
}
 
// Driver code
var Q = 2, A = 5, B = 10;
compute(A, B);
var arr = [ 50, 150 ];
 
// Find the ordered pair for every query
for (var i = 0; i < Q; i++)
{
    document.write(freq[arr[i]] + " ");
}
 
// This code is contributed by rutvik_56.
 
</script>
Producción: 

1 0

 

Complejidad de tiempo: O (tamaño + 100010)

Espacio auxiliar: O (tamaño + 100010)

Publicación traducida automáticamente

Artículo escrito por spp____ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *