📜  从L到R的所有素数的乘积

📅  最后修改于: 2021-06-26 17:17:59             🧑  作者: Mango

给定范围[L,R]。任务是找到从L到R的给定范围内所有素数的乘积,其模数为10 ^ 9 + 7。
例子:

Input: L = 10, R = 20
Output: 46189
Prime numbers between [10, 20] are:
11, 13, 17, 19
Therefore, product = 11 * 13 * 17 * 19 = 46189

Input: L = 15, R = 25
Output: 7429

一个简单的解决方案是从L遍历到R,检查当前数字是否为质数。如果是,则将其乘以乘积。最后,打印产品。
一个有效的解决方案是使用Eratosthenes筛子查找所有达到给定极限的素数。然后,计算一个前缀乘积数组以存储乘积,直到每个值超出限制为止。一旦有了前缀数组,我们只需要返回(prefix [R] * modular_inverse(prefix [L-1]))%(10 ^ 9 + 7)。
注意: prefix [i]将存储从1到i的所有素数的乘积。
下面是上述方法的实现:

C++
// CPP program to find product of primes
// in range L to R
#include 
using namespace std;
#define mod 1000000007
const int MAX = 10000;
 
// prefix[i] is going to store product of primes
// till i (including i).
int prefix[MAX + 1];
 
// Function to build the prefix product array
void buildPrefix()
{
    // Create a boolean array "prime[0..n]". A
    // value in prime[i] will finally be false
    // if i is Not a prime, else true.
    bool prime[MAX + 1];
    memset(prime, true, sizeof(prime));
 
    for (int p = 2; p * p <= MAX; p++) {
 
        // If prime[p] is not changed, then
        // it is a prime
        if (prime[p] == true) {
 
            // Update all multiples of p
            for (int i = p * 2; i <= MAX; i += p)
                prime[i] = false;
        }
    }
 
    // Build prefix array
    prefix[0] = prefix[1] = 1;
    for (int p = 2; p <= MAX; p++) {
        prefix[p] = prefix[p - 1];
        if (prime[p])
            prefix[p] = (prefix[p] * p) % mod;
    }
}
 
/* Iterative Function to calculate (x^y)%p in O(log y) */
long long int power(long long int x, long long int y, int p)
{
 
    // Initialize result
    long long int res = 1;
 
    // Update x if it is more than or
    // equal to p
    x = x % p;
 
    while (y > 0) {
 
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
 
        // y must be even now
        // y = y/2
        y = y >> 1;
        x = (x * x) % p;
    }
 
    return res;
}
 
// Returns modular inverse
long long int inverse(long long int n)
{
    return power(n, mod - 2, mod);
}
 
// Function to return product of prime in range
long long int productPrimeRange(int L, int R)
{
 
    return (prefix[R] * inverse(prefix[L - 1])) % mod;
}
 
// Driver code
int main()
{
    buildPrefix();
 
    int L = 10, R = 20;
 
    cout << productPrimeRange(L, R) << endl;
 
    return 0;
}


Java
// Java program to find product of primes
// in range L to R
 
import java.io.*;
 
class GFG {
    
 
static int mod = 1000000007;
static  int MAX = 10000;
 
// prefix[i] is going to store product of primes
// till i (including i).
static int []prefix = new int[MAX+1];
 
// Function to build the prefix product array
static void buildPrefix()
{
    // Create a boolean array "prime[0..n]". A
    // value in prime[i] will finally be false
    // if i is Not a prime, else true.
    boolean prime[] = new boolean[MAX + 1];
    for(int i=0;i 0) {
 
        // If y is odd, multiply x with result
        if ((y & 1)>0)
            res = (res * x) % p;
 
        // y must be even now
        // y = y/2
        y = y >> 1;
        x = (x * x) % p;
    }
 
    return res;
}
 
// Returns modular inverse
static long  inverse(long n)
{
    return power(n, mod - 2, mod);
}
 
// Function to return product of prime in range
static long  productPrimeRange(int L, int R)
{
 
    return (prefix[R] * inverse(prefix[L - 1])) % mod;
}
 
// Driver code
 
