给定一个包含Q 个正整数和两个数字A和B的数组arr[] ,任务是在数组arr 中找到每个数字N的有序对(x, y)的数量,使得:
- 它们满足方程A*x + B*y = N ,并且
- x 和 y 是斐波那契数列。
例子:
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
天真方法:这个问题的天真方法是:
- 对于数组中的每个查询N ,计算最多 N 的斐波那契数
- 如果满足给定的条件A*x + B*y = N ,则从此斐波那契数检查所有可能的对。
时间复杂度: O(N 2 )
有效的方法:
- 预先计算所有斐波那契数并将其存储在数组中。
- 现在,迭代斐波那契数和所有可能的组合,更新范围 [1, max(arr)] 中每个数字的有序对数,并将其存储在另一个数组中。
- 现在,对于每个查询 N,可以在恒定时间内回答有序对的数量。
下面是上述方法的实现:
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[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 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[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 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
Javascript
输出:
1 0