原文:

给定一个数 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 整除,则乘以数字。该pg电子试玩链接的解决方案的时间复杂度为 0(n)。 但是这种方法对于大的 n 值是不够的。 方法 2(更好的方法): 更好的方法是运行 i 从 1 到 sqrt(n)的循环。如果数能被 i 整除,用 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 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.

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

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

所以我们可以观察到因子的乘积将是因子/2 的 n^(number)。但是当因子数是奇数时(这意味着该数是完美平方),在这种情况下,乘积将是因子/2 的 n^(number)* 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 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

服务器端编程语言(professional hypertext preprocessor 的缩写)

 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.
?>

java 描述语言

   

输出:

1728

本文由核素投稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 contribute@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果你发现任何不正确的地方,或者你想分享更多关于上面讨论的话题的信息,请写评论。