-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFIBNK.cpp
More file actions
49 lines (42 loc) · 1012 Bytes
/
Copy pathFIBNK.cpp
File metadata and controls
49 lines (42 loc) · 1012 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
#include <iostream>
using namespace std;
typedef long long int ll;
#define R 1000000007
void multiply(ll F[2][2], ll M[2][2]){
ll x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
ll y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
ll z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
ll w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x%R;
F[0][1] = y%R;
F[1][0] = z%R;
F[1][1] = w%R;
}
void power(ll F[2][2], int n){
if(n == 0 || n == 1) return;
ll M[2][2] = {{1, 1}, {1, 0}};
power(F, n / 2);
multiply(F, F);
if (n % 2 != 0) multiply(F, M);
}
ll fib(int n){
ll F[2][2] = {{1,1},{1,0}};
if (n == 0) return 0;
power(F, n-1);
return F[0][0];
}
ll sumfib(int n){
return (fib(n+1)-1)%R;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t, n, k;
cin>>t;
while(t--){
cin>>n>>k;
if(n<=k) cout<<sumfib(n)<<"\n";
else cout<<((((n/k)*sumfib(k))%R)+(sumfib(n%k)))%R<<"\n";
}
return 0;
}