Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Basics

Fibonacci for a Huge N

Hard · Basics · Asked at Google, Amazon, Codenation, Directi

With F(0) = 0, F(1) = 1 and F(n) = F(n − 1) + F(n − 2), print F(n) modulo 10^9 + 7. n can be as large as 10^18, so a loop is far too slow.

Input

A single integer n.

Output

F(n) modulo 10^9 + 7.

Constraints

Matrix exponentiation: [[1, 1], [1, 0]]^n = [[F(n + 1), F(n)], [F(n), F(n − 1)]]. Raise the matrix to the n-th power by repeated squaring — O(log n) 2 × 2 multiplications, each taken modulo 10^9 + 7. (The fast-doubling identities F(2k) = F(k)(2F(k + 1) − F(k)) and F(2k + 1) = F(k)² + F(k + 1)² work too.)

Example 1

Input

10

Output

55

Example 2

Input

0

Output

0

Hints

  1. A loop of 10^18 steps is impossible.
  2. One Fibonacci step is a 2 × 2 matrix multiplication: [F(n+1), F(n)] = [[1,1],[1,0]] · [F(n), F(n−1)].
  3. Raise the matrix to the n-th power by repeated squaring.

Approach

Compute Q^n for Q = [[1, 1], [1, 0]] with binary exponentiation: square the base each step and multiply it into the result when the current bit of n is 1, reducing every entry modulo 10^9 + 7. Then F(n) is the top-right entry of Q^n. Every product of two entries is below 10^18, so 64-bit integers do not overflow. That is about 60 squarings for n = 10^18.

Complexity: O(log n) time · O(1) space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

typedef array<array<long long, 2>, 2> Mat;
const long long MOD = 1000000007LL;

Mat mul(const Mat &a, const Mat &b) {
    Mat c = {};
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            for (int k = 0; k < 2; k++) c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % MOD;
    return c;
}

int main() {
    long long n;
    cin >> n;
    Mat res = {{{1, 0}, {0, 1}}}, base = {{{1, 1}, {1, 0}}};
    while (n > 0) {
        if (n & 1) res = mul(res, base);
        base = mul(base, base);
        n >>= 1;
    }
    cout << res[0][1] << "\n";
    return 0;
}

Python solution

MOD = 10**9 + 7


def mul(a, b):
    return [
        [(a[0][0] * b[0][0] + a[0][1] * b[1][0]) % MOD, (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % MOD],
        [(a[1][0] * b[0][0] + a[1][1] * b[1][0]) % MOD, (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % MOD],
    ]


n = int(input())
res = [[1, 0], [0, 1]]
base = [[1, 1], [1, 0]]
while n > 0:
    if n & 1:
        res = mul(res, base)
    base = mul(base, base)
    n >>= 1
print(res[0][1])

Java solution

import java.util.*;

public class Main {
    static final long MOD = 1000000007L;

    static long[][] mul(long[][] a, long[][] b) {
        long[][] c = new long[2][2];
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++)
                for (int k = 0; k < 2; k++) c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % MOD;
        return c;
    }

    public static void main(String[] args) {
        long n = new Scanner(System.in).nextLong();
        long[][] res = {{1, 0}, {0, 1}}, base = {{1, 1}, {1, 0}};
        while (n > 0) {
            if ((n & 1) == 1) res = mul(res, base);
            base = mul(base, base);
            n >>= 1;
        }
        System.out.println(res[0][1]);
    }
}

Solve Fibonacci for a Huge N online · All practice problems