Queries for the product of first N factorials Last Updated : 13 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 = 34560Input: 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++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ll long long #define MAX 1000000 const ll MOD = 1e9 + 7; // Declare result array globally ll result[MAX + 1]; ll fact[MAX + 1]; // Function to precompute the product // of factorials upto MAX void preCompute() { // Initialize base condition if n = 0 // then factorial of 0 is equal to 1 // and answer for n = 0 is 1 fact[0] = 1; result[0] = 1; // Iterate loop from 1 to MAX for (int i = 1; i <= MAX; i++) { // factorial(i) = factorial(i - 1) * i fact[i] = ((fact[i - 1] % MOD) * i) % MOD; // Result for current n is equal to result[i-1] // multiplied by the factorial of i result[i] = ((result[i - 1] % MOD) * (fact[i] % MOD)) % MOD; } } // Function to perform the queries void performQueries(int q[], int n) { // Precomputing the result till MAX preCompute(); // Perform queries for (int i = 0; i < n; i++) cout << result[q[i]] << "\n"; } // Driver code int main() { int q[] = { 4, 5 }; int n = sizeof(q) / sizeof(q[0]); performQueries(q, n); return 0; } Java // Java implementation of the approach import java.io.*; class GFG { static int MAX = 1000000; static int MOD = 10000007; // Declare result array globally static int []result = new int [MAX + 1]; static int []fact = new int [MAX + 1]; // Function to precompute the product // of factorials upto MAX static void preCompute() { // Initialize base condition if n = 0 // then factorial of 0 is equal to 1 // and answer for n = 0 is 1 fact[0] = 1; result[0] = 1; // Iterate loop from 1 to MAX for (int i = 1; i <= MAX; i++) { // factorial(i) = factorial(i - 1) * i fact[i] = ((fact[i - 1] % MOD) * i) % MOD; // Result for current n is equal to result[i-1] // multiplied by the factorial of i result[i] = ((result[i - 1] % MOD) * (fact[i] % MOD)) % MOD; } } // Function to perform the queries static void performQueries(int q[], int n) { // Precomputing the result till MAX preCompute(); // Perform queries for (int i = 0; i < n; i++) System.out.println (result[q[i]]); } // Driver code public static void main (String[] args) { int q[] = { 4, 5 }; int n = q.length; performQueries(q, n); } } // This code is contributed by tushil. Python3 # Python3 implementation of the approach MAX = 1000000 MOD = 10**9 + 7 # Declare result array globally result = [0 for i in range(MAX + 1)] fact = [0 for i in range(MAX + 1)] # Function to precompute the product # of factorials upto MAX def preCompute(): # Initialize base condition if n = 0 # then factorial of 0 is equal to 1 # and answer for n = 0 is 1 fact[0] = 1 result[0] = 1 # Iterate loop from 1 to MAX for i in range(1, MAX + 1): # factorial(i) = factorial(i - 1) * i fact[i] = ((fact[i - 1] % MOD) * i) % MOD # Result for current n is # equal to result[i-1] # multiplied by the factorial of i result[i] = ((result[i - 1] % MOD) * (fact[i] % MOD)) % MOD # Function to perform the queries def performQueries(q, n): # Precomputing the result tiMAX preCompute() # Perform queries for i in range(n): print(result[q[i]]) # Driver code q = [4, 5] n = len(q) performQueries(q, n) # This code is contributed by Mohit Kumar C# // C# implementation of the approach using System; class GFG { static int MAX = 1000000; static int MOD = 10000007; // Declare result array globally static int []result = new int [MAX + 1]; static int []fact = new int [MAX + 1]; // Function to precompute the product // of factorials upto MAX static void preCompute() { // Initialize base condition if n = 0 // then factorial of 0 is equal to 1 // and answer for n = 0 is 1 fact[0] = 1; result[0] = 1; // Iterate loop from 1 to MAX for (int i = 1; i <= MAX; i++) { // factorial(i) = factorial(i - 1) * i fact[i] = ((fact[i - 1] % MOD) * i) % MOD; // Result for current n is equal to result[i-1] // multiplied by the factorial of i result[i] = ((result[i - 1] % MOD) * (fact[i] % MOD)) % MOD; } } // Function to perform the queries static void performQueries(int []q, int n) { // Precomputing the result till MAX preCompute(); // Perform queries for (int i = 0; i < n; i++) Console.WriteLine(result[q[i]]); } // Driver code public static void Main (String[] args) { int []q = { 4, 5 }; int n = q.Length; performQueries(q, n); } } // This code is contributed by 29AjayKumar JavaScript <script> // Javascript implementation of the approach let MAX = 1000000; let MOD = 10000007; // Declare result array globally let result = new Array(MAX + 1); result.fill(0); let fact = new Array(MAX + 1); fact.fill(0); // Function to precompute the product // of factorials upto MAX function preCompute() { // Initialize base condition if n = 0 // then factorial of 0 is equal to 1 // and answer for n = 0 is 1 fact[0] = 1; result[0] = 1; // Iterate loop from 1 to MAX for (let i = 1; i <= MAX; i++) { // factorial(i) = factorial(i - 1) * i fact[i] = ((fact[i - 1] % MOD) * i) % MOD; // Result for current n is equal to result[i-1] // multiplied by the factorial of i result[i] = ((result[i - 1] % MOD) * (fact[i] % MOD)) % MOD; } } // Function to perform the queries function performQueries(q, n) { // Precomputing the result till MAX preCompute(); // Perform queries 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> Output: 288 34560 Time Complexity: O(MAX) Auxiliary Space: O(MAX) Comment More infoAdvertise with us Next Article Range sum queries without updates U UdayBhardwaj Follow Improve Article Tags : Misc Mathematical DSA factorial Practice Tags : factorialMathematicalMisc Similar Reads PreComputation Technique on Arrays Precomputation refers to the process of pre-calculating and storing the results of certain computations or data structures(array in this case) in advance, in order to speed up the execution time of a program. This can be useful in situations where the same calculations are needed multiple times, as 15 min read 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 7 min read Range sum queries without updates Given an array arr of integers of size n. We need to compute the sum of elements from index i to index j. The queries consisting of i and j index values will be executed multiple times.Examples: Input : arr[] = {1, 2, 3, 4, 5} i = 1, j = 3 i = 2, j = 4Output : 9 12 Input : arr[] = {1, 2, 3, 4, 5} i 6 min read Range Queries for Frequencies of array elements Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; lef 13 min read Count Primes in Ranges Given a 2d array queries[][] of size n, where each query queries[i] contain 2 elements [l, r], your task is to find the count of number of primes in inclusive range [l, r]Examples: Input: queries[][] = [ [1, 10], [5, 10], [11, 20] ]Output: 4 2 4Explanation: For query 1, number of primes in range [1, 12 min read Check in binary array the number represented by a subarray is odd or even Given an array such that all its terms is either 0 or 1.You need to tell the number represented by a subarray a[l..r] is odd or even Examples : Input : arr = {1, 1, 0, 1} l = 1, r = 3 Output : odd number represented by arr[l...r] is 101 which 5 in decimal form which is odd Input : arr = {1, 1, 1, 1} 4 min read GCDs of given index ranges in an Array Given an array arr[] of size N and Q queries of type {qs, qe} where qs and qe denote the starting and ending index of the query, the task is to find the GCD of all the numbers in the range. Examples: Input: arr[] = {2, 3, 60, 90, 50};Index Ranges: {1, 3}, {2, 4}, {0, 2}Output: GCDs of given ranges a 14 min read Mean of range in array Given an array arr[] of n integers and q queries represented by an array queries[][], where queries[i][0] = l and queries[i][1] = r. For each query, the task is to calculate the mean of elements in the range l to r and return its floor value. Examples: Input: arr[] = [3, 7, 2, 8, 5] queries[][] = [[ 12 min read Difference Array | Range update query in O(1) You are given an integer array arr[] and a list of queries. Each query is represented as a list of integers where:[1, l, r, x]: Adds x to all elements from arr[l] to arr[r] (inclusive).[2]: Prints the current state of the array.You need to perform the queries in order.Examples : Input: arr[] = [10, 10 min read Range sum query using Sparse Table We have an array arr[]. We need to find the sum of all the elements in the range L and R where 0 <= L <= R <= n-1. Consider a situation when there are many range queries. Examples: Input : 3 7 2 5 8 9 query(0, 5) query(3, 5) query(2, 4) Output : 34 22 15Note : array is 0 based indexed and q 8 min read Like