📜  由两个给定数组生成的递归关系的第n个项

📅  最后修改于: 2021-05-13 22:55:09             🧑  作者: Mango

给定一个整数N和两个阵列F []和大小为K分别代表的下面递推关系的前K而言的前K术语和系数C []。

任务是找到递归关系的第N个项。由于数量可能非常大,因此取10 9 + 7为模。
例子:

天真的方法:这个想法是通过给定的递归关系,通过在先前的K个术语的帮助下计算每个术语来生成序列。形成序列后,打印第N术语。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
int mod = 1e9 + 7;
 
// Function to calculate Nth term of
// general recurrence relations
void NthTerm(int F[], int C[], int K,
             int n)
{
    // Stores the generated sequence
    int ans[n + 1] = { 0 };
 
    for (int i = 0; i < K; i++)
        ans[i] = F[i];
 
    for (int i = K; i <= n; i++) {
 
        for (int j = i - K; j < i; j++) {
 
            // Current term is sum of
            // previous k terms
            ans[i] += ans[j];
            ans[i] %= mod;
        }
    }
 
    // Print the nth term
    cout << ans[n] << endl;
}
 
// Driver Code
int main()
{
    // Given Array F[] and C[]
    int F[] = { 0, 1 };
    int C[] = { 1, 1 };
 
    // Given N and K
    int K = 2;
    int N = 10;
 
    // Function Call
    NthTerm(F, C, K, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
     
static double mod = 1e9 + 7;
 
// Function to calculate Nth term of
// general recurrence relations
static void NthTerm(int F[], int C[], int K,
                    int n)
{
     
    // Stores the generated sequence
    int ans[] = new int[n + 1 ];
 
    for(int i = 0; i < K; i++)
        ans[i] = F[i];
 
    for(int i = K; i <= n; i++)
    {
        for(int j = i - K; j < i; j++)
        {
             
            // Current term is sum of
            // previous k terms
            ans[i] += ans[j];
            ans[i] %= mod;
        }
    }
 
    // Print the nth term
    System.out.println(ans[n]);
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given Array F[] and C[]
    int F[] = { 0, 1 };
    int C[] = { 1, 1 };
 
    // Given N and K
    int K = 2;
    int N = 10;
 
    // Function call
    NthTerm(F, C, K, N);
}
}
 
// This code is contributed by jana_sayantan


Python3
# Python3 program for the above approach
mod = 1e9 + 7
 
# Function to calculate Nth term of
# general recurrence relations
def NthTerm(F, C, K, n):
 
    # Stores the generated sequence
    ans = [0] * (n + 1)
     
    i = 0
    while i < K:
        ans[i] = F[i]
        i += 1
 
    i = K
    while i <= n:
        j = i - K
        while j < i:
             
            # Current term is sum of
            # previous k terms
            ans[i] += ans[j]
            ans[i] %= mod
            j += 1
             
        i += 1
 
    # Print the nth term
    print(int(ans[n]))
     
# Driver code
if __name__ == '__main__':
 
    # Given Array F[] and C[]
    F = [ 0, 1 ]
    C = [ 1, 1 ]
 
    # Given N and K
    K = 2
    N = 10
 
    # Function call
    NthTerm(F, C, K, N)
 
# This code is contributed by jana_sayantan


C#
// C# program for
// the above approach
using System;
class GFG{
     
static double mod = 1e9 + 7;
 
// Function to calculate Nth term of
// general recurrence relations
static void NthTerm(int [] F, int [] C,
                    int K, int n)
{
  // Stores the generated sequence
  int []ans = new int[n + 1];
 
  for(int i = 0; i < K; i++)
    ans[i] = F[i];
 
  for(int i = K; i <= n; i++)
  {
    for(int j = i - K; j < i; j++)
    {
      // Current term is sum of
      // previous k terms
      ans[i] += ans[j];
      ans[i] %= (int)mod;
    }
  }
 
  // Print the nth term
  Console.WriteLine(ans[n]);
}
 
// Driver Code
public static void Main (String[] args)
{
  // Given Array F[] and C[]
  int [] F= {0, 1};
  int [] C= {1, 1};
 
  // Given N and K
  int K = 2;
  int N = 10;
 
  // Function call
  NthTerm(F, C, K, N);
}
}
 
// This code is contributed by jana_sayantan


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
int mod = 1e9 + 7;
 
// Declare T[][] as global matrix
int T[2000][2000];
 
// Result matrix
int result[2000][2000];
 
// Function to multiply two matrices
void mul_2(int K)
{
    // Create an auxiliary matrix to
    // store elements of the
    // multiplication matrix
    int temp[K + 1][K + 1];
    memset(temp, 0, sizeof temp);
 
    // Iterate over range [0, K]
    for (int i = 1; i <= K; i++) {
 
        for (int j = 1; j <= K; j++) {
 
            for (int k = 1; k <= K; k++) {
 
                // Update temp[i][j]
                temp[i][j]
                    = (temp[i][j]
                       + (T[i][k] * T[k][j])
                             % mod)
                      % mod;
            }
        }
    }
 
    // Update the final matrix
    for (int i = 1; i <= K; i++) {
        for (int j = 1; j <= K; j++) {
            T[i][j] = temp[i][j];
        }
    }
}
 
// Function to multiply two matrices
void mul_1(int K)
{
    // Create an auxiliary matrix to
    // store elements of the
    // multiplication matrix
    int temp[K + 1][K + 1];
    memset(temp, 0, sizeof temp);
 
    // Iterate over range [0, K]
    for (int i = 1; i <= K; i++) {
 
        for (int j = 1; j <= K; j++) {
 
            for (int k = 1; k <= K; k++) {
 
                // Update temp[i][j]
                temp[i][j]
                    = (temp[i][j]
                       + (result[i][k] * T[k][j])
                             % mod)
                      % mod;
            }
        }
    }
 
    // Update the final matrix
    for (int i = 1; i <= K; i++) {
        for (int j = 1; j <= K; j++) {
            result[i][j] = temp[i][j];
        }
    }
}
 
// Function to calculate matrix^n
// using binary exponentaion
void matrix_pow(int K, int n)
{
    // Initialize result matrix
    // and unity matrix
    for (int i = 1; i <= K; i++) {
        for (int j = 1; j <= K; j++) {
            if (i == j)
                result[i][j] = 1;
        }
    }
 
    while (n > 0) {
        if (n % 2 == 1)
            mul_1(K);
        mul_2(K);
        n /= 2;
    }
}
 
// Function to calculate nth term
// of general recurrence
int NthTerm(int F[], int C[], int K,
            int n)
{
 
    // Fill T[][] with appropriate value
    for (int i = 1; i <= K; i++)
        T[i][K] = C[K - i];
 
    for (int i = 1; i <= K; i++)
        T[i + 1][i] = 1;
 
    // Function Call to calculate T^n
    matrix_pow(K, n);
 
    int answer = 0;
 
    // Calculate nth term as first
    // element of F*(T^n)
    for (int i = 1; i <= K; i++) {
        answer += F[i - 1] * result[i][1];
    }
 
    // Print the result
    cout << answer << endl;
    return 0;
}
 
// Driver Code
int main()
{
    // Given Initial terms
    int F[] = { 1, 2, 3 };
 
    // Given coefficients
    int C[] = { 1, 1, 1 };
 
    // Given K
    int K = 3;
 
    // Given N
    int N = 10;
 
    // Function Call
    NthTerm(F, C, K, N);
 
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
static int mod = (int) (1e9 + 7);
 
// Declare T[][] as global matrix
static int [][]T = new int[2000][2000];
 
// Result matrix
static int [][]result = new int[2000][2000];
 
// Function to multiply two matrices
static void mul_2(int K)
{
  // Create an auxiliary matrix to
  // store elements of the
  // multiplication matrix
  int [][]temp = new int[K + 1][K + 1];
 
  // Iterate over range [0, K]
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      for (int k = 1; k <= K; k++)
      {
        // Update temp[i][j]
        temp[i][j] = (temp[i][j] +
                     (T[i][k] * T[k][j]) %
                      mod) % mod;
      }
    }
  }
 
  // Update the final matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      T[i][j] = temp[i][j];
    }
  }
}
 
