Sum of all N elements of the given series having X as its first integer
Last Updated :
01 Feb, 2023
Given two Integers N and X in which N is the total number of elements of the series that starts with X (say A). The ith element of the series Ai is defined as the sum of all the elements from A0 to Ai-1. The task is to find the sum of the N elements of the series.
Note: The answer can be very large. Therefore, print it in modulo 109 +7.
Examples:
Input: N = 5, X = 3
Output: 48
Explanation: The first 5 terms are: 3, 3, 6, 12, 24
The sum of these are (3 + 3 + 6 + 12 + 24) = 48.
Input: N = 4, X = 26
Output: 208
Approach: The problem can be solved based on the following mathematical idea:
Say, the ith term of the series is represented by Ai and sum till ith term is represented by Si .
As per the definition of the series we can say that Ai = Si-1 .
So,
A0. A0 = X and S0 = X = A0
A1 = A0 = S0 and S1 = A0 + A1 = A0 + A0 = 2*A0 [By substituting the value of S0]
A2 = S1 = 2*A0 and S2 = (A0 + A1) + A2 = 2*A0 + 2* A0 = 4*A0 = 22 * A0
So we can see that the sum of first N terms is X * 2N-1 .
Steps were taken to solve the problem:
- Calculate 2N-1 with arithmetic modulus.
- Multiply 2N-1 with X and print the answer in modulo by using the multiplication property of arithmetic modulo.
- (a*b)%mod = ((a % mod) * (b % mod)) % mod.
Below is the implementation of the above the approach.
C++
#include <iostream>
using namespace std;
#define ll long long
ll M = 1000000007 ;
ll power(ll a, ll n){
ll result = 1 ;
while (n > 0){
result = (result * a) % M ;
n-- ;
}
return result ;
}
ll Total_Sum(ll n, ll x){
ll r_pow = power(2, n-1) ;
ll ans = ((x % M) * r_pow ) % M ;
return ans ;
}
int main() {
ll N = 5, X = 3;
cout << Total_Sum(N, X);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
long N = 5 , X = 3 ;
Total_Sum(N, X);
}
static void Total_Sum( long n, long x)
{
final long M = 1000000007 ;
long r_pow = power( 2 , n - 1 );
long ans = ((x % M) * (r_pow % M)) % M;
System.out.println(ans);
}
static long power( long a, long n)
{
final long M = 1000000007 ;
long result = 1 ;
while (n > 0 ) {
if ((n & 1 ) == 1 )
result = (result * a) % M;
n >>= 1 ;
a = (a * a) % M;
}
return result;
}
}
|
Python3
M = 1000000007
def power(a, n):
result = 1
while (n > 0 ):
result = (result * a) % M
n - = 1
return result
def Total_Sum(n, x):
r_pow = power( 2 , n - 1 )
ans = ((x % M) * r_pow) % M
return ans
if __name__ = = "__main__" :
N = 5
X = 3
print (Total_Sum(N, X))
|
C#
using System;
public class GFG {
static void Total_Sum( long n, long x)
{
long M = 1000000007;
long r_pow = power(2, n - 1);
long ans = ((x % M) * (r_pow % M)) % M;
Console.WriteLine(ans);
}
static long power( long a, long n)
{
long M = 1000000007;
long result = 1;
while (n > 0) {
if ((n & 1) == 1)
result = (result * a) % M;
n >>= 1;
a = (a * a) % M;
}
return result;
}
static public void Main()
{
long N = 5, X = 3;
Total_Sum(N, X);
}
}
|
Javascript
<script>
let M = 1000000007;
function power(a, n) {
let result = 1;
while (n > 0) {
result = (result * a) % M;
n--;
}
return result;
}
function Total_Sum(n, x) {
let r_pow = Math.pow(2, n - 1);
let ans = ((x % M) * r_pow) % M;
return ans;
}
let N = 5, X = 3;
document.write(Total_Sum(N, X));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...