-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibosum.cpp
More file actions
65 lines (60 loc) · 1.05 KB
/
fibosum.cpp
File metadata and controls
65 lines (60 loc) · 1.05 KB
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
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define pr pair<int,int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
void multiply(long A[2][2],long M[2][2]) {
long m[2][2];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
m[i][j]=0;
for(int k=0;k<2;k++) {
m[i][j]=(m[i][j]+A[i][k]*M[k][j])%MOD;
}
}
}
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
A[i][j]=m[i][j];
}
}
}
void power(long A[2][2],long n) {
if(n==1)
return;
power(A,n/2);
multiply(A,A);
if(n%2!=0) {
long F[2][2]={{1,1},{1,0}};
multiply(A,F);
}
}
long fib(long n) {
if(n==0||n==1)
return n;
long A[2][2]={{1,1},{1,0}};
power(A,n-1);
return A[0][0];
}
long fiboSum(long n,long m) {
//Sum = S(m)-S(n-1)=F(m+2)-1-(F(n+1)-1)
long sum=((fib(m+2)-fib(n+1))%MOD+MOD)%MOD;
return sum;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--) {
long n,m;
cin>>n>>m;
long ans=fiboSum(n,m);
cout<<ans<<"\n";
}
return 0;
}