// Function to multiply two matrices
static void mul_1(int K)
{
  // Create an auxiliary matrix to
  // store elements of the
  // multiplication matrix
  int [][]temp = new int[K + 1][K + 1];
 
  // Iterate over range [0, K]
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      for (int k = 1; k <= K; k++)
      {
        // Update temp[i][j]
        temp[i][j] = (temp[i][j] +
                     (result[i][k] * T[k][j]) %
                      mod) % mod;
      }
    }
  }
 
  // Update the final matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      result[i][j] = temp[i][j];
    }
  }
}
 
// Function to calculate matrix^n
// using binary exponentaion
static void matrix_pow(int K, int n)
{
  // Initialize result matrix
  // and unity matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      if (i == j)
        result[i][j] = 1;
    }
  }
 
  while (n > 0)
  {
    if (n % 2 == 1)
      mul_1(K);
    mul_2(K);
    n /= 2;
  }
}
 
// Function to calculate nth term
// of general recurrence
static int NthTerm(int F[], int C[],
                   int K, int n)
{
  // Fill T[][] with appropriate value
  for (int i = 1; i <= K; i++)
    T[i][K] = C[K - i];
 
  for (int i = 1; i <= K; i++)
    T[i + 1][i] = 1;
 
  // Function Call to calculate T^n
  matrix_pow(K, n);
 
  int answer = 0;
 
  // Calculate nth term as first
  // element of F * (T ^ n)
  for (int i = 1; i <= K; i++)
  {
    answer += F[i - 1] * result[i][1];
  }
 
  // Print the result
  System.out.print(answer + "\n");
  return 0;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given Initial terms
  int F[] = {1, 2, 3};
 
  // Given coefficients
  int C[] = {1, 1, 1};
 
  // Given K
  int K = 3;
 
  // Given N
  int N = 10;
 
  // Function Call
  NthTerm(F, C, K, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for
# the above approach
mod = 1e9 + 7
 
# Declare T[][] as global matrix
T = [[0 for x in range (2000)]
        for y in range (2000)]
 
# Result matrix
result = [[0 for x in range (2000)]
             for y in range (2000)]
 
# Function to multiply two matrices
def mul_2(K):
 
    # Create an auxiliary matrix to
    # store elements of the
    # multiplication matrix
    temp = [[0 for x in range (K + 1)]
               for y in range (K + 1)]
  
    # Iterate over range [0, K]
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            for k in range (1, K + 1):
 
                # Update temp[i][j]
                temp[i][j] = ((temp[i][j] +
                              (T[i][k] * T[k][j]) %
                               mod) % mod)
          
    # Update the final matrix
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            T[i][j] = temp[i][j]
 
# Function to multiply two matrices
def mul_1(K):
 
    # Create an auxiliary matrix to
    # store elements of the
    # multiplication matrix
    temp = [[0 for x in range (K + 1)]
               for y in range (K + 1)]
     
    # Iterate over range [0, K]
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            for k in range (1, K + 1):
 
                # Update temp[i][j]
                temp[i][j] = ((temp[i][j] +
                              (result[i][k] * T[k][j]) %
                               mod) % mod)
 
    # Update the final matrix
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            result[i][j] = temp[i][j]
 
# Function to calculate matrix^n
# using binary exponentaion
def matrix_pow(K, n):
 
    # Initialize result matrix
    # and unity matrix
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            if (i == j):
                result[i][j] = 1
        
    while (n > 0):
        if (n % 2 == 1):
            mul_1(K)
        mul_2(K)
        n //= 2
 
# Function to calculate nth term
# of general recurrence
def NthTerm(F, C, K, n):
 
    # Fill T[][] with appropriate value
    for i in range (1, K + 1):
        T[i][K] = C[K - i]
 
    for i in range (1, K + 1):
        T[i + 1][i] = 1
 
    # Function Call to calculate T^n
    matrix_pow(K, n)
 
    answer = 0
 
    # Calculate nth term as first
    # element of F*(T^n)
    for i in range (1, K + 1):
        answer += F[i - 1] * result[i][1]
    
    # Print the result
    print(int(answer))
 
# Driver Code
if __name__ == "__main__":
   
    # Given Initial terms
    F = [1, 2, 3]
 
    # Given coefficients
    C = [1, 1, 1]
 
    # Given K
    K = 3
 
    # Given N
    N = 10
 
    # Function Call
    NthTerm(F, C, K, N)
 
# This code is contributed by Chitranayal


C#
// C# program for
// the above approach
using System;
class GFG{
 
static int mod = (int) (1e9 + 7);
 
// Declare T[,] as global matrix
static int [,]T = new int[2000, 2000];
 
// Result matrix
static int [,]result = new int[2000, 2000];
 
// Function to multiply two matrices
static void mul_2(int K)
{
  // Create an auxiliary matrix to
  // store elements of the
  // multiplication matrix
  int [,]temp = new int[K + 1,
                        K + 1];
 
  // Iterate over range [0, K]
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      for (int k = 1; k <= K; k++)
      {
        // Update temp[i,j]
        temp[i, j] = (temp[i, j] +
                     (T[i, k] * T[k, j]) %
                      mod) % mod;
      }
    }
  }
 
  // Update the readonly matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      T[i, j] = temp[i, j];
    }
  }
}
 
