在arr []和Q查询中以[L,R]的形式给出几何级数级数,其中L是范围的左边界, R是右边界。任务是在给定范围内找到“几何级数”元素的总和。
注意:范围为1-索引和1≤L,R≤N,其中N是ARR的大小。
例子:
Input: arr[] = {2, 4, 8, 16, 32, 64, 128, 256}, Q = [[2, 4], [2, 6], [5, 8]]
Output:
28
124
480
Explanation:
Range 1: arr = {4, 8, 16}. Therefore sum = 28
Range 2: arr = {4, 8, 16, 32, 64}. Therefore sum = 124
Range 3: arr = {32, 64, 128, 256}. Therefore sum = 480
Input: arr[] = {7, 7, 7, 7, 7, 7}, Q = [[1, 6], [2, 4], [3, 3]]
Output:
42
21
7
Explanation:
Range 1: arr = {7, 7, 7, 7, 7, 7}. Therefore sum = 42
Range 2: arr = {7, 7, 7}. Therefore sum = 21
Range 3: arr = {7}. Therefore sum = 7
方法:由于给定的序列是几何级数,因此可以很容易地通过两步轻松地求出和:
- 获取范围的第一个元素。
- 如果d = 1,则将d * k乘以它,否则将(d k – 1)/(d – 1)乘以它,其中d是GP的公共比率, k是该范围内的元素数。
例如:
假设a [i]是该范围的第一个元素, d是GP的公共比率, k是给定范围内的元素数。
则范围的总和为
= a[i] + a[i+1] + a[i+2] + ….. + a[i+k-1]
= a[i] + (a[i] * d) + (a[i] * d * d) + …. + (a[i] * dk)
= a[i] * (1 + d + … + dk)
= a[i] * (dk – 1)/(d – 1)
下面是上述方法的实现:
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)