    public static void main (String[] args) {
            buildPrefix();
 
    int L = 10, R = 20;
    System.out.println( productPrimeRange(L, R));
    }
}
 
//This code is contributed by shs..


Python 3
# Python 3 program to find product of primes
# in range L to R
 
mod = 1000000007
MAX = 10000
 
# prefix[i] is going to store product of primes
# till i (including i).
prefix = [0]*(MAX + 1)
 
# Function to build the prefix product array
def buildPrefix():
 
    # Create a boolean array "prime[0..n]". A
    # value in prime[i] will finally be false
    # if i is Not a prime, else true.
    prime = [True]*(MAX + 1)
 
    p = 2
    while p * p <= MAX :
 
        # If prime[p] is not changed, then
        # it is a prime
        if (prime[p] == True) :
 
            # Update all multiples of p
            for i in range( p * 2, MAX+1, p):
                prime[i] = False
                 
        p += 1
 
    # Build prefix array
    prefix[0] = prefix[1] = 1
    for p in range(2,MAX+1) :
        prefix[p] = prefix[p - 1]
        if (prime[p]):
            prefix[p] = (prefix[p] * p) % mod
     
# Iterative Function to calculate
# (x^y)%p in O(log y)
def power(x, y,p):
 
    # Initialize result
    res = 1
 
    # Update x if it is more than or
    # equal to p
    x = x % p
 
    while (y > 0) :
 
        # If y is odd, multiply x with result
        if (y & 1):
            res = (res * x) % p
 
        # y must be even now
        # y = y//2
        y = y >> 1
        x = (x * x) % p
 
    return res
 
# Returns modular inverse
def inverse( n):
 
    return power(n, mod - 2, mod)
 
# Function to return product of prime in range
def productPrimeRange(L, R):
    return (prefix[R] * inverse(prefix[L - 1])) % mod
 
# Driver code
if __name__ == "__main__":
    buildPrefix()
 
    L = 10
    R = 20
 
    print(productPrimeRange(L, R))
     
# this code is contributed by
# ChitraNayal


C#
// C# program to find product of
// primes in range L to R
using System;
 
class GFG
{
     
static int mod = 1000000007;
static int MAX = 10000;
 
// prefix[i] is going to store product
// of primes till i (including i).
static int []prefix = new int[MAX + 1];
 
// Function to build the prefix
// product array
static void buildPrefix()
{
    // Create a boolean array "prime[0..n]".
    // A value in prime[i] will finally be
    // false if i is Not a prime, else true.
    bool []prime = new bool[MAX + 1];
    for(int i = 0; i < MAX + 1; i++)
    prime[i] = true;
    //memset(prime, true, sizeof(prime));
 
    for (int p = 2; p * p <= MAX; p++)
    {
 
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true)
        {
 
            // Update all multiples of p
            for (int i = p * 2; i <= MAX; i += p)
                prime[i] = false;
        }
    }
 
    // Build prefix array
    prefix[0] = prefix[1] = 1;
    for (int p = 2; p <= MAX; p++)
    {
        prefix[p] = prefix[p - 1];
        if (prime[p])
            prefix[p] = (prefix[p] * p) % mod;
    }
}
 
/* Iterative Function to calculate
   (x^y)%p in O(log y) */
static long power(long x, long y, int p)
{
 
    // Initialize result
    long res = 1;
 
    // Update x if it is more
    // than or equal to p
    x = x % p;
 
    while (y > 0)
    {
 
        // If y is odd, multiply x
        // with result
        if ((y & 1) > 0)
            res = (res * x) % p;
 
        // y must be even now
        // y = y/2
        y = y >> 1;
        x = (x * x) % p;
    }
 
    return res;
}
 
// Returns modular inverse
static long inverse(long n)
{
    return power(n, mod - 2, mod);
}
 
// Function to return product
// of prime in range
static long productPrimeRange(int L, int R)
{
 
    return (prefix[R] *
            inverse(prefix[L - 1])) % mod;
}
 
// Driver code
public static void Main ()
{
    buildPrefix();
 
    int L = 10, R = 20;
    Console.WriteLine(productPrimeRange(L, R));
}
}
 
// This code is contributed by anuj_67


Javascript


输出:
46189

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。