📜  给定范围内几何级数 (GP) 的元素总和

📅  最后修改于: 2021-09-03 13:54:36             🧑  作者: Mango

给定arr[] 中的 Geometric Progression 系列和[L, R]形式的Q查询,其中L是范围的左边界, R是右边界。任务是找到给定范围内几何级数元素的总和。

注意:范围是 1-indexed 且1 ≤ L, R ≤ N ,其中N是 arr 的大小。
例子:

方法:由于给定的序列是几何级数,因此可以通过两个步骤轻松有效地找到总和:

  1. 获取范围的第一个元素。
  2. 如果d = 1,则乘以d*k ,否则乘以(d k – 1)/(d – 1) ,其中d是 GP 的公比, k是范围内的元素数。

例如:
假设 a[i] 是范围的第一个元素, d是 GP 的公比, k是给定范围内的元素数。
那么范围的总和将是

下面是上述方法的实现:

C++
// C++ program to find the sum
// of elements of an GP in the
// given range
#include 
using namespace std;
 
// Function to find sum in the given range
int findSum(int arr[], int n,
            int left, int right)
{
     
    // Find the value of k
    int k = right - left + 1;
 
    // Find the common difference
    int d = arr[1] / arr[0];
 
    // Find the sum
    int ans = arr[left - 1];
     
    if (d == 1)
        ans = ans * d * k;
    else
        ans = ans * ((int)pow(d, k) - 1 /
                                 (d - 1));
         
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 4, 8, 16, 32,
                  64, 128, 256 };
    int queries = 3;
    int q[][2] = { { 2, 4 }, { 2, 6 },
                   { 5, 8 } };
     
    int n = sizeof(arr) / sizeof(arr[0]);
     
    for(int i = 0; i < queries; i++)
        cout << (findSum(arr, n, q[i][0], q[i][1]))
             << endl;
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program to find the sum
// of elements of an GP in the
// given range
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to find sum in the given range
static int findSum(int[] arr, int n,
                int left, int right)
{
     
    // Find the value of k
    int k = right - left + 1;
 
    // Find the common difference
    int d = arr[1] / arr[0];
 
    // Find the sum
    int ans = arr[left - 1];
     
    if (d == 1)
        ans = ans * d * k;
    else
        ans = ans * ((int)Math.pow(d, k) - 1 /
                                (d - 1));
         
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    int[] arr = { 2, 4, 8, 16, 32,
                64, 128, 256 };
    int queries = 3;
    int[][] q = { { 2, 4 }, { 2, 6 }, { 5, 8 } };
     
    int n = arr.length;
     
    for(int i = 0; i < queries; i++)
        System.out.println(findSum(arr, n, q[i][0],
                                        q[i][1]));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to
# find the sum of elements
# of an GP in the given range
 
# Function to find sum in the given range
def findSum(arr, n, left, right):
 
    # Find the value of k
    k = right - left + 1
 
    # Find the common difference
    d = arr[1] // arr[0]
 
    # Find the sum
    ans = arr[left - 1]
    if d == 1:
        ans = ans * d * k
    else:
        ans = ans * (d ** k - 1) // (d -1)
    return ans
 
# Driver code
if __name__ == '__main__':
    arr = [ 2, 4, 8, 16, 32, 64, 128, 256 ]
    queries = 3
    q = [[ 2, 4 ], [ 2, 6 ], [ 5, 8 ]]
    n = len(arr)
 
    for i in range(queries):
        print(findSum(arr, n, q[i][0], q[i][1]))


C#
// C# program to find the sum
// of elements of an GP in the
// given range
using System;
 
class GFG{
     
// Function to find sum in the given range
static int findSum(int[] arr, int n,
                   int left, int right)
{
     
    // Find the value of k
    int k = right - left + 1;
 
    // Find the common difference
    int d = arr[1] / arr[0];
 
    // Find the sum
    int ans = arr[left - 1];
     
    if (d == 1)
        ans = ans * d * k;
    else
        ans = ans * ((int)Math.Pow(d, k) - 1 /
                                      (d - 1));
         
    return ans;
}
 
// Driver Code
public static void Main(string []args)
{
    int[] arr = { 2, 4, 8, 16, 32,
                  64, 128, 256 };
                   
    int queries = 3;
    int[,] q = { { 2, 4 }, { 2, 6 }, { 5, 8 } };
     
    int n = arr.Length;
     
    for(int i = 0; i < queries; i++)
        Console.Write(findSum(arr, n, q[i, 0],
                                      q[i, 1]) + "\n");
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
28
124
480
  • 时间复杂度: O(Q)
  • 空间复杂度: O(1)

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