-
Notifications
You must be signed in to change notification settings - Fork 16
/
fibonacci finding.cpp
78 lines (65 loc) · 1.5 KB
/
fibonacci finding.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
#define lli long long int
#define mod 1000000007
using namespace std;
//VJ's Code
vector<string> split_string(string);
vector < vector < lli > > mul(vector < vector < lli > >& A, vector < vector < lli > >& B)
{
vector< vector < lli > > C(A.size(), vector<lli>(A.size()));
for ( int i = 0 ; i < A.size() ; ++ i)
{
for ( int j = 0 ; j < A.size() ; ++ j)
{
C[i][j] = 0;
for ( int k = 0 ; k < A.size() ; ++ k)
{
lli x = ((A[i][k] % mod) * (B[k][j] % mod) % mod);
C[i][j] += x;
}
}
}
return C;
}
void power(vector< vector< lli > > &matrix, int n, int d)
{
vector < vector < lli > > res(d, vector<lli> (d));
for ( lli i = 0 ; i < d ; ++ i)
res[i][i] = 1;
while ( n )
{
if(!(n & 1))
{
matrix = mul(matrix,matrix);
n /= 2;
}
else
{
res = mul(res,matrix);
n --;
}
}
matrix = res;
}
// Complete the solve function below.
int solve(int a, int b, int n) {
if ( n == 0)
return a;
if( n == 1)
return b;
vector < vector < lli > > matrix ( 2 , vector < lli > (2));
matrix[0][0] = matrix[0][1] = matrix[1][0] = 1;
matrix[1][1] = 0;
power(matrix, n, 2);
return (a*matrix[1][1] + b*matrix[1][0] ) % mod;
}
int main()
{
int t,a,b,n;
cin>>t;
while(t --)
{
cin>>a>>b>>n;
cout<<solve(a,b,n)<<"\n";
}
}