Initialize result matrix and unity matrix
for ( let i = 1 ; i <= K ; i ++ ) { for ( let 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 = parseInt ( n / 2 , 10 ) ; } }