Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const unsigned long long mod_number = 1e9+7;
- // Helper function that multiplies 2
- // matrices F and M of size 2*2, and
- // puts the multiplication result
- // back to F[][]
- void multiply(unsigned long long F[2][2], unsigned long long M[2][2]);
- // Helper function that calculates F[][]
- // raise to the power n and puts the
- // result in F[][]
- // Note that this function is designed
- // only for fib() and won't work as
- // general power function
- void power(unsigned long long F[2][2], unsigned long long n);
- unsigned long long fib(unsigned long long n)
- {
- unsigned long long F[2][2] = { { 1, 1 }, { 1, 0 } };
- if (n == 0)
- return 0;
- power(F, n - 1);
- return F[0][0] % mod_number;
- }
- void multiply(unsigned long long F[2][2], unsigned long long M[2][2])
- {
- unsigned long long x = F[0][0] * M[0][0]) +
- F[0][1] * M[1][0];
- unsigned long long y = F[0][0] * M[0][1] +
- F[0][1] * M[1][1];
- unsigned long long z = F[1][0] * M[0][0] +
- F[1][1] * M[1][0];
- unsigned long long w = F[1][0] * M[0][1] +
- F[1][1] * M[1][1];
- F[0][0] = x % mod_number;
- F[0][1] = y % mod_number;
- F[1][0] = z % mod_number;
- F[1][1] = w % mod_number;
- }
- void power(unsigned long long F[2][2], unsigned long long n)
- {
- unsigned long long i;
- unsigned long long M[2][2] = { { 1, 1 }, { 1, 0 } };
- // n - 1 times multiply the
- // matrix to {{1,0},{0,1}}
- for(i = 2; i <= n; i++)
- multiply(F, M);
- }
- // Driver code
- int main()
- {
- unsigned long long n = 75218624135794268;
- // cin >> n;
- cout << fib(n) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement