原文:

给定一个数 n,求模 1000000007 的第一个 n 阶乘的乘积。

约束: 1 ≤ n ≤ 1e6

示例:

input : 3
output : 12
explanation: 1! * 2! * 3! = 12 mod (1e9   7) = 12
input : 5
output : 34560

先决条件: 方法:解决这个问题背后的基本思路就是只考虑这样大数乘法时的溢出问题,即阶乘。因此,它需要通过递归相乘来解决溢出的困难。此外,在迭代计算阶乘和模乘时,我们必须在每一步取模。

facti = facti-1 * i
where facti is the factorial of ith number
prodi = prodi-1 * facti
where prodi is the product of first i factorials

为了求模下两个大数的乘积,我们使用与模下

c

// cpp program to find the
// product of first n factorials
#include 
using namespace std;
// to compute (a * b) % mod
long long int mulmod(long long int a, long long int b,
                                    long long int mod)
{
    long long int res = 0; // initialize result
    a = a % mod;
    while (b > 0) {
        // if b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res   a) % mod;
        // multiply 'a' with 2
        a = (a * 2) % mod;
        // divide b by 2
        b /= 2;
    }
    // return result
    return res % mod;
}
// this function computes factorials and
// product by using above function i.e.
// modular multiplication
long long int findproduct(long long int n)
{
    // initialize product and fact with 1
    long long int product = 1, fact = 1;
    long long int mod = 1e9   7;
    for (int i = 1; i <= n; i  ) {
        // ith factorial
        fact = mulmod(fact, i, mod);
        // product of first i factorials
        product = mulmod(product, fact, mod);
        // if at any iteration, product becomes
        // divisible by mod, simply return 0;
        if (product == 0)
            return 0;
    }
    return product;
}
// driver code to test above functions
int main()
{
    long long int n = 3;
    cout << findproduct(n) << endl;
    n = 5;
    cout << findproduct(n) << endl;
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to find the
// product of first n factorials
class gfg{
// to compute (a * b) % mod
static double mulmod(long a, long b,
                                    long mod)
{
    long res = 0; // initialize result
    a = a % mod;
    while (b > 0) {
        // if b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res   a) % mod;
        // multiply 'a' with 2
        a = (a * 2) % mod;
        // divide b by 2
        b /= 2;
    }
    // return result
    return res % mod;
}
// this function computes factorials and
// product by using above function i.e.
// modular multiplication
static long findproduct(long n)
{
    // initialize product and fact with 1
    long product = 1, fact = 1;
    long mod = (long)(1e9   7);
    for (int i = 1; i <= n; i  ) {
        // ith factorial
        fact = (long)mulmod(fact, i, mod);
        // product of first i factorials
        product = (long)mulmod(product, fact, mod);
        // if at any iteration, product becomes
        // divisible by mod, simply return 0;
        if (product == 0)
            return 0;
    }
    return product;
}
// driver code to test above functions
public static void main(string[] args)
{
    long n = 3;
    system.out.println(findproduct(n));
    n = 5;
    system.out.println(findproduct(n));
}
}
// this code is contributed by mits

python 3

# python program to find the
# product of first n factorials
# to compute (a * b) % mod
def mulmod(a, b, mod):
    res = 0 # initialize result
    a = a % mod
    while (b > 0):
        # if b is odd, add 'a' to result
        if (b % 2 == 1):
            res = (res   a) % mod
        # multiply 'a' with 2
        a = (a * 2) % mod
        # divide b by 2
        b //= 2
    # return result
    return res % mod
# this function computes factorials and
# product by using above function i.e.
# modular multiplication
def findproduct(n):
    # initialize product and fact with 1
    product = 1; fact = 1
    mod = 1e9   7
    for i in range(1, n 1):
        # ith factorial
        fact = mulmod(fact, i, mod)
        # product of first i factorials
        product = mulmod(product, fact, mod)
        # if at any iteration, product becomes
        # divisible by mod, simply return 0
        if not product:
            return 0
    return int(product)
# driver code to test above functions
n = 3
print(findproduct(n))
n = 5
print(findproduct(n))
# this code is contributed by ansu kumari

c

// c#  program to find the
// product of first n factorials
using system;
public class gfg{
    // to compute (a * b) % mod
static double mulmod(long a, long b,
                                    long mod)
{
    long res = 0; // initialize result
    a = a % mod;
    while (b > 0) {
        // if b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res   a) % mod;
        // multiply 'a' with 2
        a = (a * 2) % mod;
        // divide b by 2
        b /= 2;
    }
    // return result
    return res % mod;
}
// this function computes factorials and
// product by using above function i.e.
// modular multiplication
static long findproduct(long n)
{
    // initialize product and fact with 1
    long product = 1, fact = 1;
    long mod = (long)(1e9   7);
    for (int i = 1; i <= n; i  ) {
        // ith factorial
        fact = (long)mulmod(fact, i, mod);
        // product of first i factorials
        product = (long)mulmod(product, fact, mod);
        // if at any iteration, product becomes
        // divisible by mod, simply return 0;
        if (product == 0)
            return 0;
    }
    return product;
}
// driver code to test above functions
    static public void main (){
        long n = 3;
        console.writeline(findproduct(n));
        n = 5;
        console.writeline(findproduct(n));
}
}
//this code is contributed by ajit.

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

 0)
    {
        // if b is odd, add 'a' to result
        if ($b % 2 == 1)
            $res = ($res   $a) % $mod;
        // multiply 'a' with 2
        $a = ($a * 2) % $mod;
        // divide b by 2
        $b /= 2;
    }
    // return result
    return $res % $mod;
}
// this function computes factorials and
// product by using above function i.e.
// modular multiplication
function findproduct($n)
{
    // initialize product and fact with 1
    $product = 1;
    $fact = 1;
    $mod = 1000000000;
    for ($i = 1; $i <= $n; $i  )
    {
        // ith factorial
        $fact = mulmod($fact, $i, $mod);
        // product of first i factorials
        $product = mulmod($product, $fact, $mod);
        // if at any iteration, product becomes
        // divisible by mod, simply return 0;
        if ($product == 0)
            return 0;
    }
    return $product;
}
// driver code
$n = 3;
echo findproduct($n),"\n";
$n = 5;
echo findproduct($n),"\n";
// this code is contributed by ajit
?>

java 描述语言


output: 

12
34560

时间复杂度: o(n * logn),其中 o(log n)为模乘的时间复杂度。