📜  每对两个数组构成的修正斐波那契数列的n个项的总和

📅  最后修改于: 2021-04-29 17:01:24             🧑  作者: Mango

给定两个大小为m的数组A和B。您必须找到第一个A元素和第二个B元素组成的斐波那契数列的第n个项之和(每个项的值是前两个项的和)。
例子:

Input : {1, 2, 3}, {4, 5, 6}, n = 3
Output : 63
Explanation : 
A[] = {1, 2, 3};
B[] = {4, 5, 6};
n = 3;
All the possible series upto 3rd terms are: 
We have considered every possible pair of A 
and B and generated third term using sum of
previous two terms.
1, 4, 5
1, 5, 6
1, 6, 7
2, 4, 6
2, 5, 7
2, 6, 8
3, 4, 7
3, 5, 8
3, 6, 9
sum = 5+6+7+6+7+8+7+8+9 = 63

Input : {5, 8, 10}, {6, 89, 5}
Output : 369

天真的方法是将阵列A和B的每一对取一个斐波那契数列。
一种有效的方法基于以下思想。

通过使用有效的方法,可以将其写为

original_fib[]={0, 1, 1, 2, 3, 5, 8};
A[] = {1, 2, 3};
B[] = {4, 5, 6};
n = 3;
for (i to m)
    sum = sum + 3*(B[i]*original_fib[2]) + 3*(A[i]*original_fib[1])

下面是上述方法的实现:

C++
// CPP program to find sum of n-th terms
// of a Fibonacci like series formed using
// first two terms of two arrays.
#include 
using namespace std;
 
int sumNth(int A[], int B[], int m, int n)
{
 
    int res = 0;
 
    // if sum of first term is required
    if (n == 1) {
        for (int i = 0; i < m; i++)
            res = res + A[i];
    }
 
    // if sum of second term is required
    else if (n == 2) {
        for (int i = 0; i < m; i++)
            res = res + B[i] * m;
    }
 
    else {
        // fibonacci series used to find the
        // nth term of every series
        int f[n];
        f[0] = 0, f[1] = 1;
        for (int i = 2; i < n; i++)
            f[i] = f[i - 1] + f[i - 2];
 
        for (int i = 0; i < m; i++) {
 
            // as every b[i] term appears m times and
            // every a[i] term also appears m times
            res = res + (m * (B[i] * f[n - 1])) +
                        (m * (A[i] * f[n - 2]));
        }
    }
 
    return res;
}
 
int main()
{
    // m is the size of the array
    int A[] = { 1, 2, 3 };
    int B[] = { 4, 5, 6 };
    int n = 3;
    int m = sizeof(A)/sizeof(A[0]);
    cout << sumNth(A, B, m, n);
    return 0;
}


Java
// Java program to find sum of n-th terms
// of a Fibonacci like series formed using
// first two terms of two arrays.
 
public class GFG {
 
    static int sumNth(int A[], int B[], int m, int n)
    {
 
        int res = 0;
 
        // if sum of first term is required
        if (n == 1) {
            for (int i = 0; i < m; i++)
                res = res + A[i];
        }
 
        // if sum of second term is required
        else if (n == 2) {
            for (int i = 0; i < m; i++)
                res = res + B[i] * m;
        }
 
        else {
            // fibonacci series used to find the
            // nth term of every series
            int f[] = new int[n];
            f[0] = 0;
            f[1] = 1;
            for (int i = 2; i < n; i++)
                f[i] = f[i - 1] + f[i - 2];
 
            for (int i = 0; i < m; i++) {
 
                // as every b[i] term appears m times and
                // every a[i] term also appears m times
                res = res + (m * (B[i] * f[n - 1])) +
                            (m * (A[i] * f[n - 2]));
            }
        }
 
        return res;
    }
 
 
    public static void main(String args[])
    {
         // m is the size of the array
        int A[] = { 1, 2, 3 };
        int B[] = { 4, 5, 6 };
        int n = 3;
        int m = A.length;
        System.out.println(sumNth(A, B, m, n));
 
    }
    // This code is contributed by ANKITRAI1
}


Python3
# Python3 program to find sum of
# n-th terms of a Fibonacci like
# series formed using first two
# terms of two arrays.
def sumNth(A, B, m, n):
 
    res = 0;
 
    # if sum of first term is required
    if (n == 1):
        for i in range(m):
            res = res + A[i];
 
    # if sum of second term is required
    elif (n == 2):
        for i in range(m):
            res = res + B[i] * m;
 
    else:
         
        # fibonacci series used to find
        # the nth term of every series
        f = [0] * n;
        f[0] = 0;
        f[1] = 1;
        for i in range(2, n):
            f[i] = f[i - 1] + f[i - 2];
 
        for i in range(m):
 
            # as every b[i] term appears m
            # times and every a[i] term also
            # appears m times
            res = (res + (m * (B[i] * f[n - 1])) +
                         (m * (A[i] * f[n - 2])));
 
    return res;
 
# Driver code
     
# m is the size of the array
A = [1, 2, 3 ];
B = [4, 5, 6 ];
n = 3;
m = len(A);
print(sumNth(A, B, m, n));
 
# This code is contributed by mits


C#
// C# program to find sum of
// n-th terms of a Fibonacci
// like series formed using
// first two terms of two arrays.
using System;
 
class GFG
{
static int sumNth(int[] A, int[] B,
                  int m, int n)
{
 
    int res = 0;
 
    // if sum of first term is required
    if (n == 1)
    {
        for (int i = 0; i < m; i++)
            res = res + A[i];
    }
 
    // if sum of second term is required
    else if (n == 2)
    {
        for (int i = 0; i < m; i++)
            res = res + B[i] * m;
    }
 
    else
    {
        // fibonacci series used to find
        // the nth term of every series
        int[] f = new int[n];
        f[0] = 0;
        f[1] = 1;
        for (int i = 2; i < n; i++)
            f[i] = f[i - 1] + f[i - 2];
 
        for (int i = 0; i < m; i++)
        {
 
            // as every b[i] term appears m
            // times and every a[i] term also
            // appears m times
            res = res + (m * (B[i] * f[n - 1])) +
                        (m * (A[i] * f[n - 2]));
        }
    }
 
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
    // m is the size of the array
    int[] A = { 1, 2, 3 };
    int[] B = { 4, 5, 6 };
    int n = 3;
    int m = A.Length;
    Console.WriteLine(sumNth(A, B, m, n));
}
}
 
// This code is contributed
// by Kirti_Mangal


PHP


Javascript


输出:
63