Example #1
1
        public static void Main(string[] args)
        {
            var n = long.Parse(Console.ReadLine());

            var aMatrix = new FibMatrix(); // the initial fibonachi matrix 1, 1, 1, 0

            Console.WriteLine(PowModeFibMatrix(aMatrix, n).Matrix[0, 1]);
        }
Example #2
0
        public FibMatrix(FibMatrix A, FibMatrix B)
        {
            this.Matrix = new long[2, 2];

            this.Matrix[0, 0] = A.Matrix[0, 0] * B.Matrix[0, 0] + A.Matrix[0, 1] * B.Matrix[1, 0];
            this.Matrix[0, 1] = A.Matrix[0, 0] * B.Matrix[0, 1] + A.Matrix[0, 1] * B.Matrix[1, 1];
            this.Matrix[1, 0] = A.Matrix[1, 0] * B.Matrix[0, 0] + A.Matrix[1, 1] * B.Matrix[1, 0];
            this.Matrix[1, 1] = A.Matrix[1, 0] * B.Matrix[0, 1] + A.Matrix[1, 1] * B.Matrix[1, 1];

            this.Matrix[0, 0] %= MODULE;
            this.Matrix[0, 1] %= MODULE;
            this.Matrix[1, 0] %= MODULE;
            this.Matrix[1, 1] %= MODULE;
        }
Example #3
0
        public static FibMatrix PowModeFibMatrix(FibMatrix a, long p)
        {
            if (p == 1)
            {
                return a;
            }

            if (p % 2 == 1)
            {
                return new FibMatrix(PowModeFibMatrix(a, p - 1), a);
            }

            a = PowModeFibMatrix(a, p / 2);
            return new FibMatrix(a, a);
        }