# Sum of all possible triplet products from given ranges

• Last Updated : 27 May, 2021

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:
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).

Below is the implementation of the above approach.

## C++

 // C++ program to implement // the above approach   #include  using namespace std; #define M 1000000007   // Function to find the sum of all // possible triplet products (i * j * k) long long findTripleSum(long long A,                         long long B,                         long long C) {       // Stores sum required sum     long long sum = 0;       // Iterate over all     // possible values of i     for (long long i = 1; i <= A;          i++) {           // Iterate over all         // possible values of j         for (long long j = 1; j <= B;              j++) {               // Iterate over all             // possible values of k             for (long long k = 1; k <= C;                  k++) {                   // Stores the product                 // of (i * j *k)                 long long prod = (((i % M)                                      * (j % M))                                   % M                                   * (k % M))                                  % M;                   // Update sum                 sum = (sum + prod) % M;             }         }     }       return sum; }   // Driver Code int main() {       long long A = 10;     long long B = 100;     long long C = 1000;     cout << findTripleSum(A, B, C);       return 0; }

## Java

 // Java program to implement // the above approach import java.util.*;   class GFG{   static int M = 1000000007;   // Function to find the sum of all // possible triplet products (i * j * k) static int findTripleSum(int A, int B,                          int C) {           // Stores sum required sum     int sum = 0;       // Iterate over all     // possible values of i     for(int i = 1; i <= A; i++)      {                   // Iterate over all         // possible values of j         for(int j = 1; j <= B; j++)         {                           // Iterate over all             // possible values of k             for(int k = 1; k <= C; k++)             {                                   // Stores the product                 // of (i * j *k)                 int prod = (((i % M) * (j % M)) %                                     M * (k % M)) % M;                   // Update sum                 sum = (sum + prod) % M;             }         }     }     return sum; }   // Driver Code public static void main(String args[]) {     int A = 10;     int B = 100;     int C = 1000;           System.out.println(findTripleSum(A, B, C)); } }   // This code is contributed by bgangwar59

## Python3

 # Python3 program to implement # the above approach M = 1000000007   # Function to find the sum  # of all possible triplet # products (i * j * k) def findTripleSum(A, B, C):           # Stores sum required sum     sum = 0       # Iterate over all     # possible values of i     for i in range(1, A + 1):                   # Iterate over all         # possible values of j         for j in range(1, B + 1):                           # Iterate over all             # possible values of k             for k in range(1, C + 1):                                   # Stores the product                 # of (i * j *k)                 prod = (((i % M) * (j % M)) %                           M * (k % M)) % M                   # Update sum                 sum = (sum + prod) % M       return sum   # Driver Code if __name__ == '__main__':       A = 10     B = 100     C = 1000           print(findTripleSum(A, B, C))   # This code is contributed by mohit kumar 29

## C#

 // C# program to implement // the above approach   using System;   class GFG{    static int M = 1000000007;    // Function to find the sum of all // possible triplet products (i * j * k) static int findTripleSum(int A, int B,                          int C) {           // Stores sum required sum     int sum = 0;        // Iterate over all     // possible values of i     for(int i = 1; i <= A; i++)      {                   // Iterate over all         // possible values of j         for(int j = 1; j <= B; j++)         {                           // Iterate over all             // possible values of k             for(int k = 1; k <= C; k++)             {                                   // Stores the product                 // of (i * j *k)                 int prod = (((i % M) * (j % M)) %                                     M * (k % M)) % M;                    // Update sum                 sum = (sum + prod) % M;             }         }     }     return sum; }    // Driver Code public static void Main() {     int A = 10;     int B = 100;     int C = 1000;            Console.WriteLine(findTripleSum(A, B, C)); } }   // This code is contributed by code_hunt

## Javascript

 

Output:

