-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonaci_num_matrix.cpp
More file actions
53 lines (50 loc) · 957 Bytes
/
fibonaci_num_matrix.cpp
File metadata and controls
53 lines (50 loc) · 957 Bytes
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
#include <iostream>
#define ll long long
#define mod 1000000007
using namespace std;
class Mtx{
public:
long long a,b,c,d;
Mtx operator*(const Mtx &q){
Mtx p;
p.a=((this->a)*(q.a) + (this->b)*(q.c))%mod;
p.c=((this->c)*(q.a) + (this->d)*(q.c))%mod;
p.b=((this->a)*(q.b) + (this->b)*(q.d))%mod;
p.d=((this->c)*(q.b) + (this->d)*(q.d))%mod;
return p;
}
};
Mtx Mul(Mtx d,ll n){
if(n==1)
return d;
if(n%2==0){
Mtx s;
s=Mul(d,n/2);
return s*s;
}
Mtx s;
s=Mul(d,n/2);
return s*s*d;
}
ll fib(ll x,ll y,ll n){
Mtx ss,uu;
ss.a=1;
ss.b=1;
ss.c=1;
ss.d=0;
ss=Mul(ss,n);
uu.a=y;
uu.b=0;
uu.c=x;
uu.d=0;
Mtx u=(ss*uu);
return u.c;
}
int main(){
ll t,x,y,n;
cin>>t;
while(t--){
cin>>x>>y>>n;
cout<<fib(x,y,n)<<endl;
}
}