type m22 = { a : int; b : int; c : int; d : int }

let mul x y =
  { a = x.a * y.a + x.b * y.c;
    b = x.a * y.b + x.b * y.d;
    c = x.c * y.a + x.d * y.c;
    d = x.c * y.b + x.d * y.d }

let rec mpow m n =
  if n = 0 then { a = 1; b = 0; c = 0; d = 1 }
  else if n mod 2 = 0 then
    let h = mpow m (n / 2) in mul h h
  else
    mul m (mpow m (n - 1))

;;

let fib_matrix = { a = 1; b = 1; c = 1; d = 0 } in
let r = mpow fib_matrix 30 in
r.b
