📜  数量因子乘积

📅  最后修改于: 2021-04-28 17:43:30             🧑  作者: Mango

给定数字n,找到所有n因子的乘积。由于乘积可能非常大,因此以10 ^ 9 + 7为模。
例子 :

Input : 12
Output : 1728
1 * 2 * 3 * 4 * 6 * 12 = 1728

Input : 18
Output : 5832
1 * 2 * 3 * 6 * 9 * 18 = 5832

方法1(朴素的方法):
我们可以为i从1到n循环,如果n可被i整除,则将它们乘以数字。该解决方案的时间复杂度将为O(n)。
但是,这种方法对于n较大的值是不够的。
方法2(更好的方法):
更好的方法是让i的循环从1到sqrt(n)。如果数字可被我整除,则将其乘以i和n / i。
该解决方案的时间复杂度将为O(sqrt(n))。

C++
// C++ program to calculate product
// of factors of number
#include 
#define M 1000000007
using namespace std;
 
// function to product the factors
long long multiplyFactors(int n)
{
    long long prod = 1;
    for (int i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            // If factors are equal,
            // multiply only once
            if (n / i == i)
                prod = (prod * i) % M;
 
            // Otherwise multiply both
            else {
                prod = (prod * i) % M;
                prod = (prod * n / i) % M;
            }
        }
    }
    return prod;
}
 
// Driver code
int main()
{
    int n = 12;
 
    cout << multiplyFactors(n) << endl;
 
    return 0;
}


Java
// Java program to calculate product
// of factors of number
class GFG
{
public static final long M = 1000000007;
 
    // function to product the factors
    static long multiplyFactors(int n)
    {
        long prod = 1;
        for (int i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
 
                // If factors are equal,
                // multiply only once
                if (n / i == i)
                    prod = (prod * i) % M;
 
                // Otherwise multiply both
                else {
                    prod = (prod * i) % M;
                    prod = (prod * n / i) % M;
                }
            }
        }
        return prod;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 12;
        System.out.println(multiplyFactors(n));
    }
}


Python
# Python program to calculate product
# of factors of number
 
M = 1000000007
 
# function to product the factors
def multiplyFactors(n) :
    prod = 1
     
    i = 1
    while i * i <= n :
        if (n % i == 0) :
             
            # If factors are equal,
            # multiply only once
            if (n / i == i) :
                prod = (prod * i) % M
                 
            #Otherwise multiply both
            else :
                prod = (prod * i) % M
                prod = (prod * n / i) % M
        i = i + 1
 
    return prod
 
# Driver Code
 
n = 12
print (multiplyFactors(n))
 
 
# This code is contributed by Nikita Tiwari.


C#
//C# program to calculate product
// of factors of number
using System;
 
class GFG
{
public static long M = 1000000007;
 
    // function to product the factors
    static long multiplyFactors(int n)
    {
        long prod = 1;
        for (int i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
 
                // If factors are equal,
                // multiply only once
                if (n / i == i)
                    prod = (prod * i) % M;
 
                // Otherwise multiply both
                else {
                    prod = (prod * i) % M;
                    prod = (prod * n / i) % M;
                }
            }
        }
        return prod;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 12;
        Console.Write(multiplyFactors(n));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


C++
// C++ program to calculate product
// of factors of number
#include 
#define M 1000000007
using namespace std;
 
// Iterative Function to calculate
// (x^y) in O(log y)
long long power(long long x, long long y)
{
    long long res = 1;
 
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % M;
        y = (y >> 1) % M;
        x = (x * x) % M;
    }
    return res;
}
 
// function to count the factors
int countFactors(int n)
{
    int count = 0;
    for (int i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
 
            // If factors are equal,
            // count only once
            if (n / i == i)
                count++;
 
            // Otherwise count both
            else
                count += 2;
        }
    }
    return count;
}
 
long long multiplyFactors(int n)
{
    int numFactor = countFactors(n);
 
    // Calculate product of factors
    long long product = power(n, numFactor / 2);
 
    // If numFactor is odd return
    // power(n, numFactor/2) * sqrt(n)
    if (numFactor & 1)
        product = (product * (int)sqrt(n)) % M;
 
    return product;
}
 
// Driver code
int main()
{
    int n = 12;
    cout << multiplyFactors(n) << endl;
    return 0;
}


Java
// Java program to calculate product
// of factors of number
class GFG
{
public static final long M = 1000000007;
 
    // Iterative Function to calculate
    // (x^y) in O(log y)
    static long power(long x, long y)
    {
        long res = 1;
 
        while (y > 0)
        {
            if (y % 2 == 1)
                res = (res * x) % M;
            y = (y >> 1) % M;
            x = (x * x) % M;
        }
        return res;
    }
 
