Related Articles

Related Articles

Product of proper divisors of a number for Q queries
  • Last Updated : 27 Aug, 2020

Given an integer N, the task is to find the product of proper divisors of the number modulo 109 + 7 for Q queries.

Examples:

Input: Q = 4, arr[] = { 4, 6, 8, 16 };
Output: 2 6 8 64
Explanation:
4 => 1, 2 = 1 * 2 = 2 
6 => 1, 2, 3 = 1 * 2 * 3 = 6
8 => 1, 2, 4 = 1 * 2 * 4 = 8
16 => 1, 2, 4, 8 = 1 * 2 * 4 * 8 = 64

Input: arr[] = { 3, 6, 9, 12 }
Output: 1 6 3 144

 

Approach: The idea is to pre-compute and store product of proper divisors of elements with the help of Sieve of Eratosthenes.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of
// the above approach
  
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
  
using namespace std;
  
vector<ll> ans(100002, 1);
  
// Function to precompute the product
// of proper divisors of a number at
// it's corresponding index
void preCompute()
{
    for (int i = 2; i <= 100000 / 2; i++) {
        for (int j = 2 * i; j <= 100000; j += i) {
            ans[j] = (ans[j] * i) % mod;
        }
    }
}
  
int productOfProperDivi(int num)
{
  
    // Returning the pre-computed
    // values
    return ans[num];
}
  
// Driver code
int main()
{
    preCompute();
    int queries = 5;
    int a[queries] = { 4, 6, 8, 16, 36 };
  
    for (int i = 0; i < queries; i++) {
        cout << productOfProperDivi(a[i])
             << ", ";
    }
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of
// the above approach
import java.util.*;
class GFG
{
    static final int mod = 1000000007;
  
    static long[] ans = new long[100002];
  
    // Function to precompute the product
    // of proper divisors of a number at
    // it's corresponding index
    static void preCompute()
    {
        for (int i = 2; i <= 100000 / 2; i++) 
        {
            for (int j = 2 * i; j <= 100000; j += i) 
            {
                ans[j] = (ans[j] * i) % mod;
            }
        }
    }
  
    static long productOfProperDivi(int num)
    {
  
        // Returning the pre-computed
        // values
        return ans[num];
    }
  
    // Driver code
    public static void main(String[] args)
    {
        Arrays.fill(ans, 1);
        preCompute();
        int queries = 5;
        int[] a = { 4, 6, 8, 16, 36 };
  
        for (int i = 0; i < queries; i++) 
        {
            System.out.print(productOfProperDivi(a[i]) + ", ");
        }
    }
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of
# the above approach
mod = 1000000007
  
ans = [1] * (100002)
  
# Function to precompute the product
# of proper divisors of a number at
# it's corresponding index
def preCompute():
  
    for i in range(2, 100000 // 2 + 1):
        for j in range(2 * i, 100001, i):
            ans[j] = (ans[j] * i) % mod
  
def productOfProperDivi(num):
  
    # Returning the pre-computed
    # values
    return ans[num]
  
# Driver code
if __name__ == "__main__":
  
    preCompute()
    queries = 5
    a = [ 4, 6, 8, 16, 36 ]
  
    for i in range(queries):
        print(productOfProperDivi(a[i]), end = ", ")
  
# This code is contributed by chitranayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of
// the above approach
using System;
  
class GFG{
      
static readonly int mod = 1000000007;
  
static long[] ans = new long[100002];
  
// Function to precompute the product
// of proper divisors of a number at
// it's corresponding index
static void preCompute()
{
    for(int i = 2; i <= 100000 / 2; i++) 
    {
        for(int j = 2 * i; j <= 100000; j += i) 
        {
            ans[j] = (ans[j] * i) % mod;
        }
    }
}
  
static long productOfProperDivi(int num)
{
  
    // Returning the pre-computed
    // values
    return ans[num];
}
  
// Driver code
public static void Main(String[] args)
{
    for(int i = 0 ; i < 100002; i++)
        ans[i] = 1;
          
    preCompute();
  
    int queries = 5;
    int[] a = { 4, 6, 8, 16, 36 };
  
    for(int i = 0; i < queries; i++) 
    {
        Console.Write(productOfProperDivi(a[i]) + ", ");
    }
}
}
  
// This code is contributed by Princi Singh

chevron_right


Output: 

2, 6, 8, 64, 279936,

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :