Given an array arr[] of integers of size n. For each element, you have to print the summation of multiplication of every triplet formed using divisors of this element.
Examples:
Input: arr[] = {4}
Output: 8
4 has three divisors 1, 2 and 4.
1 * 2 * 4 = 8Input: arr[] = {9, 5, 6}
Output: 27 0 72
9 has three divisors 1, 3 and 9. 1 * 3 * 9 = 27
5 has two divisors 1 and 5. So, no triplet is formed .
Similarly, 6 has four divisors 1, 2, 3 and 6. (1 * 2 * 3) + (1 * 3 * 6) + (2 * 3 * 6) + (1 * 6 * 2) = 72
Naive approach: Store all the divisors of a number and apply brute force method and for every triplet, add multiplication of these three elements to the answer.
Efficient approach: This method is similar to Sieve of Eratosthenes, we use for finding all prime numbers of a range.
- First of all we make an array sum1 which stores the sum of all divisors of number x at sum1[x]. So first iterate through all the numbers less than maximum_Element and add this number to the sum of divisors of multiple of this number. Hence we will be able to store sum of all divisors of any number at that number’s position.
- After filling sum1 array, its time to fill second array sum2 which will store the sum of multiplication of every pair divisor of number x at sum2[x].For filling this we go for every number similar to step1 and add multiplication of this number with to its higher values.
- For filling sum3 array we will go similarly for all numbers less than max_Element and will add sum of multiplication of divisors of j such that i is a divisor of j.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;#define ll long longconst ll max_Element = 1e6 + 5; // Global array declarationint sum1[max_Element], sum2[max_Element], sum3[max_Element]; // Function to find the sum of multiplication of// every triplet in the divisors of a numbervoid precomputation(int arr[], int n){ // sum1[x] represents the sum of all the divisors of x for (int i = 1; i < max_Element; i++) for (int j = i; j < max_Element; j += i) // Adding i to sum1[j] because i // is a divisor of j sum1[j] += i; // sum2[x] represents the sum of all the divisors of x for (int i = 1; i < max_Element; i++) for (int j = i; j < max_Element; j += i) // Here i is divisor of j and sum1[j] - i // represents sum of all divisors of // j which do not include i so we add // i * (sum1[j] - i) to sum2[j] sum2[j] += (sum1[j] - i) * i; // In the above implementation we have considered // every pair two times so we have to divide // every sum2 array element by 2 for (int i = 1; i < max_Element; i++) sum2[i] /= 2; // Here i is the divisor of j and we are trying to // add the sum of multiplication of all triplets of // divisors of j such that one of the divisors is i for (int i = 1; i < max_Element; i++) for (int j = i; j < max_Element; j += i) sum3[j] += i * (sum2[j] - i * (sum1[j] - i)); // In the above implementation we have considered // every triplet three times so we have to divide // every sum3 array element by 3 for (int i = 1; i < max_Element; i++) sum3[i] /= 3; // Print the results for (int i = 0; i < n; i++) cout << sum3[arr[i]] << " ";} // Driver codeint main(){ int arr[] = { 9, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); // Precomputing precomputation(arr, n); return 0;} |
Java
// Java implementation of the approachclass GFGq { static int max_Element = (int) (1e6 + 5); // Global array declarationstatic int sum1[] = new int[max_Element], sum2[] = new int[max_Element], sum3[] = new int[max_Element]; // Function to find the sum of multiplication of// every triplet in the divisors of a numberstatic void precomputation(int arr[], int n){ // sum1[x] represents the sum of all the divisors of x for (int i = 1; i < max_Element; i++) for (int j = i; j < max_Element; j += i) // Adding i to sum1[j] because i // is a divisor of j sum1[j] += i; // sum2[x] represents the sum of all the divisors of x for (int i = 1; i < max_Element; i++) for (int j = i; j < max_Element; j += i) // Here i is divisor of j and sum1[j] - i // represents sum of all divisors of // j which do not include i so we add // i * (sum1[j] - i) to sum2[j] sum2[j] += (sum1[j] - i) * i; // In the above implementation we have considered // every pair two times so we have to divide // every sum2 array element by 2 for (int i = 1; i < max_Element; i++) sum2[i] /= 2; // Here i is the divisor of j and we are trying to // add the sum of multiplication of all triplets of // divisors of j such that one of the divisors is i for (int i = 1; i < max_Element; i++) for (int j = i; j < max_Element; j += i) sum3[j] += i * (sum2[j] - i * (sum1[j] - i)); // In the above implementation we have considered // every triplet three times so we have to divide // every sum3 array element by 3 for (int i = 1; i < max_Element; i++) sum3[i] /= 3; // Print the results for (int i = 0; i < n; i++) System.out.print(sum3[arr[i]] + " ");} // Driver codepublic static void main(String[] args){ int arr[] = { 9, 5, 6 }; int n = arr.length; // Precomputing precomputation(arr, n);}} // This code has been contributed by 29AjayKumar |
Python3
# Python 3 implementation of the approach # Global array declaration#global max_Elementmax_Element = 100005 sum1 = [0 for i in range(max_Element)]sum2 = [0 for i in range(max_Element)]sum3 = [0 for i in range(max_Element)] # Function to find the sum of multiplication of# every triplet in the divisors of a numberdef precomputation(arr, n): # global max_Element # sum1[x] represents the sum of # all the divisors of x for i in range(1, max_Element, 1): for j in range(i, max_Element, i): # Adding i to sum1[j] because i # is a divisor of j sum1[j] += i # sum2[x] represents the sum of # all the divisors of x for i in range(1, max_Element, 1): for j in range(i, max_Element, i): # Here i is divisor of j and sum1[j] - i # represents sum of all divisors of # j which do not include i so we add # i * (sum1[j] - i) to sum2[j] sum2[j] += (sum1[j] - i) * i # In the above implementation we have considered # every pair two times so we have to divide # every sum2 array element by 2 for i in range(1, max_Element, 1): sum2[i] = int(sum2[i] / 2) # Here i is the divisor of j and we are trying to # add the sum of multiplication of all triplets of # divisors of j such that one of the divisors is i for i in range(1, max_Element, 1): for j in range(i, max_Element, i): sum3[j] += i * (sum2[j] - i * (sum1[j] - i)) # In the above implementation we have considered # every triplet three times so we have to divide # every sum3 array element by 3 for i in range(1, max_Element, 1): sum3[i] = int(sum3[i] / 3) # Print the results for i in range(n): print(sum3[arr[i]], end = " ") # Driver codeif __name__ == '__main__': arr = [9, 5, 6] n = len(arr) # Precomputing precomputation(arr, n) # This code is contributed by# Surendra_Gangwar |
C#
// C# implementation of the approach using System; class GFG { static int max_Element = (int) (1e6 + 5); // Global array declaration static int []sum1 = new int[max_Element]; static int []sum2 = new int[max_Element]; static int []sum3 = new int[max_Element]; // Function to find the sum of multiplication of // every triplet in the divisors of a number static void precomputation(int []arr, int n) { // sum1[x] represents the sum of all the divisors of x for (int i = 1; i < max_Element; i++) for (int j = i; j < max_Element; j += i) // Adding i to sum1[j] because i // is a divisor of j sum1[j] += i; // sum2[x] represents the sum of all the divisors of x for (int i = 1; i < max_Element; i++) for (int j = i; j < max_Element; j += i) // Here i is divisor of j and sum1[j] - i // represents sum of all divisors of // j which do not include i so we add // i * (sum1[j] - i) to sum2[j] sum2[j] += (sum1[j] - i) * i; // In the above implementation we have considered // every pair two times so we have to divide // every sum2 array element by 2 for (int i = 1; i < max_Element; i++) sum2[i] /= 2; // Here i is the divisor of j and we are trying to // add the sum of multiplication of all triplets of // divisors of j such that one of the divisors is i for (int i = 1; i < max_Element; i++) for (int j = i; j < max_Element; j += i) sum3[j] += i * (sum2[j] - i * (sum1[j] - i)); // In the above implementation we have considered // every triplet three times so we have to divide // every sum3 array element by 3 for (int i = 1; i < max_Element; i++) sum3[i] /= 3; // Print the results for (int i = 0; i < n; i++) Console.Write(sum3[arr[i]] + " "); } // Driver code public static void Main() { int []arr = { 9, 5, 6 }; int n = arr.Length; // Precomputing precomputation(arr, n); } } // This code has been contributed by Ryuga |
PHP
<?php// PHP implementation of the approach $max_Element = 1005; // Global array declaration $sum1 = array_fill(0, $max_Element, 0);$sum2 = array_fill(0, $max_Element, 0);$sum3 = array_fill(0, $max_Element, 0); // Function to find the sum of multiplication of // every triplet in the divisors of a number function precomputation($arr, $n) { global $max_Element, $sum3, $sum2, $sum1; // sum1[x] represents the sum of // all the divisors of x for ($i = 1; $i < $max_Element; $i++) for ($j = $i; $j < $max_Element; $j += $i) // Adding i to sum1[j] because i // is a divisor of j $sum1[$j] += $i; // sum2[x] represents the sum of // all the divisors of x for ($i = 1; $i < $max_Element; $i++) for ($j = $i; $j < $max_Element; $j += $i) // Here i is divisor of j and sum1[j] - i // represents sum of all divisors of // j which do not include i so we add // i * (sum1[j] - i) to sum2[j] $sum2[$j] += ($sum1[$j] - $i) * $i; // In the above implementation we have considered // every pair two times so we have to divide // every sum2 array element by 2 for ($i = 1; $i < $max_Element; $i++) $sum2[$i] = (int)($sum2[$i] / 2); // Here i is the divisor of j and we are trying to // add the sum of multiplication of all triplets of // divisors of j such that one of the divisors is i for ($i = 1; $i < $max_Element; $i++) for ($j = $i; $j < $max_Element; $j += $i) $sum3[$j] += $i * ($sum2[$j] - $i * ($sum1[$j] - $i)); // In the above implementation we have considered // every triplet three times so we have to divide // every sum3 array element by 3 for ($i = 1; $i < $max_Element; $i++) $sum3[$i] = (int)($sum3[$i] / 3); // Print the results for ($i = 0; $i < $n; $i++) echo $sum3[$arr[$i]] . " "; } // Driver code $arr = array( 9, 5, 6 ); $n = count($arr); // Precomputing precomputation($arr, $n); // This code is contributed by mits?> |
27 0 72
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