// Function to multiply two matrices
static void mul_1(int K)
{
  // Create an auxiliary matrix to
  // store elements of the
  // multiplication matrix
  int [,]temp = new int[K + 1,
                        K + 1];
 
  // Iterate over range [0, K]
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      for (int k = 1; k <= K; k++)
      {
        // Update temp[i,j]
        temp[i,j] = (temp[i, j] +
                    (result[i, k] * T[k, j]) %
                     mod) % mod;
      }
    }
  }
 
  // Update the readonly matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      result[i, j] = temp[i, j];
    }
  }
}
 
// Function to calculate matrix^n
// using binary exponentaion
static void matrix_pow(int K, int n)
{
  // Initialize result matrix
  // and unity matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      if (i == j)
        result[i, j] = 1;
    }
  }
 
  while (n > 0)
  {
    if (n % 2 == 1)
      mul_1(K);
    mul_2(K);
    n /= 2;
  }
}
 
// Function to calculate nth term
// of general recurrence
static int NthTerm(int []F, int []C,
                   int K, int n)
{
  // Fill T[,] with appropriate value
  for (int i = 1; i <= K; i++)
    T[i, K] = C[K - i];
 
  for (int i = 1; i <= K; i++)
    T[i + 1, i] = 1;
 
  // Function Call to calculate T^n
  matrix_pow(K, n);
 
  int answer = 0;
 
  // Calculate nth term as first
  // element of F * (T ^ n)
  for (int i = 1; i <= K; i++)
  {
    answer += F[i - 1] * result[i, 1];
  }
 
  // Print the result
  Console.Write(answer + "\n");
  return 0;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given Initial terms
  int []F = {1, 2, 3};
 
  // Given coefficients
  int []C = {1, 1, 1};
 
  // Given K
  int K = 3;
 
  // Given N
  int N = 10;
 
  // Function Call
  NthTerm(F, C, K, N);
}
}
  
