Sum of all possible triplet products from given ranges
Last Updated :
09 Mar, 2023
Given three integers A, B and C, the task is to find the value of the expression
Since the answer can be very large, print the answer modulo 109 + 7.
Examples:
Input: A = 1, B = 1, C = 2
Output: 3
Explanation: The value of the given expression is: (1 * 1 * 1 + 1 * 1 * 2) % (109 + 7) = 3
Therefore, the required output is 3.
Input: A = 10, B =100, C = 1000
Output: 13874027
Naive Approach: The simplest approach to solve this problem is to generate all possible triplets (i, j, k) and print the sum of all possible products (i * j * k) mod (109 + 7).
Algorithm:
Step 1: Initialize a constant integer M should have the value 1000000007.
Step 2: Create a function called “findTripleSum” that accepts three long long integers, A, B, and C, as inputs and outputs another long long integer.
Step 3: In order to store the necessary sum, initialize the long integer “sum” to 0.
Step 4: Use a for loop to iterate through i’s range of possible values, from 1 to A.
a. Use a second for loop inside the first one to run through all conceivable j values, from 1 to B.
1. Use a third for loop inside the second loop to run through all k’s potential values, from 1 to C.
a1. Calculate the (i * j * k) product and place the result in the long long integer variable “prod” inside the third loop.
a2. Update the “sum” variable by adding the “prod” variable modulo M.
Step 5: Return the final value of “sum”.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
long long findTripleSum( long long A,
long long B,
long long C)
{
long long sum = 0;
for ( long long i = 1; i <= A;
i++) {
for ( long long j = 1; j <= B;
j++) {
for ( long long k = 1; k <= C;
k++) {
long long prod = (((i % M)
* (j % M))
% M
* (k % M))
% M;
sum = (sum + prod) % M;
}
}
}
return sum;
}
int main()
{
long long A = 10;
long long B = 100;
long long C = 1000;
cout << findTripleSum(A, B, C);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int M = 1000000007 ;
static int findTripleSum( int A, int B,
int C)
{
int sum = 0 ;
for ( int i = 1 ; i <= A; i++)
{
for ( int j = 1 ; j <= B; j++)
{
for ( int k = 1 ; k <= C; k++)
{
int prod = (((i % M) * (j % M)) %
M * (k % M)) % M;
sum = (sum + prod) % M;
}
}
}
return sum;
}
public static void main(String args[])
{
int A = 10 ;
int B = 100 ;
int C = 1000 ;
System.out.println(findTripleSum(A, B, C));
}
}
|
Python3
M = 1000000007
def findTripleSum(A, B, C):
sum = 0
for i in range ( 1 , A + 1 ):
for j in range ( 1 , B + 1 ):
for k in range ( 1 , C + 1 ):
prod = (((i % M) * (j % M)) %
M * (k % M)) % M
sum = ( sum + prod) % M
return sum
if __name__ = = '__main__' :
A = 10
B = 100
C = 1000
print (findTripleSum(A, B, C))
|
C#
using System;
class GFG{
static int M = 1000000007;
static int findTripleSum( int A, int B,
int C)
{
int sum = 0;
for ( int i = 1; i <= A; i++)
{
for ( int j = 1; j <= B; j++)
{
for ( int k = 1; k <= C; k++)
{
int prod = (((i % M) * (j % M)) %
M * (k % M)) % M;
sum = (sum + prod) % M;
}
}
}
return sum;
}
public static void Main()
{
int A = 10;
int B = 100;
int C = 1000;
Console.WriteLine(findTripleSum(A, B, C));
}
}
|
Javascript
<script>
let M = 1000000007;
function findTripleSum(A, B, C)
{
let sum = 0;
for (let i = 1; i <= A; i++)
{
for (let j = 1; j <= B; j++)
{
for (let k = 1; k <= C; k++)
{
let prod = (((i % M) * (j % M)) %
M * (k % M)) % M;
sum = (sum + prod) % M;
}
}
}
return sum;
}
let A = 10;
let B = 100;
let C = 1000;
document.write(findTripleSum(A, B, C));
</script>
|
Time Complexity: O(A * B * C)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
Single summation:
Double summation:
= ((A * (A + 1) / 2) * (B * (B + 1) / 2))
Similarly, for triple summation:
= ((A * (A + 1) / 2) * (B * (B + 1) / 2) * (C * (C + 1) / 2))
Follow the steps below to solve the problem:
Finally, print the value of ((A * (A + 1) % M) * (B * (B + 1) % M) * (C * (C + 1) % M) * MMI) % M.
C++
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
long long power( long long x,
long long N)
{
long long res = 1;
while (N > 0) {
if (N & 1) {
res = (res * x) % M;
}
x = (x * x) % M;
N = N >> 1;
}
return res;
}
long long modinv( long long X)
{
return power(X, M - 2);
}
int findTripleSum( long long A, long long B,
long long C)
{
long long MMI = modinv(8);
long long res = 0;
res = ((((A % M * (A + 1) % M)
% M
* (B % M * (B + 1) % M)
% M)
% M
* (C % M * (C + 1) % M)
% M)
% M
* MMI)
% M;
return res;
}
int main()
{
long long A = 10;
long long B = 100;
long long C = 1000;
cout << findTripleSum(A, B, C);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int M = 1000000007 ;
static long power( long x, long N)
{
long res = 1 ;
while (N > 0 )
{
if (N % 2 == 1 )
{
res = (res * x) % M;
}
x = (x * x) % M;
N = N >> 1 ;
}
return res;
}
static long modinv( long X)
{
return power(X, M - 2 );
}
static long findTripleSum( long A, long B,
long C)
{
long MMI = modinv( 8 );
long res = 0 ;
res = ((((A % M * (A + 1 ) % M) % M *
(B % M * (B + 1 ) % M) % M) % M *
(C % M * (C + 1 ) % M) % M) % M *
MMI) % M;
return res;
}
public static void main(String[] args)
{
long A = 10 ;
long B = 100 ;
long C = 1000 ;
System.out.print(findTripleSum(A, B, C));
}
}
|
Python3
M = 1000000007
def power(x,N):
global M
res = 1
while (N > 0 ):
if (N & 1 ):
res = (res * x) % M
x = (x * x) % M
N = N >> 1
return res
def modinv(X):
return power(X, M - 2 )
def findTripleSum(A, B, C):
global M
MMI = modinv( 8 )
res = 0
res = ((((A % M * (A + 1 ) % M) % M *
(B % M * (B + 1 ) % M) % M) % M *
(C % M * (C + 1 ) % M) % M) % M *
MMI) % M
return res
if __name__ = = '__main__' :
A = 10
B = 100
C = 1000
print (findTripleSum(A, B, C))
|
C#
using System;
class GFG{
static readonly int M = 1000000007;
static long power( long x, long N)
{
long res = 1;
while (N > 0)
{
if (N % 2 == 1)
{
res = (res * x) % M;
}
x = (x * x) % M;
N = N >> 1;
}
return res;
}
static long modinv( long X)
{
return power(X, M - 2);
}
static long findTripleSum( long A, long B,
long C)
{
long MMI = modinv(8);
long res = 0;
res = ((((A % M * (A + 1) % M) % M *
(B % M * (B + 1) % M) % M) % M *
(C % M * (C + 1) % M) % M) % M *
MMI) % M;
return res;
}
public static void Main(String[] args)
{
long A = 10;
long B = 100;
long C = 1000;
Console.Write(findTripleSum(A, B, C));
}
}
|
Javascript
let M = 1000000007n
function power(x,N)
{
let res = 1n
while (N > 0n)
{
if ((N & 1n) == 1n)
res = (res * x) % M
x = (x * x) % M
N = N >> 1n
}
return res
}
function modinv(X)
{
return power(X, M - 2n)
}
function findTripleSum(A, B, C)
{
let MMI = modinv(8n)
let res = 0n
res = ((((A % M * (A + 1n) % M) % M *
(B % M * (B + 1n) % M) % M) % M *
(C % M * (C + 1n) % M) % M) % M *
MMI)% M
return res
}
let A = 10n
let B = 100n
let C = 1000n
console.log(findTripleSum(A, B, C))
|
Time Complexity: O(log2N), Where N = (A * B * C)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...