Sum of product of proper divisors of all Numbers lying in range [L, R]
Last Updated :
03 Nov, 2023
Given an array
arr[][]
consisting of
Q
queries where every row consists of two numbers
L
and
R
which denotes the range
[L, R]
; the task is to find the sum of the product of proper divisors of all numbers lying in the range [L, R].
Note:
Since the answer might be very big, perform
% with 1000000007
for every query.
Examples:
Input: Q = 2, arr[] = { { 4, 6 }, { 8, 10 } } Output: 9 21 Explanation: Query 1: From 4 to 6 Product of proper divisors of 4 = 1 * 2 = 2 Product of proper divisors of 5 = 1 Product of proper divisors of 6 = 1 * 2 * 3 = 6 Sum of product of proper divisors from 4 to 6 = 2 + 1 + 6 = 9 Query 2: From 8 to 10 Product of proper divisors of 8 = 1 * 2 * 4 = 8 Product of proper divisors of 9 = 1 * 3 = 3 Product of proper divisors of 10 = 1 * 2 * 5 = 10 Sum of product of proper divisors from 8 to 10 = 8 + 3 + 10 = 21 Input: Q = 2, arr[] = { { 10, 20 }, { 12, 16 } } Output: 975 238
Approach:
Since there can be multiple queries and finding the divisors and product for every query is not feasible, the idea is to precompute and store every element along with its product of proper divisors in an array using a modification of
Sieve of Eratosthenes
. Once the product of the proper divisors of all the numbers is stored in an array, the idea is to use the concept of
prefix sum array
. The sum of the product of proper divisors till that particular index is precomputed and stored in an array
pref[]
so that every query can be answered in constant time. For each query, the sum of all product of proper divisors for the range
[L, R]
can be found as follows:
sum = pref[R] - pref[L - 1]
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
vector<ll> ans(100002, 1);
long long pref[100002];
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;
}
}
for ( int i = 1; i < 100002; ++i) {
pref[i] = pref[i - 1]
+ ans[i];
pref[i] %= mod;
}
}
void printSum( int L, int R)
{
cout << pref[R] - pref[L - 1]
<< " " ;
}
void printSumProper( int arr[][2], int Q)
{
preCompute();
for ( int i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
}
int main()
{
int Q = 2;
int arr[][2] = { { 10, 20 },
{ 12, 16 } };
printSumProper(arr, Q);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int mod = 1000000007 ;
static int []ans = new int [ 100002 ];
static int []pref = new int [ 100002 ];
static void preCompute()
{
Arrays.fill(ans, 1 );
for ( int i = 2 ; i <= 100000 / 2 ; i++) {
for ( int j = 2 * i; j <= 100000 ; j += i) {
ans[j] = (ans[j] * i) % mod;
}
}
for ( int i = 1 ; i < 100002 ; ++i) {
pref[i] = pref[i - 1 ]
+ ans[i];
pref[i] %= mod;
}
}
static void printSum( int L, int R)
{
System.out.print(pref[R] - pref[L - 1 ]+ " " );
}
static void printSumProper( int [][]arr, int Q)
{
preCompute();
for ( int i = 0 ; i < Q; i++) {
printSum(arr[i][ 0 ], arr[i][ 1 ]);
}
}
public static void main(String args[])
{
int Q = 2 ;
int [][] arr = {{ 10 , 20 },
{ 12 , 16 } };
printSumProper(arr, Q);
}
}
|
Python3
mod = 1000000007
ans = [ 1 ] * ( 100002 )
pref = [ 0 ] * 100002
def preCompute():
for i in range ( 2 , 100000 / / 2 + 1 ):
for j in range ( 2 * i, 100000 + 1 ,i):
ans[j] = (ans[j] * i) % mod
for i in range ( 1 , 100002 ):
pref[i] = pref[i - 1 ] + ans[i]
pref[i] % = mod
def printSum(L, R):
print (pref[R] - pref[L - 1 ],end = " " )
def printSumProper(arr, Q):
preCompute()
for i in range (Q):
printSum(arr[i][ 0 ], arr[i][ 1 ])
if __name__ = = '__main__' :
Q = 2
arr = [ [ 10 , 20 ],
[ 12 , 16 ] ]
printSumProper(arr, Q)
|
C#
using System;
class GFG
{
static int mod = 1000000007;
static int [] ans = new int [100002];
static int [] pref = new int [100002];
static void PreCompute()
{
Array.Fill(ans, 1);
for ( int i = 2; i <= 100000 / 2; i++)
{
for ( int j = 2 * i; j <= 100000; j += i)
{
ans[j] = ( int )(( long )ans[j] * i % mod);
}
}
for ( int i = 1; i < 100002; ++i)
{
pref[i] = (pref[i - 1] + ans[i]) % mod;
}
}
static void PrintSum( int L, int R)
{
Console.Write((pref[R] - pref[L - 1]) + " " );
}
static void PrintSumProper( int [][] arr, int Q)
{
PreCompute();
for ( int i = 0; i < Q; i++)
{
PrintSum(arr[i][0], arr[i][1]);
}
}
public static void Main( string [] args)
{
int Q = 2;
int [][] arr = { new int [] { 10, 20 }, new int [] { 12, 16 } };
PrintSumProper(arr, Q);
}
}
|
Javascript
const mod = 1000000007;
const ans = new Array(100002).fill(1);
const pref = new Array(100002).fill(0);
function preCompute() {
for (let i = 2; i <= 100000 / 2; i++) {
for (let j = 2 * i; j <= 100000; j += i) {
ans[j] = (ans[j] * i) % mod;
}
}
for (let i = 1; i < 100002; i++) {
pref[i] = (pref[i - 1] + ans[i]) % mod;
}
}
function printSum(L, R) {
console.log(pref[R] - pref[L - 1]);
}
function printSumProper(arr, Q) {
preCompute();
for (let i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
}
const Q = 2;
const arr = [
[10, 20],
[12, 16]
];
printSumProper(arr, Q);
|
Share your thoughts in the comments
Please Login to comment...