// This code is contributed by Rajput-Ji


输出:
55

时间复杂度: O(N 2 )
辅助空间: O(N)

高效方法:使用矩阵幂运算可以找到递归关系的第N项。步骤如下:

  • 让我们考虑初始状态为:
  • 将大小为K 2的矩阵定义为:
  • 使用二进制幂计算矩阵T [] []的N次方。
  • 现在,将F []T [] []的N次方相乘得到:
  • 所得矩阵F x T N的第一项是所需结果。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
int mod = 1e9 + 7;
 
// Declare T[][] as global matrix
int T[2000][2000];
 
// Result matrix
int result[2000][2000];
 
// Function to multiply two matrices
void mul_2(int K)
{
    // Create an auxiliary matrix to
    // store elements of the
    // multiplication matrix
    int temp[K + 1][K + 1];
    memset(temp, 0, sizeof temp);
 
    // Iterate over range [0, K]
    for (int i = 1; i <= K; i++) {
 
        for (int j = 1; j <= K; j++) {
 
            for (int k = 1; k <= K; k++) {
 
                // Update temp[i][j]
                temp[i][j]
                    = (temp[i][j]
                       + (T[i][k] * T[k][j])
                             % mod)
                      % mod;
            }
        }
    }
 
    // Update the final matrix
    for (int i = 1; i <= K; i++) {
        for (int j = 1; j <= K; j++) {
            T[i][j] = temp[i][j];
        }
    }
}
 
