📜  求除数之和

📅  最后修改于: 2021-04-21 22:08:37             🧑  作者: Mango

给定三个整数ABC ,任务是查找
ΣI = J = 1Σc ^ k = 1时d(IJK),其中d(x)是x的约数的数量。答案可能非常大,因此,以10 9 +7为模数打印答案。

例子:

Input: A = 2, B = 2, c = 2
Output: 20
Explanation: d(1.1.1) = d(1) = 1;
    d(1·1·2) = d(2) = 2;
    d(1·2·1) = d(2) = 2;
    d(1·2·2) = d(4) = 3;
    d(2·1·1) = d(2) = 2;
    d(2·1·2) = d(4) = 3;
    d(2·2·1) = d(4) = 3;
    d(2·2·2) = d(8) = 4. 

Input: A = 5, B = 6, C = 7
Output: 1520

方法:

  • 查找范围为[1,N]的所有数字的除数
  • 使用迭代器i,j,k从1到N开始运行三个嵌套循环
  • 然后使用预先计算的除数来找到d(ijk)。

下面是上述方法的实现:

C++
#include 
using namespace std;
  
#define N 100005
#define mod 1000000007
  
// To store the number of divisors
int cnt[N];
  
// Function to find the number of divisors
// of all numbers in  the range 1 to n
void Divisors()
{
    memset(cnt, 0, sizeof cnt);
  
    // For every number 1 to n
    for (int i = 1; i < N; i++) {
  
        // Increase divisors count for every number
        for (int j = 1; j * i < N; j++)
            cnt[i * j]++;
    }
}
  
// Function to find the sum of divisors
int Sumofdivisors(int A, int B, int C)
{
    // To store sum
    int sum = 0;
  
    Divisors();
  
    for (int i = 1; i <= A; i++) {
        for (int j = 1; j <= B; j++) {
            for (int k = 1; k <= C; k++) {
                int x = i * j * k;
  
                // Count the diviosrs
                sum += cnt[x];
                if (sum >= mod)
                    sum -= mod;
            }
        }
    }
  
    return sum;
}
  
// Driver code
int main()
{
  
    int A = 5, B = 6, C = 7;
  
    // Function call
    cout << Sumofdivisors(A, B, C);
  
    return 0;
}


Java
// Java code for above given approach
class GFG 
{
  
    static int N = 100005;
    static int mod = 1000000007;
  
    // To store the number of divisors 
    static int cnt[] = new int[N];
  
    // Function to find the number of divisors 
    // of all numbers in the range 1 to n 
    static void Divisors() 
    {
  
        // For every number 1 to n 
        for (int i = 1; i < N; i++) 
        {
  
            // Increase divisors count for every number 
            for (int j = 1; j * i < N; j++) 
            {
                cnt[i * j]++;
            }
        }
    }
  
    // Function to find the sum of divisors 
    static int Sumofdivisors(int A, int B, int C) 
    {
        // To store sum 
        int sum = 0;
  
        Divisors();
  
        for (int i = 1; i <= A; i++) 
        {
            for (int j = 1; j <= B; j++) 
            {
                for (int k = 1; k <= C; k++) 
                {
                    int x = i * j * k;
  
                    // Count the diviosrs 
                    sum += cnt[x];
                    if (sum >= mod) 
                    {
                        sum -= mod;
                    }
                }
            }
        }
  
        return sum;
    }
  
    // Driver code 
    public static void main(String[] args)
    {
        int A = 5, B = 6, C = 7;
  
        // Function call 
        System.out.println(Sumofdivisors(A, B, C));
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 code for above given approach
N = 100005
mod = 1000000007
  
# To store the number of divisors 
cnt = [0] * N; 
  
# Function to find the number of divisors 
# of all numbers in the range 1 to n 
def Divisors() : 
  
    # For every number 1 to n 
    for i in range(1, N) :
  
        # Increase divisors count
        # for every number 
        for j in range(1, N // i) : 
            cnt[i * j] += 1; 
  
# Function to find the sum of divisors 
def Sumofdivisors(A, B, C) : 
      
    # To store sum 
    sum = 0; 
  
    Divisors(); 
  
    for i in range(1,A + 1) :
        for j in range(1, B + 1) : 
            for k in range(1, C + 1) :
                x = i * j * k;
                  
                # Count the diviosrs
                sum += cnt[x];
                if (sum >= mod) :
                    sum -= mod; 
  
    return sum; 
  
# Driver code 
if __name__ == "__main__" : 
  
    A = 5; B = 6; C = 7; 
  
    # Function call 
    print(Sumofdivisors(A, B, C)); 
  
# This code is contributed by Ryuga


C#
// C# code for above given approach
using System;
      
class GFG 
{
  
    static int N = 100005;
    static int mod = 1000000007;
  
    // To store the number of divisors 
    static int []cnt = new int[N];
  
    // Function to find the number of divisors 
    // of all numbers in the range 1 to n 
    static void Divisors() 
    {
  
        // For every number 1 to n 
        for (int i = 1; i < N; i++) 
        {
  
            // Increase divisors count for every number 
            for (int j = 1; j * i < N; j++) 
            {
                cnt[i * j]++;
            }
        }
    }
  
    // Function to find the sum of divisors 
    static int Sumofdivisors(int A, int B, int C) 
    {
        // To store sum 
        int sum = 0;
  
        Divisors();
  
        for (int i = 1; i <= A; i++) 
        {
            for (int j = 1; j <= B; j++) 
            {
                for (int k = 1; k <= C; k++) 
                {
                    int x = i * j * k;
  
                    // Count the diviosrs 
                    sum += cnt[x];
                    if (sum >= mod) 
                    {
                        sum -= mod;
                    }
                }
            }
        }
  
        return sum;
    }
  
    // Driver code 
    public static void Main(String[] args)
    {
        int A = 5, B = 6, C = 7;
  
        // Function call 
        Console.WriteLine(Sumofdivisors(A, B, C));
    }
}
  
// This code contributed by Rajput-Ji


输出:
1520