📌  相关文章
📜  位于[L,R]范围内的所有数字的适当除数的乘积和

📅  最后修改于: 2021-04-29 12:17:22             🧑  作者: Mango

给定由Q个查询组成的数组arr [] [] ,其中每行包含两个数字LR ,它们表示范围[L,R] ;任务是找到位于[L,R]范围内的所有数字的适当除数的乘积之和。

注意:由于答案可能很大,因此对每个查询都用1000000007执行%。

例子:

方法:由于可能存在多个查询,因此无法为每个查询找到除数和乘积,因此,想法是使用Eratosthenes筛网的一种改进,将每个元素及其适当除数的乘积预先计算并存储在一个数组中。

将所有数字的适当除数的乘积存储在数组中后,便会使用前缀和数组的概念。适当除数乘积的总和,直到该特定索引被预先计算并存储在数组pref []中,以便可以在恒定时间内回答每个查询。

对于每个查询,可以找到范围为[L,R]的适当除数的所有乘积之和:

sum = pref[R] - pref[L - 1] 

下面是上述方法的实现:

CPP
// C++ implementation to find the sum
// of the product of proper divisors of
// all the numbers lying in the range [L, R]
  
#include 
#define ll long long int
#define mod 1000000007
using namespace std;
  
// Vector to store the product
// of the proper divisors of a number
vector ans(100002, 1);
  
// Variable to store the prefix
// sum of the product array
long long pref[100002];
  
// Function to precompute the product
// of proper divisors of a number at
// it's corresponding index
void preCompute()
{
    // Modificatino of sieve to store the
    // product of the proper divisors
    for (int i = 2; i <= 100000 / 2; i++) {
        for (int j = 2 * i; j <= 100000; j += i) {
  
            // Multiplying the existing value
            // with i because i is the
            // proper divisor of ans[j]
            ans[j] = (ans[j] * i) % mod;
        }
    }
  
    // Loop to store the prefix sum of the
    // previously computed product array
    for (int i = 1; i < 100002; ++i) {
  
        // Computing the prefix sum
        pref[i] = pref[i - 1]
                  + ans[i];
        pref[i] %= mod;
    }
}
  
// Function to print the sum
// for each query
void printSum(int L, int R)
{
    cout << pref[R] - pref[L - 1]
         << " ";
}
  
// Function to print te sum of product
// of proper divisors of a number in
// [L, R]
void printSumProper(int arr[][2], int Q)
{
  
    // Calling the function that
    // pre computes
    // the sum of product
    // of proper divisors
    preCompute();
  
    // Iterate over all Queries
    // to print the sum
    for (int i = 0; i < Q; i++) {
        printSum(arr[i][0], arr[i][1]);
    }
}
  
// Driver code
int main()
{
    int Q = 2;
    int arr[][2] = { { 10, 20 },
                     { 12, 16 } };
  
    printSumProper(arr, Q);
    return 0;
}


Java
// Java implementation to find the sum
// of the product of proper divisors of
// all the numbers lying in the range [L, R]
import java.util.*;
  
class GFG{
  
static int mod = 1000000007;
  
// Vector to store the product
// of the proper divisors of a number
static int []ans = new int[100002];
  
// Variable to store the prefix
// sum of the product array
static int []pref = new int[100002];
  
// Function to precompute the product
// of proper divisors of a number at
// it's corresponding index
static void preCompute()
{
    // Modificatino of sieve to store the
    // product of the proper divisors
    Arrays.fill(ans, 1);
    for (int i = 2; i <= 100000 / 2; i++) {
        for (int j = 2 * i; j <= 100000; j += i) {
  
            // Multiplying the existing value
            // with i because i is the
            // proper divisor of ans[j]
            ans[j] = (ans[j] * i) % mod;
        }
    }
  
    // Loop to store the prefix sum of the
    // previously computed product array
    for (int i = 1; i < 100002; ++i) {
  
        // Computing the prefix sum
        pref[i] = pref[i - 1]
                + ans[i];
        pref[i] %= mod;
    }
}
  
// Function to print the sum
// for each query
static void printSum(int L, int R)
{
    System.out.print(pref[R] - pref[L - 1]+" ");
}
  
// Function to print te sum of product
// of proper divisors of a number in
// [L, R]
static void printSumProper(int [][]arr, int Q)
{
  
    // Calling the function that
    // pre computes
    // the sum of product
    // of proper divisors
    preCompute();
  
    // Iterate over all Queries
    // to print the sum
    for (int i = 0; i < Q; i++) {
        printSum(arr[i][0], arr[i][1]);
    }
}
  
// Driver code
public static void main(String args[])
{
    int Q = 2;
    int[][] arr = {{10, 20 },
                    { 12, 16 } };
  
    printSumProper(arr, Q);
}
}
  
// This code is contributed by Surendra_Gangwar


Python3
# Python3 implementation to find the sum
# of the product of proper divisors of
# all the numbers lying in the range [L, R]
  
mod = 1000000007
  
# Vector to store the product
# of the proper divisors of a number
ans = [1]*(100002)
  
# Variable to store the prefix
# sum of the product array
pref = [0]*100002
  
# Function to precompute the product
# of proper divisors of a number at
# it's corresponding index
def preCompute():
  
    # Modificatino of sieve to store the
    # product of the proper divisors
    for i in range(2,100000//2+1): 
        for j in range(2*i,100000+1,i): 
  
            # Multiplying the existing value
            # with i because i is the
            # proper divisor of ans[j]
            ans[j] = (ans[j] * i) % mod
          
    # Loop to store the prefix sum of the
    # previously computed product array
    for i in range(1,100002): 
  
        # Computing the prefix sum
        pref[i] = pref[i - 1]+ ans[i]
        pref[i] %= mod
      
# Function to prthe sum
# for each query
def printSum(L, R):
  
    print(pref[R] - pref[L - 1],end=" ")
  
# Function to prte sum of product
# of proper divisors of a number in
# [L, R]
def printSumProper(arr, Q):
  
    # Calling the function that
    # pre computes
    # the sum of product
    # of proper divisors
    preCompute()
  
    # Iterate over all Queries
    # to prthe sum
    for i in range(Q): 
        printSum(arr[i][0], arr[i][1])
      
# Driver code
if __name__ == '__main__':
    Q = 2
    arr= [ [ 10, 20 ],
            [ 12, 16 ] ]
  
    printSumProper(arr, Q)
  
# This code is contributed by mohit kumar 29


输出:
975 238