// Function to multiply two matrices
void mul_1(int K)
{
    // Create an auxiliary matrix to
    // store elements of the
    // multiplication matrix
    int temp[K + 1][K + 1];
    memset(temp, 0, sizeof temp);
 
    // Iterate over range [0, K]
    for (int i = 1; i <= K; i++) {
 
        for (int j = 1; j <= K; j++) {
 
            for (int k = 1; k <= K; k++) {
 
                // Update temp[i][j]
                temp[i][j]
                    = (temp[i][j]
                       + (result[i][k] * T[k][j])
                             % mod)
                      % mod;
            }
        }
    }
 
    // Update the final matrix
    for (int i = 1; i <= K; i++) {
        for (int j = 1; j <= K; j++) {
            result[i][j] = temp[i][j];
        }
    }
}
 
// Function to calculate matrix^n
// using binary exponentaion
void matrix_pow(int K, int n)
{
    // Initialize result matrix
    // and unity matrix
    for (int i = 1; i <= K; i++) {
        for (int j = 1; j <= K; j++) {
            if (i == j)
                result[i][j] = 1;
        }
    }
 
    while (n > 0) {
        if (n % 2 == 1)
            mul_1(K);
        mul_2(K);
        n /= 2;
    }
}
 
// Function to calculate nth term
// of general recurrence
int NthTerm(int F[], int C[], int K,
            int n)
{
 
    // Fill T[][] with appropriate value
    for (int i = 1; i <= K; i++)
        T[i][K] = C[K - i];
 
    for (int i = 1; i <= K; i++)
        T[i + 1][i] = 1;
 
    // Function Call to calculate T^n
    matrix_pow(K, n);
 
    int answer = 0;
 
    // Calculate nth term as first
    // element of F*(T^n)
    for (int i = 1; i <= K; i++) {
        answer += F[i - 1] * result[i][1];
    }
 
    // Print the result
    cout << answer << endl;
    return 0;
}
 
// Driver Code
int main()
{
    // Given Initial terms
    int F[] = { 1, 2, 3 };
 
    // Given coefficients
    int C[] = { 1, 1, 1 };
 
    // Given K
    int K = 3;
 
    // Given N
    int N = 10;
 
    // Function Call
    NthTerm(F, C, K, N);
 
    return 0;
}

Java

// Java program for
// the above approach
import java.util.*;
class GFG{
 
static int mod = (int) (1e9 + 7);
 
// Declare T[][] as global matrix
static int [][]T = new int[2000][2000];
 
// Result matrix
static int [][]result = new int[2000][2000];
 
// Function to multiply two matrices
static void mul_2(int K)
{
  // Create an auxiliary matrix to
  // store elements of the
  // multiplication matrix
  int [][]temp = new int[K + 1][K + 1];
 
  // Iterate over range [0, K]
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      for (int k = 1; k <= K; k++)
      {
        // Update temp[i][j]
        temp[i][j] = (temp[i][j] +
                     (T[i][k] * T[k][j]) %
                      mod) % mod;
      }
    }
  }
 
  // Update the final matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      T[i][j] = temp[i][j];
    }
  }
}
 
