Related Articles
Count of Fibonacci pairs which satisfy the given equation
• Difficulty Level : Hard
• Last Updated : 13 Apr, 2020

Given an array arr[] containing Q positive integers and two numbers A and B, the task is to find the number of ordered pairs (x, y) for every number N, in array arr, such that:

Examples:

Input: arr[] = {15, 25}, A = 1, B = 2
Output: 2 1
Explanation:
For 15: There are 2 ordered pairs (x, y) = (13, 1) & (5, 5) such that 1*x + 2*y = 15
For 25: There is only one ordered pair (x, y) = (21, 2) such that 1*x + 2*y = 25

Input: arr[] = {50, 150}, A = 5, B = 10
Output: 1 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The naive approach for this problem is:

• For every query N in the array, compute the Fibonacci numbers up to N
• Check for all possible pairs, from this Fibonacci numbers, if it satisfies the given condition A*x + B*y = N.

Time complexity: O(N2)

Efficient Approach:

• Precompute all the Fibonacci numbers and store it in an array.
• Now, iterate over the Fibonacci numbers and for all the possible combinations, update the number of ordered pairs for every number in range [1, max(arr)], and store it in another array.
• Now, for every query N, the number of ordered pairs can be answered in constant time.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the count of ` `// Fibonacci pairs (x, y) which ` `// satisfy the equation Ax+By=N ` ` `  `#include ` `#define size 10001 ` `using` `namespace` `std; ` ` `  `// Array to store the Fibonacci numbers ` `long` `long` `fib; ` ` `  `// Array to store the number of ordered pairs ` `int` `freq; ` ` `  `// 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 Fibinacci if one of ` `    ``// 5*n*n + 4 or 5*n*n - 4 or both ` `    ``// are perferct 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 Fibinacci if one of ` `    ``// 5*n*n + 4 or 5*n*n - 4 or both ` `    ``// are perferct 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 Fibinacci if one of ` `    ``# 5*n*n + 4 or 5*n*n - 4 or both ` `    ``# are perferct 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``; ` `   `  `// Array to store the number of ordered pairs ` `static` `int` `[]freq = ``new` `int``; ` `   `  `// 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 Fibinacci if one of ` `    ``// 5*n*n + 4 or 5*n*n - 4 or both ` `    ``// are perferct 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 `

Output:

```1 0
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :