📜  Q查询的数字的适当除数的乘积

📅  最后修改于: 2021-04-22 01:59:43             🧑  作者: Mango

给定一个整数N,任务是为Q查询找到模数为10 9 + 7的适当除数的乘积。

例子:

方法:这个想法是在Eratosthenes的Sieve的帮助下预先计算和存储元素的适当除数的乘积。

下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
  
#include 
#define ll long long int
#define mod 1000000007
  
using namespace std;
  
vector 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;
}


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


Python3
# 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


C#
// 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


输出:
2, 6, 8, 64, 279936,