forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFIBEASY.cpp
114 lines (81 loc) · 1.48 KB
/
FIBEASY.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
author: kevinmathew
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 10ll
unordered_map<ll,vector<vector<ll>>> dp;
vector<vector<ll>> multiply(vector<vector<ll>> &a,vector<vector<ll>> &b){
vector<vector<ll>> c(2,vector<ll>(2,0));
for(ll i=0;i<2;i++){
for(ll j=0;j<2;j++){
for(ll k=0;k<2;k++){
c[i][j]+=(a[i][k]*b[k][j])%MOD;
c[i][j]%=MOD;
}
}
}
return c;
}
vector<vector<ll>> power2(vector<vector<ll>> &a,ll b){
if(b==1){
return a;
}
if(dp.find(b)!=dp.end()){
return dp[b];
}
vector<vector<ll>> small_ans=power2(a,b/2);
small_ans=multiply(small_ans,small_ans);
if(b&1){
small_ans=multiply(small_ans,a);
}
dp[b]=small_ans;
return small_ans;
}
ll power1(ll a,ll b){
ll ans=1;
while(b>0){
if(b&1){
ans=(ans*a);
}
a=a*a;
b=b>>1;
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll t;
cin>>t;
while(t--){
ll n;
cin>>n;
ll cnt=0;
while(n>1){
cnt++;
n=n/2;
}
n=power1(2,cnt);
if(n==1){
cout<<"0"<<endl;
continue;
}
if(n==2){
cout<<"1"<<endl;
continue;
}
vector<vector<ll>> t(2,vector<ll>(2,0));
t[0][1]=1;
t[1][0]=1;
t[1][1]=1;
vector<ll> f(2,0);
f[1]=1;
vector<vector<ll>> T=power2(t,n-1);
ll res=0;
for(ll i=0;i<2;i++){
res+=(T[0][i]*f[i])%MOD;
res%=MOD;
}
cout<<res<<endl;
}
}