给定整数N ,任务是找到表达式(N 1 *(N – 1) 2 *…* 1 N )%(10 9 + 7)的值。
Input: N = 1
Output: 1
Explanation:
11 = 1
Input: N = 4
Output: 288
Explanation:
41 * (4 – 1)2 * (4 – 2)3 * (4-3)4
= 4 * 9 * 8 * 1
= 288
天真的方法:解决此问题的最简单方法是在[1,N]范围内进行迭代。对于第i次迭代,计算(N – i + 1) i的值。最后,打印每次迭代中所有计算值的乘积。
时间复杂度: O(N 2 * log 2 (N))
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
F(N) = N1 * (N – 1)2 * … * 1N
= N * (N – 1) * (N – 1) * (N – 2) * (N – 2) * (N – 2)* … 1 * 1 * 1
= N * (N – 1) * (N – 2)*… * 1 * (N -1) * (N – 2) * …* 1 * …
= N! * (N – 1)! * (N – 2)! * … * 1!
请按照以下步骤解决问题:
- 使用factorial(N)= N * factorial(N – 1)预先计算从1到N的阶乘值。
- 使用以上观察结果,在[1,N]范围内进行迭代,并找到在[1,N]范围内的所有阶乘的乘积
- 最后,打印表达式的值。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
#define mod 1000000007
// Function to find the value of the expression
// ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).
int ValOfTheExpression(int n)
{
// factorial[i]: Stores factorial of i
int factorial[n] = { 0 };
// Base Case for factorial
factorial[0] = factorial[1] = 1;
// Precompute the factorial
for (int i = 2; i <= n; i++) {
factorial[i] = ((factorial[i - 1] % mod)
* (i % mod))
% mod;
}
// dp[N]: Stores the value of the expression
// ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).
int dp[n] = { 0 };
dp[1] = 1;
for (int i = 2; i <= n; i++) {
// Update dp[i]
dp[i] = ((dp[i - 1] % mod)
* (factorial[i] % mod))
% mod;
}
// Return the answer.
return dp[n];
}
// Driver Code
int main()
{
int n = 4;
// Function call
cout << ValOfTheExpression(n) << "\n";
}
Java
// Java program to implement
// the above approach
class GFG
{
static int mod = 1000000007;
// Function to find the value of the expression
// ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).
static int ValOfTheExpression(int n)
{
// factorial[i]: Stores factorial of i
int[] factorial = new int[n + 1];
// Base Case for factorial
factorial[0] = factorial[1] = 1;
// Precompute the factorial
for (int i = 2; i <= n; i++)
{
factorial[i] = ((factorial[i - 1] % mod)
* (i % mod)) % mod;
}
// dp[N]: Stores the value of the expression
// ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).
int[] dp = new int[n + 1];
dp[1] = 1;
for (int i = 2; i <= n; i++)
{
// Update dp[i]
dp[i] = ((dp[i - 1] % mod)
* (factorial[i] % mod)) % mod;
}
// Return the answer.
return dp[n];
}
// Driver code
public static void main(String[] args) {
int n = 4;
// Function call
System.out.println(ValOfTheExpression(n));
}
}
// This code is contributed by divyesh072019
Python3
# Python 3 program to implement
# the above approach
mod = 1000000007
# Function to find the value of the expression
# ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).
def ValOfTheExpression(n):
global mod
# factorial[i]: Stores factorial of i
factorial = [0 for i in range(n + 1)]
# Base Case for factorial
factorial[0] = 1
factorial[1] = 1
# Precompute the factorial
for i in range(2, n + 1, 1):
factorial[i] = ((factorial[i - 1] % mod) * (i % mod))%mod
# dp[N]: Stores the value of the expression
# ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).
dp = [0 for i in range(n+1)]
dp[1] = 1
for i in range(2, n + 1, 1):
# Update dp[i]
dp[i] = ((dp[i - 1] % mod)*(factorial[i] % mod)) % mod
# Return the answer.
return dp[n]
# Driver Code
if __name__ == '__main__':
n = 4
# Function call
print(ValOfTheExpression(n))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program to implement
// the above approach
using System;
class GFG
{
static int mod = 1000000007;
// Function to find the value of the expression
// ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).
static int ValOfTheExpression(int n)
{
// factorial[i]: Stores factorial of i
int[] factorial = new int[n + 1];
// Base Case for factorial
factorial[0] = factorial[1] = 1;
// Precompute the factorial
for (int i = 2; i <= n; i++)
{
factorial[i] = ((factorial[i - 1] % mod)
* (i % mod))
% mod;
}
// dp[N]: Stores the value of the expression
// ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).
int[] dp = new int[n + 1];
dp[1] = 1;
for (int i = 2; i <= n; i++)
{
// Update dp[i]
dp[i] = ((dp[i - 1] % mod)
* (factorial[i] % mod))
% mod;
}
// Return the answer.
return dp[n];
}
// Driver code
static void Main()
{
int n = 4;
// Function call
Console.WriteLine(ValOfTheExpression(n));
}
}
// This code is contributed by divyeshrabadiya07
输出:
288
时间复杂度: O(N)
辅助空间: O(N)