📜  一个数的除数的三元组相乘的总和

📅  最后修改于: 2021-05-06 19:11:01             🧑  作者: Mango

给定大小为n的整数的数组arr [] 。对于每个元素,必须打印使用该元素的除数形成的每个三元组的乘积之和。

例子:

天真的方法:存储数字的所有除数并应用蛮力方法,对于每个三元组,将这三个元素的乘积添加到答案中。

高效的方法:此方法类似于Eratosthenes的筛网,我们用于查找范围内的所有素数。

  1. 首先,我们创建一个数组sum1,该数组在sum1 [x]处存储数字x的所有除数之和。因此,首先迭代所有小于maximum_Element的数字,然后将此数字加到该数字的倍数的和中。因此,我们将能够在该数字位置存储任意数目的所有除数的总和。
  2. 填充sum1数组后,需要填充第二个数组sum2的时间,第二个数组sum2会将数字x的每个对数的乘积之和存储在sum2 [x]处。为此,我们对与step1类似的每个数字进行求和并用达到更高的价值。
  3. 为了填充sum3数组,我们将对小于max_Element的所有数字进行相似的处理,并将j的除数之和相加,以使i为j的除数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long
const ll max_Element = 1e6 + 5;
  
// Global array declaration
int sum1[max_Element], sum2[max_Element], sum3[max_Element];
  
// Function to find the sum of multiplication of
// every triplet in the divisors of a number
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++)
        cout << sum3[arr[i]] << " ";
}
  
// Driver code
int main()
{
  
    int arr[] = { 9, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Precomputing
    precomputation(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFGq 
{
  
static int max_Element = (int) (1e6 + 5);
  
// Global array declaration
static 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 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++)
        System.out.print(sum3[arr[i]] + " ");
}
  
// Driver code
public 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_Element
max_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 number
def 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 code
if __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


输出:
27 0 72