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++
  • Python3

// 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;

}

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.

#arrays #mathematical #school programming #array-range-queries #divisors

Product of proper divisors of a number for Q queries - GeeksforGeeks
1.75 GEEK