    // function to count the factors
    static int countFactors(int n)
    {
        int count = 0;
        for (int i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
 
                // If factors are equal,
                // count only once
                if (n / i == i)
                    count++;
 
                // Otherwise count both
                else
                    count += 2;
            }
        }
        return count;
    }
 
    static long multiplyFactors(int n)
    {
        int numFactor = countFactors(n);
 
        // Calculate product of factors
        long product = power(n, numFactor / 2);
 
        // If numFactor is odd return
        // power(n, numFactor/2) * sqrt(n)
        if (numFactor % 2 == 1)
            product = (product * (int)Math.sqrt(n)) % M;
 
        return product;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 12;
        System.out.println(multiplyFactors(n));
    }
}


Python
# Python program to calculate product
# of factors of number
 
M = 1000000007
 
# Iterative Function to calculate
# (x^y) in O(log y)
def power(x, y) :
     
    res = 1
    while (y > 0) :
         
        if (y % 2 == 1) :
            res = (res * x) % M
        y = (y >> 1) % M
        x = (x * x) % M
         
    return res
     
# function to count the factors
def countFactors(n) :
    count = 0
    i = 1
    while i * i <= n :
        if (n % i == 0) :
            # If factors are equal,
            # count only once
            if (n / i == i) :
                count = count + 1
             
            # Otherwise count both
            else :
                count = count + 2
        i = i + 1
    return count
     
def multiplyFactors(n) :
     
    numFactor = countFactors(n)
     
    # Calculate product of factors
    product = power(n, numFactor / 2)
 
    # If numFactor is odd return
    # power(n, numFactor/2) * sqrt(n)
    if (numFactor % 2 == 1) :
        product = (product *
                (int)(math.sqrt(n))) % M
 
    return product
     
# Driver Code
n = 12
print multiplyFactors(n)
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to calculate product
// of factors of number
using System;
 
class GFG {
     
    public static long M = 1000000007;
 
    // Iterative Function to calculate
    // (x^y) in O(log y)
    static long power(long x, long y)
    {
        long res = 1;
 
        while (y > 0)
        {
            if (y % 2 == 1)
                res = (res * x) % M;
            y = (y >> 1) % M;
            x = (x * x) % M;
        }
        return res;
    }
 
    // function to count the factors
    static int countFactors(int n)
    {
        int count = 0;
        for (int i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
 
                // If factors are equal,
                // count only once
                if (n / i == i)
                    count++;
 
                // Otherwise count both
                else
                    count += 2;
            }
        }
         
        return count;
    }
 
    static long multiplyFactors(int n)
    {
        int numFactor = countFactors(n);
 
        // Calculate product of factors
        long product = power(n, numFactor / 2);
 
        // If numFactor is odd return
        // power(n, numFactor/2) * sqrt(n)
        if (numFactor % 2 == 1)
            product = (product *
                       (int)Math.Sqrt(n)) % M;
 
        return product;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 12;
        Console.Write(multiplyFactors(n));
    }
}
 
// This code is contributed by nitin mittal


PHP
 0)
    {
        if ($y & 1)
            $res = ($res * $x) % $M;
        $y = ($y >> 1) % $M;
        $x = ($x *$x) % $M;
    }
    return $res;
}
 
// function to count the factors
function countFactors( $n)
{
    $count = 0;
    for ($i = 1; $i * $i <= $n; $i++)
    {
        if ($n % $i == 0)
        {
 
            // If factors are equal,
            // count only once
            if ($n / $i == $i)
                $count++;
 
            // Otherwise count both
            else
                $count += 2;
        }
    }
    return $count;
}
 
function multiplyFactors( $n)
{
    $numFactor = countFactors($n);
 
    // Calculate product of factors
    $product = power($n, $numFactor / 2);
 
    // If numFactor is odd return
    // power(n, numFactor/2) * sqrt(n)
    if ($numFactor & 1)
        $product = ($product * sqrt($n)) % $M;
 
    return $product;
}
 
    // Driver code
    $n = 12;
    echo multiplyFactors($n);
     
// This code is contributed by anuj_67.
?>


输出 :

1728

方法3(另一种方法):
让我们观察一件事:

All factors of 12 are: 1, 2, 3, 4, 6, 12
1 * 2 * 3 * (2*2) * (2*3) * (2*2*3) = 2^6 * 3^3 = 12^3
and number of factors are 6

因此,我们可以观察到因子乘积将为n ^(因子数/ 2)。但是,当因子的数量为奇数(这意味着该数量为正方)时,乘积将为n ^(因子的数量/ 2)* sqrt(n)。我们可以计算出与上述方法类似的因素数量。我们可以使用模幂有效地计算功率

C++

// C++ program to calculate product
// of factors of number
#include 
#define M 1000000007
using namespace std;
 
// Iterative Function to calculate
// (x^y) in O(log y)
long long power(long long x, long long y)
{
    long long res = 1;
 
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % M;
        y = (y >> 1) % M;
        x = (x * x) % M;
    }
    return res;
}
 
// function to count the factors
int countFactors(int n)
{
    int count = 0;
    for (int i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
 
            // If factors are equal,
            // count only once
            if (n / i == i)
                count++;
 
            // Otherwise count both
            else
                count += 2;
        }
    }
    return count;
}
 
