给定一个带有正整数的数组。任务是找到所有数组元素的阶乘。
注意:由于数字会非常大,请使用10 9 +7取模数来打印它们。
例子:
Input: arr[] = {3, 10, 200, 20, 12}
Output: 6 3628800 722479105 146326063 479001600
Input: arr[] = {5, 7, 10}
Output: 120 5040 3628800
天真的方法:我们知道有一种简单的方法可以计算数字的阶乘。我们可以为所有数组值运行一个循环,并可以使用上述方法找到每个数字的阶乘。
时间复杂度为O(N 2 )
空间复杂度将为O(1)
高效的方法:我们知道一个数的阶乘:
N! = N*(N-1)*(N-2)*(N-3)*****3*2*1
计算数字阶乘的递归公式为:
fact(N) = N*fact(N-1).
因此,我们将使用上述递归以自底向上的方式构建一个数组。一旦将值存储在数组中,便可以在O(1)时间内回答查询。因此,总的时间复杂度将为O(N)。仅当数组值小于10 ^ 6时,我们才可以使用此方法。否则,我们将无法将它们存储在数组中。
下面是上述方法的实现:
C/C++
// C++ implementation of the approach
#include
#include
using namespace std;
#define MOD 1000000007
#define SIZE 10000
// Function to calculate the factorial
// using dynamic programing
void factorial(vector& fact)
{
int i;
fact[0] = 1;
for (i = 1; i <= SIZE; i++) {
// Calculation of factorial
// As fact[i-1] stores the factorial of n-1
// so factorial of n is fact[i] = (fact[i-1]*i)
fact[i] = (fact[i - 1] * i) % MOD;
}
}
// Function to print factorial of every element
// of the array
void PrintFactorial(vector &fact,
int arr[],int n){
for(int i=0;i fact(SIZE + 1, 0);
int arr[5] = {3, 10, 200, 20, 12};
int n = sizeof(arr)/sizeof(arr[0]);
// Function to store factorial values mod 10**9+7
factorial(fact);
// Function to print the factorial values mod 10**9+7
PrintFactorial(fact,arr,n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class Sol
{
static int MOD = 1000000007 ;
static int SIZE = 10000;
// vector to store the factorial values
// max_element(arr) should be less than SIZE
static Vector fact = new Vector();
// Function to calculate the factorial
// using dynamic programing
static void factorial()
{
int i;
fact.add((long)1);
for (i = 1; i <= SIZE; i++)
{
// Calculation of factorial
// As fact[i-1] stores the factorial of n-1
// so factorial of n is fact[i] = (fact[i-1]*i)
fact.add((fact.get(i - 1) * i) % MOD);
}
}
// Function to print factorial of every element
// of the array
static void PrintFactorial(int arr[],int n)
{
for(int i = 0; i < n; i += 1)
{
// Printing the stored value of arr[i]!
System.out.print(fact.get(arr[i])+" ");
}
}
// Driver code
public static void main(String args[])
{
int arr[] = {3, 10, 200, 20, 12};
int n = arr.length;
// Function to store factorial values mod 10**9+7
factorial();
// Function to print the factorial values mod 10**9+7
PrintFactorial(arr,n);
}
}
// This code is contributed by Arnab Kundu
Python
# Python implementation of the above Approach
mod = 1000000007
SIZE = 10000
# declaring list initially and making
# it 1 i.e for every index
fact = [1]*SIZE
# Calculation of factorial using Dynamic programing
def factorial():
for i in range(1, SIZE):
# Calculation of factorial
# As fact[i-1] stores the factorial of n-1
# so factorial of n is fact[i] = (fact[i-1]*i)
fact[i] = (fact[i-1]*i) % mod
# function call
factorial()
# Driver code
arr=[3,10,200,20,12]
for i in arr:
print fact[i],
PHP
C#
// C# implementation of above approach
using System.Collections.Generic;
using System;
class Sol
{
static int MOD = 1000000007 ;
static int SIZE = 10000;
// vector to store the factorial values
// max_element(arr) should be less than SIZE
static List fact = new List();
// Function to calculate the factorial
// using dynamic programing
static void factorial()
{
int i;
fact.Add((long)1);
for (i = 1; i <= SIZE; i++)
{
// Calculation of factorial
// As fact[i-1] stores the factorial of n-1
// so factorial of n is fact[i] = (fact[i-1]*i)
fact.Add((fact[i - 1] * i) % MOD);
}
}
// Function to print factorial of every element
// of the array
static void PrintFactorial(int []arr,int n)
{
for(int i = 0; i < n; i += 1)
{
// Printing the stored value of arr[i]!
Console.Write(fact[arr[i]]+" ");
}
}
// Driver code
public static void Main(String []args)
{
int []arr = {3, 10, 200, 20, 12};
int n = arr.Length;
// Function to store factorial values mod 10**9+7
factorial();
// Function to print the factorial values mod 10**9+7
PrintFactorial(arr,n);
}
}
// This code is contributed by 29AjayKumar
输出:
6 3628800 722479105 146326063 479001600
时间复杂度:O(N)
空间复杂度:O(N)