-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFastDoubling.cpp
46 lines (41 loc) · 895 Bytes
/
FastDoubling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007;
long long int a,b,c,d;
void fast_fib(long long int n,long long int ans[])
{
if(n == 0)
{
ans[0] = 0;
ans[1] = 1;
return;
}
fast_fib((n/2),ans);
a = ans[0]; /* F(n) */
b = ans[1]; /* F(n+1) */
c = 2*b - a;
if(c < 0)
c += MOD;
c = (a * c) % MOD; /* F(2n) */
d = (a*a + b*b) % MOD; /* F(2n + 1) */
if(n%2 == 0)
{
ans[0] = c;
ans[1] = d;
}
else
{
ans[0] = d;
ans[1] = c+d;
}
}
int main()
{
long long int n; /* nth value to be found */
scanf("%lld",&n);
long long int ans[2]={0};
fast_fib(n,ans);
printf("%lld\n", ans[0]);
return 0;
}
//This code has a complexity of O(log n) which is way too faster than previously discussed function.