-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL055.cpp
53 lines (53 loc) · 1.12 KB
/
SCTDL055.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
47
48
49
50
51
52
53
/**
* @file SCTDL055.cpp
* @author long ([email protected])
* @brief n-th fibonacci number
* @version 0.1
* @date 2023-06-10
*
* @copyright Copyright (c) 2023
*
*/
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7, F[2][2], M[2][2];
void Mul(long long f[2][2], long long m[2][2])
{
long long x = (f[0][0] * m[0][0] % mod + f[0][1] * m[1][0] % mod) % mod;
long long y = (f[0][0] * m[0][1] % mod + f[0][1] * m[1][1] % mod) % mod;
long long z = (f[1][0] * m[0][0] % mod + f[1][1] * m[1][0] % mod) % mod;
long long t = (f[1][0] * m[0][1] % mod + f[1][1] * m[1][1] % mod) % mod;
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = t;
}
void Pow(long long f[2][2], long long n)
{
if (n <= 1)
return;
Pow(f, n / 2);
Mul(f, f);
if (n & 1)
Mul(f, M);
}
long long fibo(long long n)
{
F[0][0] = F[0][1] = F[1][0] = 1;
F[1][1] = 0;
M[0][0] = M[0][1] = M[1][0] = 1;
M[1][1] = 0;
Pow(F, n - 1);
return F[0][0];
}
int main()
{
int t;
cin >> t;
while (t--)
{
long long n;
cin >> n;
cout << fibo(n) << endl;
}
}