Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:31:39+00:00 2026-06-06T02:31:39+00:00

Can anyone please explain or suggest some good tutorial for the method of matrix

  • 0

Can anyone please explain or suggest some good tutorial for the method of matrix exponentiation in order to optimize the solution of the problem :
great wall of byteland

The solution which I uploaded is based dynamic programming with this underlying equation :
[ f(n) = f(n-1) + 4*f(n-2) + 2*f(n-3) ]
but the solution is giving me Time Limit Exceeded Error.

This is the code I built :

    #include<iostream>
    #define num 1000000007
    using namespace std;
    int main(){
        int t;
        cin>>t;
        while(t--){
            int n;
            cin>>n;
            if(n<=3){
                switch(n){
                    case 1:
                        cout<<1<<endl;
                        break;
                    case 2:
                        cout<<5<<endl;
                        break;
                    case 3:
                        cout<<11<<endl;
                        break;
                }
            }
            else{
                int a=1 , b=5 , c=11 ;
                int next;
                for(int i=4;i<=n;i++){
                    next = (c + 4*b + 2*a)%num ;
                    a = b;
                    b = c;
                    c = next;
                }
                cout<<next<<endl;
            }
        }
        return 0;
    }

Please suggest the matrix exponentiation method for optimizing the run time of the solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:31:42+00:00Added an answer on June 6, 2026 at 2:31 am

    If you have a sequence defined by:

    u(0) to u(d-1) are given
    for n > d u(n)=a(1)*u(n-1)+…+a(d)*u(n-d)
    

    then let A be the companion matrix defined by:

    A(i,j) = a(d+1-j) if i = d
             1 if i+1 = j
             0 otherwise
    

    and let uinit = transpose(u(0) … u(d-1))

    You have A^n*uinit = transpose(u(n) … u(n+d-1)) (you can verify by yourself that A*transpose(u(n) … u(n+d-1)) = transpose(u(n+1) … u(n+d))).

    Then you can compute A^n in O(Log(n)) and use it to compute u(n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, can anyone please explain how I can subclass UIButton and override some
Can anyone please explain me the use of development certificates and how I can
Can anyone please explain me what delegateEvents in backbone.js does? The documentation did not
Can anyone please explain how to replace the values of one column with the
Can anyone please explain why the following code behaving strangely: public class UserInputTest {
Can anyone please explain to me why the following two queries yield different results?
Can anyone please explain me what is DataContractTranslstor and what's it's use?
Can anyone please explain me about carp subroutine with sample Perl code?
can anyone please explain the concept of dispatcher, is it one dispatcher per thread
Can anyone please explain to me what is the difference between IEnumerable & IEnumerator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.