13874027

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++

 // C++ implementation to implement // the above approach   #include  using namespace std; #define M 1000000007   // Function to find the value // of power(X, N) % M long long power(long long x,                 long long N) {       // Stores the value     // of (X ^ N) % M     long long res = 1;       // Calculate the value of     // power(x, N) % M     while (N > 0) {           // If N is odd         if (N & 1) {               // Update res             res = (res * x) % M;         }           // Update x         x = (x * x) % M;           // Update N         N = N >> 1;     }     return res; }   // Function to find modulo multiplicative // inverse of X under modulo M long long modinv(long long X) {     return power(X, M - 2); }   // Function to find the sum of all // possible triplet products (i * j * k) int findTripleSum(long long A, long long B,                   long long C) {       // Stores modulo multiplicative     // inverse of 8     long long MMI = modinv(8);       // Stores the sum of all     // possible values of (i * j * k)     long long res = 0;       // Update res     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; }   // Driver Code int main() {       long long A = 10;     long long B = 100;     long long C = 1000;     cout << findTripleSum(A, B, C);       return 0; }

## Java

 // Java implementation to implement // the above approach import java.util.*;   class GFG{     static final int M = 1000000007;   // Function to find the value // of power(X, N) % M static long power(long x, long N) {         // Stores the value     // of (X ^ N) % M     long res = 1;       // Calculate the value of     // power(x, N) % M     while (N > 0)     {                 // If N is odd         if (N % 2 == 1)         {                         // Update res             res = (res * x) % M;         }           // Update x         x = (x * x) % M;           // Update N         N = N >> 1;     }     return res; }   // Function to find modulo multiplicative // inverse of X under modulo M static long modinv(long X) {     return power(X, M - 2); }   // Function to find the sum of all // possible triplet products (i * j * k) static long findTripleSum(long A, long B,                           long C) {         // Stores modulo multiplicative     // inverse of 8     long MMI = modinv(8);       // Stores the sum of all     // possible values of (i * j * k)     long res = 0;       // Update res     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; }   // Driver Code public static void main(String[] args) {     long A = 10;     long B = 100;     long C = 1000;         System.out.print(findTripleSum(A, B, C)); } }   // This code is contributed by Rajput-Ji

## Python3

 # Python3 implementation to  # implement the above approach M = 1000000007   # Function to find the value # of power(X, N) % M def power(x,N):         global M           # Stores the value     # of (X ^ N) % M     res = 1       # Calculate the value of     # power(x, N) % M     while (N > 0):                 # If N is odd         if (N & 1):                         # Update res             res = (res * x) % M           # Update x         x = (x * x) % M           # Update N         N = N >> 1     return res   # Function to find modulo  # multiplicative inverse  # of X under modulo M def modinv(X):         return power(X, M - 2)   # Function to find the  # sum of all possible  # triplet products (i * j * k) def findTripleSum(A, B, C):         global M           # Stores modulo multiplicative     # inverse of 8     MMI = modinv(8)     # Stores the sum of all     # possible values of (i * j * k)     res = 0       # Update res     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   # Driver Code if __name__ == '__main__':         A = 10     B = 100     C = 1000     print(findTripleSum(A, B, C))   # This code is contributed by SURENDRA_GANGWAR

## C#

 // C# implementation to implement // the above approach using System;   class GFG{     static readonly int M = 1000000007;   // Function to find the value // of power(X, N) % M static long power(long x, long N) {           // Stores the value     // of (X ^ N) % M     long res = 1;       // Calculate the value of     // power(x, N) % M     while (N > 0)     {                   // If N is odd         if (N % 2 == 1)         {                         // Update res             res = (res * x) % M;         }           // Update x         x = (x * x) % M;           // Update N         N = N >> 1;     }     return res; }   // Function to find modulo multiplicative // inverse of X under modulo M static long modinv(long X) {     return power(X, M - 2); }   // Function to find the sum of all // possible triplet products (i * j * k) static long findTripleSum(long A, long B,                           long C) {         // Stores modulo multiplicative     // inverse of 8     long MMI = modinv(8);       // Stores the sum of all     // possible values of (i * j * k)     long res = 0;       // Update res     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; }   // Driver Code public static void Main(String[] args) {     long A = 10;     long B = 100;     long C = 1000;         Console.Write(findTripleSum(A, B, C)); } }   // This code is contributed by Amit Katiyar

Output:

13874027

Time Complexity: O(log2N), Where N = (A * B * C)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :