Segmentation fault

small modification: https://ide.codingblocks.com/s/261390

@Ishitagambhir
Your logic is correct but the problem is during multiplication of T and B arrays in line 56.
Inorder to multiply 2 matrix of order nm and pq, m should be equals to p. But in your case your T matrix is kk whereas your B is 1k. Please check this. That’s why it is giving segmentation fault.

@shrm.abhishek.kmr https://ide.codingblocks.com/s/261806
I changed the dimensions of B, and T is also correct according me but I am getting final answer wrong, please see.

@Ishitagambhir
Can you please share me link of this question at hackerblocks ?

@shrm.abhishek.kmr https://hack.codingblocks.com/app/contests/1463/164/problem

@Ishitagambhir
There are few mistake in your code. I am suggesting some changes

  1. while multiplying matrix, apply mod 1000000000 there itself. So change line 11 to sum += m1[i][j]*m2[j][k]; sum%=1000000000;
  2. Line 28 should be if (pow & 1) not (order & 1)
  3. condition in line 34 should be if (n < k)
  4. line 53 should be T = fastPowerMatrix(T, n, k);
  5. line 68 return a[0][0] % 1000000000;

@Ishitagambhir
I have updated your code and it is working fine. Check it here https://ide.codingblocks.com/s/262000

@shrm.abhishek.kmr i did not understand why are line 4, and 5 needed.
It should be T^n-1 x F1 = Fn, and last element of Fn matrix contains the answer. Then why is it not producing the correct output?

@Ishitagambhir

  1. For line 4, you have already decrement n to n-1 while calling findAns() function in line no 85
  2. for line 5, go through video again, F[0][0] stores the final ans.
1 Like