-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIBOSUM.cpp
67 lines (58 loc) · 1.64 KB
/
FIBOSUM.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
#include<bits/stdc++.h>
using namespace std;
long long mod = pow(10, 9) + 7;
void multiply(vector<vector<int>> &arr) {
vector<vector<int>> arr1(2, vector<int>(2, 1));
arr1[0][0] = ((arr[0][0] % mod * arr[0][0] % mod) % mod + (arr[0][1] % mod * arr[1][0] % mod) % mod) % mod;
arr1[0][1] = ((arr[0][0] % mod * arr[0][1] % mod) % mod + (arr[0][1] % mod * arr[1][1] % mod) % mod) % mod;
arr1[1][0] = ((arr[1][0] % mod * arr[0][0] % mod) % mod + (arr[1][1] % mod * arr[1][0] % mod) % mod) % mod;
arr1[1][1] = ((arr[1][0] % mod * arr[0][1] % mod) % mod + (arr[1][1] % mod * arr[1][1] % mod) % mod) % mod;
arr[0][0] = arr1[0][0];
arr[0][1] = arr1[0][1];
arr[1][0] = arr1[1][0];
arr[1][1] = arr1[1][1];
}
void fib(int m, vector<vector<int>> &arr) {
if (m == 0 || m == 1) {
return;
}
fib(m / 2, arr);
multiply(arr);
if (m % 2 == 0) {
return;
}
vector<vector<int>> arr1(2, vector<int>(2, 1));
arr1[0][0] = (arr[0][0] * 1 + arr[0][1] * 1) % mod;
arr1[0][1] = arr[0][0];
arr1[1][0] = arr[1][0] + arr[1][1];
arr1[1][1] = arr[1][0];
arr[0][0] = arr1[0][0];
arr[0][1] = arr1[0][1];
arr[1][0] = arr1[1][0];
arr[1][1] = arr1[1][1];
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
vector<vector<int>> arr(2, vector<int>(2, 1));
arr[1][1] = 0;
fib(m + 1, arr);
long long ans1 = arr[0][0] % mod;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
arr[i][j] = 1;
}
}
arr[1][1] = 0;
fib(n , arr);
long long ans2 = arr[0][0] % mod;
long long ans = (ans1 % mod - ans2 % mod + mod) % mod;
cout << ans << endl;
}
return 0;
}