一个孩子正在跑一个有 n 级台阶的楼梯,一次可以跳 1 级、2 级或 3 级。实施一种方法来计算孩子可以通过多少种可能的方式跑上楼梯。
例子:
Input : 4
Output : 7
Explanation:
Below are the seven ways
1 step + 1 step + 1 step + 1 step
1 step + 2 step + 1 step
2 step + 1 step + 1 step
1 step + 1 step + 2 step
2 step + 2 step
3 step + 1 step
1 step + 3 step
Input : 3
Output : 4
Explanation:
Below are the four ways
1 step + 1 step + 1 step
1 step + 2 step
2 step + 1 step
3 step
有两种方法可以解决这个问题:
- 递归方法
- 动态规划
方法一:递归。
有n个楼梯,一个人可以跳下一个楼梯,跳过一个楼梯或跳过两个楼梯。所以有n个楼梯。因此,如果一个人站在第 i 个楼梯,则该人可以移动到第 i+1、第 i+2、第 i+3 个楼梯。可以形成递归函数,其中在当前索引 i 处,为第 i+1、i+2 和第 i+3 个阶梯递归调用该函数。
还有另一种形成递归函数。要到达第 i 个楼梯,一个人必须从第 i-1、i-2 或 i-3 楼梯跳,否则 i 是起始楼梯。
算法:
- 创建一个仅接受一个参数的递归函数(count(int n))。
- 检查基本情况。如果 n 的值小于 0,则返回 0,如果 n 的值等于 0,则返回 1,因为它是起始阶梯。
- 使用值 n-1、n-2 和 n-3 递归调用该函数并对返回的值求和,即 sum = count(n-1) + count(n-2) + count(n-3)
- 返回总和的值。
C++
// C++ Program to find n-th stair using step size
// 1 or 2 or 3.
#include
using namespace std;
class GFG {
// Returns count of ways to reach n-th stair
// using 1 or 2 or 3 steps.
public:
int findStep(int n)
{
if (n == 1 || n == 0)
return 1;
else if (n == 2)
return 2;
else
return findStep(n - 3) + findStep(n - 2)
+ findStep(n - 1);
}
};
// Driver code
int main()
{
GFG g;
int n = 4;
cout << g.findStep(n);
return 0;
}
// This code is contributed by SoM15242
C
// Program to find n-th stair using step size
// 1 or 2 or 3.
#include
// Returns count of ways to reach n-th stair
// using 1 or 2 or 3 steps.
int findStep(int n)
{
if (n == 1 || n == 0)
return 1;
else if (n == 2)
return 2;
else
return findStep(n - 3) + findStep(n - 2) + findStep(n - 1);
}
// Driver code
int main()
{
int n = 4;
printf("%d\n", findStep(n));
return 0;
}
Java
// Program to find n-th stair
// using step size 1 or 2 or 3.
import java.util.*;
import java.lang.*;
public class GfG {
// Returns count of ways to reach
// n-th stair using 1 or 2 or 3 steps.
public static int findStep(int n)
{
if (n == 1 || n == 0)
return 1;
else if (n == 2)
return 2;
else
return findStep(n - 3) + findStep(n - 2) + findStep(n - 1);
}
// Driver function
public static void main(String argc[])
{
int n = 4;
System.out.println(findStep(n));
}
}
/* This code is contributed by Sagar Shukla */
Python
# Python program to find n-th stair
# using step size 1 or 2 or 3.
# Returns count of ways to reach n-th
# stair using 1 or 2 or 3 steps.
def findStep( n) :
if (n == 1 or n == 0) :
return 1
elif (n == 2) :
return 2
else :
return findStep(n - 3) + findStep(n - 2) + findStep(n - 1)
# Driver code
n = 4
print(findStep(n))
# This code is contributed by Nikita Tiwari.
C#
// Program to find n-th stair
// using step size 1 or 2 or 3.
using System;
public class GfG {
// Returns count of ways to reach
// n-th stair using 1 or 2 or 3 steps.
public static int findStep(int n)
{
if (n == 1 || n == 0)
return 1;
else if (n == 2)
return 2;
else
return findStep(n - 3) + findStep(n - 2) + findStep(n - 1);
}
// Driver function
public static void Main()
{
int n = 4;
Console.WriteLine(findStep(n));
}
}
/* This code is contributed by vt_m */
PHP
Javascript
C++
// A C++ program to count number of ways
// to reach n't stair when
#include
using namespace std;
// A recursive function used by countWays
int countWays(int n)
{
int res[n + 1];
res[0] = 1;
res[1] = 1;
res[2] = 2;
for (int i = 3; i <= n; i++)
res[i] = res[i - 1] + res[i - 2]
+ res[i - 3];
return res[n];
}
// Driver program to test above functions
int main()
{
int n = 4;
cout << countWays(n);
return 0;
}
//This code is contributed by shubhamsingh10
C
// A C program to count number of ways
// to reach n't stair when
#include
// A recursive function used by countWays
int countWays(int n)
{
int res[n + 1];
res[0] = 1;
res[1] = 1;
res[2] = 2;
for (int i = 3; i <= n; i++)
res[i] = res[i - 1] + res[i - 2]
+ res[i - 3];
return res[n];
}
// Driver program to test above functions
int main()
{
int n = 4;
printf("%d", countWays(n));
return 0;
}
Java
// Program to find n-th stair
// using step size 1 or 2 or 3.
import java.util.*;
import java.lang.*;
public class GfG {
// A recursive function used by countWays
public static int countWays(int n)
{
int[] res = new int[n + 1];
res[0] = 1;
res[1] = 1;
res[2] = 2;
for (int i = 3; i <= n; i++)
res[i] = res[i - 1] + res[i - 2]
+ res[i - 3];
return res[n];
}
// Driver function
public static void main(String argc[])
{
int n = 4;
System.out.println(countWays(n));
}
}
/* This code is contributed by Sagar Shukla */
Python
# Python program to find n-th stair
# using step size 1 or 2 or 3.
# A recursive function used by countWays
def countWays(n) :
res = [0] * (n + 2)
res[0] = 1
res[1] = 1
res[2] = 2
for i in range(3, n + 1) :
res[i] = res[i - 1] + res[i - 2] + res[i - 3]
return res[n]
# Driver code
n = 4
print(countWays(n))
# This code is contributed by Nikita Tiwari.
C#
// Program to find n-th stair
// using step size 1 or 2 or 3.
using System;
public class GfG {
// A recursive function used by countWays
public static int countWays(int n)
{
int[] res = new int[n + 2];
res[0] = 1;
res[1] = 1;
res[2] = 2;
for (int i = 3; i <= n; i++)
res[i] = res[i - 1] + res[i - 2]
+ res[i - 3];
return res[n];
}
// Driver function
public static void Main()
{
int n = 4;
Console.WriteLine(countWays(n));
}
}
/* This code is contributed by vt_m */
PHP
Javascript
C++
#include
#define k 3
using namespace std;
// Multiply Two Matrix Function
vector> multiply(vector> A, vector> B) {
// third matrix to store multiplication of Two matrix9*
vector> C(k + 1, vector(k + 1));
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= k; j++) {
for (int x = 1; x <= k; x++) {
C[i][j] = (C[i][j] + (A[i][x] * B[x][j]));
}
}
}
return C;
}
// Optimal Way For finding pow(t,n)
// If n Is Odd then It Will be t*pow(t,n-1)
// else return pow(t,n/2)*pow(t,n/2)
vector> pow(vector> t, int n) {
// base Case
if (n == 1) {
return t;
}
// Recurrence Case
if (n & 1) {
return multiply(t, pow(t, n - 1));
} else {
vector> X = pow(t, n / 2);
return multiply(X, X);
}
}
int compute(int n) {
// base Case
if (n == 0) return 1;
if (n == 1) return 1;
if (n == 2) return 2;
// Function Vector(indexing 1 )
// that is [1,2]
int f1[k + 1] = {};
f1[1] = 1;
f1[2] = 2;
f1[3] = 4;
// Constucting Transformation Matrix that will be
/*[[0,1,0],[0,0,1],[3,2,1]]
*/
vector> t(k + 1, vector(k + 1));
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= k; j++) {
if (i < k) {
// Store 1 in cell that is next to diagonal of Matrix else Store 0 in
// cell
if (j == i + 1) {
t[i][j] = 1;
} else {
t[i][j] = 0;
}
continue;
}
// Last Row - store the Coefficients in reverse order
t[i][j] = 1;
}
}
// Computing T^(n-1) and Setting Tranformation matrix T to T^(n-1)
t = pow(t, n - 1);
int sum = 0;
// Computing first cell (row=1,col=1) For Resultant Matrix TXF
for (int i = 1; i <= k; i++) {
sum += t[1][i] * f1[i];
}
return sum;
}
int main() {
int n = 4;
cout << compute(n) << endl;
n = 5;
cout << compute(n) << endl;
n = 10;
cout << compute(n) << endl;
return 0;
}
Java
import java.io.*;
import java.util.*;
class GFG{
static int k = 3;
// Multiply Two Matrix Function
static int[][] multiply(int[][] A, int[][] B)
{
// Third matrix to store multiplication
// of Two matrix9*
int[][] C = new int[k + 1][k + 1];
for(int i = 1; i <= k; i++)
{
for(int j = 1; j <= k; j++)
{
for(int x = 1; x <= k; x++)
{
C[i][j] = (C[i][j] + (A[i][x] * B[x][j]));
}
}
}
return C;
}
// Optimal Way For finding pow(t,n)
// If n Is Odd then It Will be t*pow(t,n-1)
// else return pow(t,n/2)*pow(t,n/2)
static int[][] pow(int[][] t, int n)
{
// Base Case
if (n == 1)
{
return t;
}
// Recurrence Case
if ((n & 1) != 0)
{
return multiply(t, pow(t, n - 1));
}
else
{
int[][] X = pow(t, n / 2);
return multiply(X, X);
}
}
static int compute(int n)
{
// Base Case
if (n == 0) return 1;
if (n == 1) return 1;
if (n == 2) return 2;
// Function int(indexing 1 )
// that is [1,2]
int f1[]=new int[k + 1];
f1[1] = 1;
f1[2] = 2;
f1[3] = 4;
// Constucting Transformation Matrix that will be
/*[[0,1,0],[0,0,1],[3,2,1]]
*/
int[][] t = new int[k + 1][k + 1];
for(int i = 1; i <= k; i++)
{
for(int j = 1; j <= k; j++)
{
if (i < k)
{
// Store 1 in cell that is next to
// diagonal of Matrix else Store 0 in
// cell
if (j == i + 1)
{
t[i][j] = 1;
}
else
{
t[i][j] = 0;
}
continue;
}
// Last Row - store the Coefficients
// in reverse order
t[i][j] = 1;
}
}
// Computing T^(n-1) and Setting
// Tranformation matrix T to T^(n-1)
t = pow(t, n - 1);
int sum = 0;
// Computing first cell (row=1,col=1)
// For Resultant Matrix TXF
for(int i = 1; i <= k; i++)
{
sum += t[1][i] * f1[i];
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
// Input
int n = 4;
System.out.println(compute(n));
n = 5;
System.out.println(compute(n));
n = 10;
System.out.println(compute(n));
}
}
// This code is contributed by Shubhamsingh10
Python3
k = 3
# Multiply Two Matrix Function
def multiply(A, B):
# third matrix to store multiplication of Two matrix9*
C = [[0 for x in range(k+1)] for y in range(k+1)]
for i in range(1, k+1):
for j in range(1, k+1):
for x in range(1, k+1):
C[i][j] = (C[i][j] + (A[i][x] * B[x][j]))
return C
# Optimal Way For finding pow(t,n)
# If n Is Odd then It Will be t*pow(t,n-1)
# else return pow(t,n/2)*pow(t,n/2)
def pow(t, n):
# base Case
if (n == 1):
return t
# Recurrence Case
if (n & 1):
return multiply(t, pow(t, n - 1))
else:
X = pow(t, n // 2)
return multiply(X, X)
def compute( n):
# base Case
if (n == 0):
return 1
if (n == 1):
return 1
if (n == 2):
return 2
# Function Vector(indexing 1 )
# that is [1,2]
f1 = [0]*(k + 1)
f1[1] = 1
f1[2] = 2
f1[3] = 4
# Constucting Transformation Matrix that will be
#[[0,1,0],[0,0,1],[3,2,1]]
t = [[0 for x in range(k+1)] for y in range(k+1)]
for i in range(1, k+1):
for j in range(1, k+1):
if (i < k):
# Store 1 in cell that is next to diagonal of Matrix else Store 0 in
# cell
if (j == i + 1):
t[i][j] = 1
else:
t[i][j] = 0
continue
# Last Row - store the Coefficients in reverse order
t[i][j] = 1
# Computing T^(n-1) and Setting Tranformation matrix T to T^(n-1)
t = pow(t, n - 1)
sum = 0
# Computing first cell (row=1,col=1) For Resultant Matrix TXF
for i in range(1, k+1):
sum += t[1][i] * f1[i]
return sum
# Driver Code
n = 4
print(compute(n))
n = 5
print(compute(n))
n = 10
print(compute(n))
# This code is contributed by Shubhamsingh10
C#
// C# program for the above approach
using System;
class GFG{
static int k = 3;
// Multiply Two Matrix Function
static int[,] multiply(int[,] A, int[,] B)
{
// Third matrix to store multiplication
// of Two matrix9*
int[,] C = new int[k + 1,k + 1];
for(int i = 1; i <= k; i++)
{
for(int j = 1; j <= k; j++)
{
for(int x = 1; x <= k; x++)
{
C[i,j] = (C[i,j] + (A[i,x] * B[x,j]));
}
}
}
return C;
}
// Optimal Way For finding pow(t,n)
// If n Is Odd then It Will be t*pow(t,n-1)
// else return pow(t,n/2)*pow(t,n/2)
static int[,] pow(int[,] t, int n)
{
// Base Case
if (n == 1)
{
return t;
}
// Recurrence Case
if ((n & 1) != 0)
{
return multiply(t, pow(t, n - 1));
}
else
{
int[,] X = pow(t, n / 2);
return multiply(X, X);
}
}
static int compute(int n)
{
// Base Case
if (n == 0) return 1;
if (n == 1) return 1;
if (n == 2) return 2;
// Function int(indexing 1 )
// that is [1,2]
int[] f1=new int[k + 1];
f1[1] = 1;
f1[2] = 2;
f1[3] = 4;
// Constucting Transformation Matrix that will be
/*[[0,1,0],[0,0,1],[3,2,1]]
*/
int[,] t = new int[k + 1,k + 1];
for(int i = 1; i <= k; i++)
{
for(int j = 1; j <= k; j++)
{
if (i < k)
{
// Store 1 in cell that is next to
// diagonal of Matrix else Store 0 in
// cell
if (j == i + 1)
{
t[i,j] = 1;
}
else
{
t[i,j] = 0;
}
continue;
}
// Last Row - store the Coefficients
// in reverse order
t[i,j] = 1;
}
}
// Computing T^(n-1) and Setting
// Tranformation matrix T to T^(n-1)
t = pow(t, n - 1);
int sum = 0;
// Computing first cell (row=1,col=1)
// For Resultant Matrix TXF
for(int i = 1; i <= k; i++)
{
sum += t[1,i] * f1[i];
}
return sum;
}
// Driver Code
static public void Main (){
// Input
int n = 4;
Console.WriteLine(compute(n));
n = 5;
Console.WriteLine(compute(n));
n = 10;
Console.WriteLine(compute(n));
}
}
// This code is contributed by Shubhamsingh10
Javascript
输出 :
7
在职的:
复杂度分析:
- 时间复杂度: O(3 n )。
上述解决方案的时间复杂度是指数级的,接近的上限将是 O(3 n )。从每个状态调用 3 个递归函数。所以 n 个状态的上限是 O(3 n )。 - 空间复杂度: O(1)。
因为不需要额外的空间。
注意:程序的时间复杂度可以使用动态规划进行优化。
方法二:动态规划。
思路类似,但是可以观察到有n个状态但是递归函数被调用了3^n次。这意味着某些状态会被重复调用。所以这个想法是存储状态的值。这可以通过两种方式完成。
- 自上而下的方法:第一种方法是保持递归结构不变,只将值存储在 HashMap 中,每当再次调用该函数时,不计算 () 就返回值存储。
- 自底向上方法:第二种方法是取一个额外的大小为 n 的空间,开始计算从 1, 2 .. 到 n 的状态值,即计算 i, i+1, i+2 的值,然后将它们用于计算 i+3 的值。
算法:
- 创建一个大小为 n + 1 的数组,并用 1、1、2 初始化前 3 个变量。基本情况。
- 运行从 3 到 n 的循环。
- 对于每个索引 i,第 i 个位置的计算机值为 dp[i] = dp[i-1] + dp[i-2] + dp[i-3]。
- 打印 dp[n] 的值,作为到达第 n 步的方法数的计数。
C++
// A C++ program to count number of ways
// to reach n't stair when
#include
using namespace std;
// A recursive function used by countWays
int countWays(int n)
{
int res[n + 1];
res[0] = 1;
res[1] = 1;
res[2] = 2;
for (int i = 3; i <= n; i++)
res[i] = res[i - 1] + res[i - 2]
+ res[i - 3];
return res[n];
}
// Driver program to test above functions
int main()
{
int n = 4;
cout << countWays(n);
return 0;
}
//This code is contributed by shubhamsingh10
C
// A C program to count number of ways
// to reach n't stair when
#include
// A recursive function used by countWays
int countWays(int n)
{
int res[n + 1];
res[0] = 1;
res[1] = 1;
res[2] = 2;
for (int i = 3; i <= n; i++)
res[i] = res[i - 1] + res[i - 2]
+ res[i - 3];
return res[n];
}
// Driver program to test above functions
int main()
{
int n = 4;
printf("%d", countWays(n));
return 0;
}
Java
// Program to find n-th stair
// using step size 1 or 2 or 3.
import java.util.*;
import java.lang.*;
public class GfG {
// A recursive function used by countWays
public static int countWays(int n)
{
int[] res = new int[n + 1];
res[0] = 1;
res[1] = 1;
res[2] = 2;
for (int i = 3; i <= n; i++)
res[i] = res[i - 1] + res[i - 2]
+ res[i - 3];
return res[n];
}
// Driver function
public static void main(String argc[])
{
int n = 4;
System.out.println(countWays(n));
}
}
/* This code is contributed by Sagar Shukla */
Python
# Python program to find n-th stair
# using step size 1 or 2 or 3.
# A recursive function used by countWays
def countWays(n) :
res = [0] * (n + 2)
res[0] = 1
res[1] = 1
res[2] = 2
for i in range(3, n + 1) :
res[i] = res[i - 1] + res[i - 2] + res[i - 3]
return res[n]
# Driver code
n = 4
print(countWays(n))
# This code is contributed by Nikita Tiwari.
C#
// Program to find n-th stair
// using step size 1 or 2 or 3.
using System;
public class GfG {
// A recursive function used by countWays
public static int countWays(int n)
{
int[] res = new int[n + 2];
res[0] = 1;
res[1] = 1;
res[2] = 2;
for (int i = 3; i <= n; i++)
res[i] = res[i - 1] + res[i - 2]
+ res[i - 3];
return res[n];
}
// Driver function
public static void Main()
{
int n = 4;
Console.WriteLine(countWays(n));
}
}
/* This code is contributed by vt_m */
PHP
Javascript
输出 :
7
- 在职的:
1 -> 1 -> 1 -> 1
1 -> 1 -> 2
1 -> 2 -> 1
1 -> 3
2 -> 1 -> 1
2 -> 2
3 -> 1
So Total ways: 7
- 复杂度分析:
- 时间复杂度: O(n)。
只需要遍历一次数组。所以时间复杂度是 O(n)。 - 空间复杂度: O(n)。
要将值存储在 DP 中,需要额外的空间。
- 时间复杂度: O(n)。
方法 3:矩阵求幂(O(logn) 方法)
矩阵求幂是一种以更好的时间复杂度解决 DP 问题的数学方法。矩阵求幂技术具有大小为 KXK 的变换矩阵和函数向量 (KX 1)。通过取变换矩阵的 n-1 次方并将其与函数向量相乘得到结果向量,表示大小为 KX 1 的 Res。Res 的第一个元素将是答案对于给定的 n 值。这种方法将采用 O(K^3logn) 时间复杂度,即寻找变换矩阵的 (n-1) 次幂的复杂度。
关键术语:
K = F(n) 依赖的项数,从递归关系我们可以说 F(n) 依赖于 F(n-1) 和 F(n-2)。 => K =3
F1 = 包含前 K 项的 F(n) 值的向量(一维数组)。由于 K=3 =>F1 将具有前 2 项的 F(n) 值。 F1=[1,2,4]
T = 变换矩阵,它是一个大小为 KXK 的二维矩阵,由对角线后的所有 1 组成,除最后一行外全为零。最后一行将具有 F(n) 依逆序依赖的所有 K 项的系数。 => T =[ [0 1 0] ,[0 0 1], [1 1 1] ]。
算法:
1)Take Input N
2)If N < K then Return Precalculated Answer //Base Condition
3)construct F1 Vector and T (Transformation Matrix)
4)Take N-1th power of T by using Optimal Power(T,N) Methods and assign it in T
5)return (TXF)[1]
对于最佳功率(T,N)方法,请参阅以下文章:https://www.geeksforgeeks.org/write-ac-program-to-calculate-powxn/
C++
#include
#define k 3
using namespace std;
// Multiply Two Matrix Function
vector> multiply(vector> A, vector> B) {
// third matrix to store multiplication of Two matrix9*
vector> C(k + 1, vector(k + 1));
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= k; j++) {
for (int x = 1; x <= k; x++) {
C[i][j] = (C[i][j] + (A[i][x] * B[x][j]));
}
}
}
return C;
}
// Optimal Way For finding pow(t,n)
// If n Is Odd then It Will be t*pow(t,n-1)
// else return pow(t,n/2)*pow(t,n/2)
vector> pow(vector> t, int n) {
// base Case
if (n == 1) {
return t;
}
// Recurrence Case
if (n & 1) {
return multiply(t, pow(t, n - 1));
} else {
vector> X = pow(t, n / 2);
return multiply(X, X);
}
}
int compute(int n) {
// base Case
if (n == 0) return 1;
if (n == 1) return 1;
if (n == 2) return 2;
// Function Vector(indexing 1 )
// that is [1,2]
int f1[k + 1] = {};
f1[1] = 1;
f1[2] = 2;
f1[3] = 4;
// Constucting Transformation Matrix that will be
/*[[0,1,0],[0,0,1],[3,2,1]]
*/
vector> t(k + 1, vector(k + 1));
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= k; j++) {
if (i < k) {
// Store 1 in cell that is next to diagonal of Matrix else Store 0 in
// cell
if (j == i + 1) {
t[i][j] = 1;
} else {
t[i][j] = 0;
}
continue;
}
// Last Row - store the Coefficients in reverse order
t[i][j] = 1;
}
}
// Computing T^(n-1) and Setting Tranformation matrix T to T^(n-1)
t = pow(t, n - 1);
int sum = 0;
// Computing first cell (row=1,col=1) For Resultant Matrix TXF
for (int i = 1; i <= k; i++) {
sum += t[1][i] * f1[i];
}
return sum;
}
int main() {
int n = 4;
cout << compute(n) << endl;
n = 5;
cout << compute(n) << endl;
n = 10;
cout << compute(n) << endl;
return 0;
}
Java
import java.io.*;
import java.util.*;
class GFG{
static int k = 3;
// Multiply Two Matrix Function
static int[][] multiply(int[][] A, int[][] B)
{
// Third matrix to store multiplication
// of Two matrix9*
int[][] C = new int[k + 1][k + 1];
for(int i = 1; i <= k; i++)
{
for(int j = 1; j <= k; j++)
{
for(int x = 1; x <= k; x++)
{
C[i][j] = (C[i][j] + (A[i][x] * B[x][j]));
}
}
}
return C;
}
// Optimal Way For finding pow(t,n)
// If n Is Odd then It Will be t*pow(t,n-1)
// else return pow(t,n/2)*pow(t,n/2)
static int[][] pow(int[][] t, int n)
{
// Base Case
if (n == 1)
{
return t;
}
// Recurrence Case
if ((n & 1) != 0)
{
return multiply(t, pow(t, n - 1));
}
else
{
int[][] X = pow(t, n / 2);
return multiply(X, X);
}
}
static int compute(int n)
{
// Base Case
if (n == 0) return 1;
if (n == 1) return 1;
if (n == 2) return 2;
// Function int(indexing 1 )
// that is [1,2]
int f1[]=new int[k + 1];
f1[1] = 1;
f1[2] = 2;
f1[3] = 4;
// Constucting Transformation Matrix that will be
/*[[0,1,0],[0,0,1],[3,2,1]]
*/
int[][] t = new int[k + 1][k + 1];
for(int i = 1; i <= k; i++)
{
for(int j = 1; j <= k; j++)
{
if (i < k)
{
// Store 1 in cell that is next to
// diagonal of Matrix else Store 0 in
// cell
if (j == i + 1)
{
t[i][j] = 1;
}
else
{
t[i][j] = 0;
}
continue;
}
// Last Row - store the Coefficients
// in reverse order
t[i][j] = 1;
}
}
// Computing T^(n-1) and Setting
// Tranformation matrix T to T^(n-1)
t = pow(t, n - 1);
int sum = 0;
// Computing first cell (row=1,col=1)
// For Resultant Matrix TXF
for(int i = 1; i <= k; i++)
{
sum += t[1][i] * f1[i];
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
// Input
int n = 4;
System.out.println(compute(n));
n = 5;
System.out.println(compute(n));
n = 10;
System.out.println(compute(n));
}
}
// This code is contributed by Shubhamsingh10
蟒蛇3
k = 3
# Multiply Two Matrix Function
def multiply(A, B):
# third matrix to store multiplication of Two matrix9*
C = [[0 for x in range(k+1)] for y in range(k+1)]
for i in range(1, k+1):
for j in range(1, k+1):
for x in range(1, k+1):
C[i][j] = (C[i][j] + (A[i][x] * B[x][j]))
return C
# Optimal Way For finding pow(t,n)
# If n Is Odd then It Will be t*pow(t,n-1)
# else return pow(t,n/2)*pow(t,n/2)
def pow(t, n):
# base Case
if (n == 1):
return t
# Recurrence Case
if (n & 1):
return multiply(t, pow(t, n - 1))
else:
X = pow(t, n // 2)
return multiply(X, X)
def compute( n):
# base Case
if (n == 0):
return 1
if (n == 1):
return 1
if (n == 2):
return 2
# Function Vector(indexing 1 )
# that is [1,2]
f1 = [0]*(k + 1)
f1[1] = 1
f1[2] = 2
f1[3] = 4
# Constucting Transformation Matrix that will be
#[[0,1,0],[0,0,1],[3,2,1]]
t = [[0 for x in range(k+1)] for y in range(k+1)]
for i in range(1, k+1):
for j in range(1, k+1):
if (i < k):
# Store 1 in cell that is next to diagonal of Matrix else Store 0 in
# cell
if (j == i + 1):
t[i][j] = 1
else:
t[i][j] = 0
continue
# Last Row - store the Coefficients in reverse order
t[i][j] = 1
# Computing T^(n-1) and Setting Tranformation matrix T to T^(n-1)
t = pow(t, n - 1)
sum = 0
# Computing first cell (row=1,col=1) For Resultant Matrix TXF
for i in range(1, k+1):
sum += t[1][i] * f1[i]
return sum
# Driver Code
n = 4
print(compute(n))
n = 5
print(compute(n))
n = 10
print(compute(n))
# This code is contributed by Shubhamsingh10
C#
// C# program for the above approach
using System;
class GFG{
static int k = 3;
// Multiply Two Matrix Function
static int[,] multiply(int[,] A, int[,] B)
{
// Third matrix to store multiplication
// of Two matrix9*
int[,] C = new int[k + 1,k + 1];
for(int i = 1; i <= k; i++)
{
for(int j = 1; j <= k; j++)
{
for(int x = 1; x <= k; x++)
{
C[i,j] = (C[i,j] + (A[i,x] * B[x,j]));
}
}
}
return C;
}
// Optimal Way For finding pow(t,n)
// If n Is Odd then It Will be t*pow(t,n-1)
// else return pow(t,n/2)*pow(t,n/2)
static int[,] pow(int[,] t, int n)
{
// Base Case
if (n == 1)
{
return t;
}
// Recurrence Case
if ((n & 1) != 0)
{
return multiply(t, pow(t, n - 1));
}
else
{
int[,] X = pow(t, n / 2);
return multiply(X, X);
}
}
static int compute(int n)
{
// Base Case
if (n == 0) return 1;
if (n == 1) return 1;
if (n == 2) return 2;
// Function int(indexing 1 )
// that is [1,2]
int[] f1=new int[k + 1];
f1[1] = 1;
f1[2] = 2;
f1[3] = 4;
// Constucting Transformation Matrix that will be
/*[[0,1,0],[0,0,1],[3,2,1]]
*/
int[,] t = new int[k + 1,k + 1];
for(int i = 1; i <= k; i++)
{
for(int j = 1; j <= k; j++)
{
if (i < k)
{
// Store 1 in cell that is next to
// diagonal of Matrix else Store 0 in
// cell
if (j == i + 1)
{
t[i,j] = 1;
}
else
{
t[i,j] = 0;
}
continue;
}
// Last Row - store the Coefficients
// in reverse order
t[i,j] = 1;
}
}
// Computing T^(n-1) and Setting
// Tranformation matrix T to T^(n-1)
t = pow(t, n - 1);
int sum = 0;
// Computing first cell (row=1,col=1)
// For Resultant Matrix TXF
for(int i = 1; i <= k; i++)
{
sum += t[1,i] * f1[i];
}
return sum;
}
// Driver Code
static public void Main (){
// Input
int n = 4;
Console.WriteLine(compute(n));
n = 5;
Console.WriteLine(compute(n));
n = 10;
Console.WriteLine(compute(n));
}
}
// This code is contributed by Shubhamsingh10
Javascript
7
13
274
Explanation:
We Know For This Question
Transformation Matrix M= [[0,1,0],[0,0,1],[1,1,1]]
Functional Vector F1 = [1,2,4]
for n=2 :
ans = (M X F1)[1]
ans = [2,4,7][1]
ans = 2 //[2,4,7][1] = First cell value of [2,4,7] i.e 2
for n=3 :
ans = (M X M X F1)[1] //M^(3-1) X F1 = M X M X F1
ans = (M X [2,4,7])[1]
ans = [4,7,13][1]
ans = 4
for n = 4 :
ans = (M^(4-1) X F1)[1]
ans = (M X M X M X F1) [1]
ans = (M X [4,7,13])[1]
ans = [7,13,24][1]
ans = 7
for n = 5 :
ans = (M^4 X F1)[1]
ans = (M X [7,13,24])[1]
ans = [13,24,44][1]
ans = 13
时间复杂度:
O(K^3log(n)) //For Computing pow(t,n-1)
For this question K is 3
So Overall Time Complexity is O(27log(n))=O(logn)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。