Skip to content

Instantly share code, notes, and snippets.

@hjroh0315
Last active May 6, 2022 09:37
Show Gist options
  • Save hjroh0315/756f8c6b4f15cc9693df8c229f5875a9 to your computer and use it in GitHub Desktop.
Save hjroh0315/756f8c6b4f15cc9693df8c229f5875a9 to your computer and use it in GitHub Desktop.
Fibonacci in TMP, log N time complexity, without using a matrix. | 피보나치, 로그 시복도, 행렬 없이.
#include<iostream>
#include<type_traits>
using namespace std;
using ll=long long;
template<ll val>
using llconst=integral_constant<ll,val>;
constexpr ll mod=1000000007;
template<ll n, int odd>
struct fibo{};
template<ll n>
struct fibo<n,0>:llconst<
((fibo<n/2+1,(n/2+1)&1>::value)
*(fibo<n/2+1,(n/2+1)&1>::value)
%mod
-(fibo<n/2-1,(n/2-1)&1>::value)
*(fibo<n/2-1,(n/2-1)&1>::value)
%mod
+mod)%mod>{};
template<ll n>
struct fibo<n,1>:llconst<
((fibo<n/2+1,(n/2+1)&1>::value)
*(fibo<n/2+1,(n/2+1)&1>::value)
%mod
+(fibo<n/2,(n/2)&1>::value)
*(fibo<n/2,(n/2)&1>::value)
%mod
)%mod>{};
template<>
struct fibo<0,0>:llconst<0>{};
template<>
struct fibo<2,0>:llconst<1>{};
template<>
struct fibo<1,1>:llconst<1>{};
template<ll n>
using fibonacci_t=fibo<n,n&1>;
int main()
{
ll val=fibonacci_t<1000000000000000000>::value;
cout<<val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment