Queries for the product of first N factorials
Given Q[] queries where each query consists of an integer N, the task is to find the product of first N factorials for each of the query. Since the result could be large, compute it modulo 109 + 7.
Examples:
Input: Q[] = {4, 5}
Output:
288
34560
Query 1: 1! * 2! * 3! * 4! = 1 * 2 * 6 * 24 = 288
Query 2: 1! * 2! * 3! * 4! * 5! = 1 * 2 * 6 * 24 * 120 = 34560
Input: Q[] = {500, 1000, 7890}
Output:
976141892
560688561
793351288
Approach: Create two arrays result[] and fact[] where fact[i] will store the factorial of i and result[i] will store the product of first i factorial.
Initialise fact[0] = 1 and result[0] = 1. Now for the rest of the values, the recurrence relation will be:
fact[i] = fact[i – 1] * i
result[i] = result[i – 1] * fact[i]
Now, each query can be answered using the result[] array generated.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 1000000
const ll MOD = 1e9 + 7;
ll result[MAX + 1];
ll fact[MAX + 1];
void preCompute()
{
fact[0] = 1;
result[0] = 1;
for ( int i = 1; i <= MAX; i++) {
fact[i] = ((fact[i - 1] % MOD) * i) % MOD;
result[i] = ((result[i - 1] % MOD) * (fact[i] % MOD)) % MOD;
}
}
void performQueries( int q[], int n)
{
preCompute();
for ( int i = 0; i < n; i++)
cout << result[q[i]] << "\n" ;
}
int main()
{
int q[] = { 4, 5 };
int n = sizeof (q) / sizeof (q[0]);
performQueries(q, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MAX = 1000000 ;
static int MOD = 10000007 ;
static int []result = new int [MAX + 1 ];
static int []fact = new int [MAX + 1 ];
static void preCompute()
{
fact[ 0 ] = 1 ;
result[ 0 ] = 1 ;
for ( int i = 1 ; i <= MAX; i++)
{
fact[i] = ((fact[i - 1 ] % MOD) * i) % MOD;
result[i] = ((result[i - 1 ] % MOD) *
(fact[i] % MOD)) % MOD;
}
}
static void performQueries( int q[], int n)
{
preCompute();
for ( int i = 0 ; i < n; i++)
System.out.println (result[q[i]]);
}
public static void main (String[] args)
{
int q[] = { 4 , 5 };
int n = q.length;
performQueries(q, n);
}
}
|
Python3
MAX = 1000000
MOD = 10 * * 9 + 7
result = [ 0 for i in range ( MAX + 1 )]
fact = [ 0 for i in range ( MAX + 1 )]
def preCompute():
fact[ 0 ] = 1
result[ 0 ] = 1
for i in range ( 1 , MAX + 1 ):
fact[i] = ((fact[i - 1 ] % MOD) * i) % MOD
result[i] = ((result[i - 1 ] % MOD) *
(fact[i] % MOD)) % MOD
def performQueries(q, n):
preCompute()
for i in range (n):
print (result[q[i]])
q = [ 4 , 5 ]
n = len (q)
performQueries(q, n)
|
C#
using System;
class GFG
{
static int MAX = 1000000;
static int MOD = 10000007;
static int []result = new int [MAX + 1];
static int []fact = new int [MAX + 1];
static void preCompute()
{
fact[0] = 1;
result[0] = 1;
for ( int i = 1; i <= MAX; i++)
{
fact[i] = ((fact[i - 1] % MOD) * i) % MOD;
result[i] = ((result[i - 1] % MOD) *
(fact[i] % MOD)) % MOD;
}
}
static void performQueries( int []q, int n)
{
preCompute();
for ( int i = 0; i < n; i++)
Console.WriteLine(result[q[i]]);
}
public static void Main (String[] args)
{
int []q = { 4, 5 };
int n = q.Length;
performQueries(q, n);
}
}
|
Javascript
<script>
let MAX = 1000000;
let MOD = 10000007;
let result = new Array(MAX + 1);
result.fill(0);
let fact = new Array(MAX + 1);
fact.fill(0);
function preCompute()
{
fact[0] = 1;
result[0] = 1;
for (let i = 1; i <= MAX; i++)
{
fact[i] = ((fact[i - 1] % MOD) * i) % MOD;
result[i] = ((result[i - 1] % MOD) *
(fact[i] % MOD)) % MOD;
}
}
function performQueries(q, n)
{
preCompute();
for (let i = 0; i < n; i++)
document.write(result[q[i]] + "</br>" );
}
let q = [ 4, 5 ];
let n = q.length;
performQueries(q, n);
</script>
|
Time Complexity: O(MAX)
Auxiliary Space: O(MAX)
Last Updated :
13 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...