📌  相关文章
📜  Q 查询的一个数的真除数的乘积

📅  最后修改于: 2021-09-06 06:46:18             🧑  作者: Mango

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

例子:

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

下面是上述方法的实现:

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


Javascript


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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live