long long multiplyFactors(int n)
{
    int numFactor = countFactors(n);
 
    // Calculate product of factors
    long long product = power(n, numFactor / 2);
 
    // If numFactor is odd return
    // power(n, numFactor/2) * sqrt(n)
    if (numFactor & 1)
        product = (product * (int)sqrt(n)) % M;
 
    return product;
}
 
// Driver code
int main()
{
    int n = 12;
    cout << multiplyFactors(n) << endl;
    return 0;
}

Java

// Java program to calculate product
// of factors of number
class GFG
{
public static final long M = 1000000007;
 
    // Iterative Function to calculate
    // (x^y) in O(log y)
    static long power(long x, long y)
    {
        long res = 1;
 
        while (y > 0)
        {
            if (y % 2 == 1)
                res = (res * x) % M;
            y = (y >> 1) % M;
            x = (x * x) % M;
        }
        return res;
    }
 
    // function to count the factors
    static int countFactors(int n)
    {
        int count = 0;
        for (int i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
 
                // If factors are equal,
                // count only once
                if (n / i == i)
                    count++;
 
                // Otherwise count both
                else
                    count += 2;
            }
        }
        return count;
    }
 
    static long multiplyFactors(int n)
    {
        int numFactor = countFactors(n);
 
        // Calculate product of factors
        long product = power(n, numFactor / 2);
 
        // If numFactor is odd return
        // power(n, numFactor/2) * sqrt(n)
        if (numFactor % 2 == 1)
            product = (product * (int)Math.sqrt(n)) % M;
 
        return product;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 12;
        System.out.println(multiplyFactors(n));
    }
}

Python

# Python program to calculate product
# of factors of number
 
M = 1000000007
 
# Iterative Function to calculate
# (x^y) in O(log y)
def power(x, y) :
     
    res = 1
    while (y > 0) :
         
        if (y % 2 == 1) :
            res = (res * x) % M
        y = (y >> 1) % M
        x = (x * x) % M
         
    return res
     
# function to count the factors
def countFactors(n) :
    count = 0
    i = 1
    while i * i <= n :
        if (n % i == 0) :
            # If factors are equal,
            # count only once
            if (n / i == i) :
                count = count + 1
             
            # Otherwise count both
            else :
                count = count + 2
        i = i + 1
    return count
     
def multiplyFactors(n) :
     
    numFactor = countFactors(n)
     
    # Calculate product of factors
    product = power(n, numFactor / 2)
 
    # If numFactor is odd return
    # power(n, numFactor/2) * sqrt(n)
    if (numFactor % 2 == 1) :
        product = (product *
                (int)(math.sqrt(n))) % M
 
    return product
     
# Driver Code
n = 12
print multiplyFactors(n)
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to calculate product
// of factors of number
using System;
 
class GFG {
     
    public static long M = 1000000007;
 
    // Iterative Function to calculate
    // (x^y) in O(log y)
    static long power(long x, long y)
    {
        long res = 1;
 
        while (y > 0)
        {
            if (y % 2 == 1)
                res = (res * x) % M;
            y = (y >> 1) % M;
            x = (x * x) % M;
        }
        return res;
    }
 
    // function to count the factors
    static int countFactors(int n)
    {
        int count = 0;
        for (int i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
 
                // If factors are equal,
                // count only once
                if (n / i == i)
                    count++;
 
                // Otherwise count both
                else
                    count += 2;
            }
        }
         
        return count;
    }
 
    static long multiplyFactors(int n)
    {
        int numFactor = countFactors(n);
 
        // Calculate product of factors
        long product = power(n, numFactor / 2);
 
        // If numFactor is odd return
        // power(n, numFactor/2) * sqrt(n)
        if (numFactor % 2 == 1)
            product = (product *
                       (int)Math.Sqrt(n)) % M;
 
        return product;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 12;
        Console.Write(multiplyFactors(n));
    }
}
 
// This code is contributed by nitin mittal

的PHP

 0)
    {
        if ($y & 1)
            $res = ($res * $x) % $M;
        $y = ($y >> 1) % $M;
        $x = ($x *$x) % $M;
    }
    return $res;
}
 
// function to count the factors
function countFactors( $n)
{
    $count = 0;
    for ($i = 1; $i * $i <= $n; $i++)
    {
        if ($n % $i == 0)
        {
 
            // If factors are equal,
            // count only once
            if ($n / $i == $i)
                $count++;
 
            // Otherwise count both
            else
                $count += 2;
        }
    }
    return $count;
}
 
function multiplyFactors( $n)
{
    $numFactor = countFactors($n);
 
    // Calculate product of factors
    $product = power($n, $numFactor / 2);
 
    // If numFactor is odd return
    // power(n, numFactor/2) * sqrt(n)
    if ($numFactor & 1)
        $product = ($product * sqrt($n)) % $M;
 
    return $product;
}
 
    // Driver code
    $n = 12;
    echo multiplyFactors($n);
     
// This code is contributed by anuj_67.
?>

输出 :

1728