Find the sum of the number of divisors

Given three integers A, B, C, the task is to find
ΣAi=1 ΣBj=1ΣCk=1 d(i.j.k), where d(x) is the number of divisors of x. Answer can be very large, So, print answer modulo 109+7.

Examples:

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

Approach:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
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;
}

chevron_right


Output:

1520


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.