Help with error

code : https://ide.codingblocks.com/s/454778

question: fibo using matrix exponentiation

not getting an output

you are mixing 0 based indexing and 1 based indexing

you declare vector which is 0 based index
but why multiplying you use 1 based indexing


fixed it in line 62, is it an issue somewhere else too? I guess it is because of vector T so I’m thinking inserting 0’s in the first row and not using that row

but only inserting 1 row will not solve the problem
as columns are also messed up

best is to use 0 based indexing always in the whole code


getting wrong values

i think you shared a wrong code

this is the correct one, indexing is not correct in the multiplication function but fixing it still gives the wrong answer

image
this is correct one

Modified code

one issuse with this code is that the indexing of the term is 1 based for example for input 5 it gives answer as 3 and not 5, one solution is that I can manually increment the input by 1, but I saw someone else’s code https://ide.codingblocks.com/s/455099 and it looks the same as mine but gives the desired output(0, based indexing)

it should be 1 based
as it is for user
user will consider first term as 1 not as 0

but the test cases won’t pass that way

then just pass n-1 from main()
to pass testcases

can you please explain why this is giving answer with 0 based indexing

image
make these changes

  1. change the T matrix
  2. after multiplying T and F we get matrix of 2X1
    whose second element will be the ans so to calculate that
    use A[1][i]

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.