// Function to multiply two matrices
static void mul_1(int K)
{
  // Create an auxiliary matrix to
  // store elements of the
  // multiplication matrix
  int [][]temp = new int[K + 1][K + 1];
 
  // Iterate over range [0, K]
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      for (int k = 1; k <= K; k++)
      {
        // Update temp[i][j]
        temp[i][j] = (temp[i][j] +
                     (result[i][k] * T[k][j]) %
                      mod) % mod;
      }
    }
  }
 
  // Update the final matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      result[i][j] = temp[i][j];
    }
  }
}
 
// Function to calculate matrix^n
// using binary exponentaion
static void matrix_pow(int K, int n)
{
  // Initialize result matrix
  // and unity matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      if (i == j)
        result[i][j] = 1;
    }
  }
 
  while (n > 0)
  {
    if (n % 2 == 1)
      mul_1(K);
    mul_2(K);
    n /= 2;
  }
}
 
// Function to calculate nth term
// of general recurrence
static int NthTerm(int F[], int C[],
                   int K, int n)
{
  // Fill T[][] with appropriate value
  for (int i = 1; i <= K; i++)
    T[i][K] = C[K - i];
 
  for (int i = 1; i <= K; i++)
    T[i + 1][i] = 1;
 
  // Function Call to calculate T^n
  matrix_pow(K, n);
 
  int answer = 0;
 
  // Calculate nth term as first
  // element of F * (T ^ n)
  for (int i = 1; i <= K; i++)
  {
    answer += F[i - 1] * result[i][1];
  }
 
  // Print the result
  System.out.print(answer + "\n");
  return 0;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given Initial terms
  int F[] = {1, 2, 3};
 
  // Given coefficients
  int C[] = {1, 1, 1};
 
  // Given K
  int K = 3;
 
  // Given N
  int N = 10;
 
  // Function Call
  NthTerm(F, C, K, N);
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program for
# the above approach
mod = 1e9 + 7
 
# Declare T[][] as global matrix
T = [[0 for x in range (2000)]
        for y in range (2000)]
 
# Result matrix
result = [[0 for x in range (2000)]
             for y in range (2000)]
 
# Function to multiply two matrices
def mul_2(K):
 
    # Create an auxiliary matrix to
    # store elements of the
    # multiplication matrix
    temp = [[0 for x in range (K + 1)]
               for y in range (K + 1)]
  
    # Iterate over range [0, K]
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            for k in range (1, K + 1):
 
                # Update temp[i][j]
                temp[i][j] = ((temp[i][j] +
                              (T[i][k] * T[k][j]) %
                               mod) % mod)
          
    # Update the final matrix
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            T[i][j] = temp[i][j]
 
# Function to multiply two matrices
def mul_1(K):
 
    # Create an auxiliary matrix to
    # store elements of the
    # multiplication matrix
    temp = [[0 for x in range (K + 1)]
               for y in range (K + 1)]
     
    # Iterate over range [0, K]
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            for k in range (1, K + 1):
 
                # Update temp[i][j]
                temp[i][j] = ((temp[i][j] +
                              (result[i][k] * T[k][j]) %
                               mod) % mod)
 
    # Update the final matrix
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            result[i][j] = temp[i][j]
 
# Function to calculate matrix^n
# using binary exponentaion
def matrix_pow(K, n):
 
    # Initialize result matrix
    # and unity matrix
    for i in range (1, K + 1):
        for j in range (1, K + 1):
            if (i == j):
                result[i][j] = 1
        
    while (n > 0):
        if (n % 2 == 1):
            mul_1(K)
        mul_2(K)
        n //= 2
 
# Function to calculate nth term
# of general recurrence
def NthTerm(F, C, K, n):
 
    # Fill T[][] with appropriate value
    for i in range (1, K + 1):
        T[i][K] = C[K - i]
 
    for i in range (1, K + 1):
        T[i + 1][i] = 1
 
    # Function Call to calculate T^n
    matrix_pow(K, n)
 
    answer = 0
 
    # Calculate nth term as first
    # element of F*(T^n)
    for i in range (1, K + 1):
        answer += F[i - 1] * result[i][1]
    
    # Print the result
    print(int(answer))
 
# Driver Code
if __name__ == "__main__":
   
    # Given Initial terms
    F = [1, 2, 3]
 
    # Given coefficients
    C = [1, 1, 1]
 
    # Given K
    K = 3
 
    # Given N
    N = 10
 
    # Function Call
    NthTerm(F, C, K, N)
 
# This code is contributed by Chitranayal

C#

// C# program for
// the above approach
using System;
class GFG{
 
static int mod = (int) (1e9 + 7);
 
// Declare T[,] as global matrix
static int [,]T = new int[2000, 2000];
 
// Result matrix
static int [,]result = new int[2000, 2000];
 
// Function to multiply two matrices
static void mul_2(int K)
{
  // Create an auxiliary matrix to
  // store elements of the
  // multiplication matrix
  int [,]temp = new int[K + 1,
                        K + 1];
 
  // Iterate over range [0, K]
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      for (int k = 1; k <= K; k++)
      {
        // Update temp[i,j]
        temp[i, j] = (temp[i, j] +
                     (T[i, k] * T[k, j]) %
                      mod) % mod;
      }
    }
  }
 
  // Update the readonly matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      T[i, j] = temp[i, j];
    }
  }
}
 
