diff --git a/Coding Exercise Solutions/Fibonacci Sum.cpp b/Coding Exercise Solutions/Fibonacci Sum.cpp new file mode 100644 index 0000000..22bb895 --- /dev/null +++ b/Coding Exercise Solutions/Fibonacci Sum.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; +#define mod 1000000007 + +void multiply(vector>& F, vector>&M) +{ + long long x,y,z,w; + x = (F[0][0]*M[0][0] + F[0][1]*M[1][0])%mod; + y = (F[0][0]*M[0][1] + F[0][1]*M[1][1])%mod; + z = (F[1][0]*M[0][0] + F[1][1]*M[1][0])%mod; + w = (F[1][0]*M[0][1] + F[1][1]*M[1][1])%mod; + + F[0][0] = x; + F[0][1] = y; + F[1][0] = z; + F[1][1] = w; +} + +void power(vector>&F, int n){ + if( n == 0 || n == 1) + return; + + vector>M = {{1,1},{1,0}}; + + power(F, n/2); + multiply(F, F); + + if (n%2 != 0){ + multiply(F, M); + } +} + +long long fib(long long n) +{ + vector> F= {{1,1},{1,0}}; + if (n == 0){ + return 0; + } + power(F, n-1); + return F[0][0]; +} + +int fibSum(int n,int m){ + int ans=(fib(m+2)-fib(n+1))%mod; + if(ans<0){ + ans+=mod; + } + return ans; +} +