// Function to multiply two matrices
static void mul_1(int K)
{
  // Create an auxiliary matrix to
  // store elements of the
  // multiplication matrix
  int [,]temp = new int[K + 1,
                        K + 1];
 
  // Iterate over range [0, K]
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      for (int k = 1; k <= K; k++)
      {
        // Update temp[i,j]
        temp[i,j] = (temp[i, j] +
                    (result[i, k] * T[k, j]) %
                     mod) % mod;
      }
    }
  }
 
  // Update the readonly matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      result[i, j] = temp[i, j];
    }
  }
}
 
// Function to calculate matrix^n
// using binary exponentaion
static void matrix_pow(int K, int n)
{
  // Initialize result matrix
  // and unity matrix
  for (int i = 1; i <= K; i++)
  {
    for (int j = 1; j <= K; j++)
    {
      if (i == j)
        result[i, j] = 1;
    }
  }
 
  while (n > 0)
  {
    if (n % 2 == 1)
      mul_1(K);
    mul_2(K);
    n /= 2;
  }
}
 
// Function to calculate nth term
// of general recurrence
static int NthTerm(int []F, int []C,
                   int K, int n)
{
  // Fill T[,] with appropriate value
  for (int i = 1; i <= K; i++)
    T[i, K] = C[K - i];
 
  for (int i = 1; i <= K; i++)
    T[i + 1, i] = 1;
 
  // Function Call to calculate T^n
  matrix_pow(K, n);
 
  int answer = 0;
 
  // Calculate nth term as first
  // element of F * (T ^ n)
  for (int i = 1; i <= K; i++)
  {
    answer += F[i - 1] * result[i, 1];
  }
 
  // Print the result
  Console.Write(answer + "\n");
  return 0;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given Initial terms
  int []F = {1, 2, 3};
 
  // Given coefficients
  int []C = {1, 1, 1};
 
  // Given K
  int K = 3;
 
  // Given N
  int N = 10;
 
  // Function Call
  NthTerm(F, C, K, N);
}
}
  
// This code is contributed by Rajput-Ji
输出:
423

时间复杂度: O(K 3 log(N))
辅助空间: O(K * K)