#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
void mul( ll a[2][2] , ll b[2][2])
{
ll c[2][2];
for(int i = 0 ; i < 2 ; ++i ){
for(int j = 0 ; j < 2 ; ++j ){
c[i][j] = 0;
for(int k = 0 ; k < 2 ; ++k )
c[i][j] = (c[i][j] + (a[i][k]*b[k][j])%mod)%mod;
}
}
for(int i = 0 ; i < 2 ; ++i ){
for(int j = 0 ; j < 2; ++j )
a[i][j] = c[i][j];
}
}
ll fib( ll n)
{
if(n==0)
return 0;
if(n == 1)
return 1;
ll res[2][2]={{1,0},{0,1}} , mat[2][2] = {{1,1} ,{1,0}};
while(n)
{
if(n%2!=0)
mul(res,mat);
n = n >> 1;
mul(mat,mat);
}
return res[0][1];
}
int main()
{
int t;
cin >> t;
while(t--)
{
ll n , m;
cin >> n >> m;
ll ans = fib(m+2) - 1;
ans = ans - (fib(n+1)-1);
if(ans<0)
ans+=mod;
ans = ans%mod;
cout << ans << endl;
}
return 0;
}
#define ll long long
#define mod 1000000007
using namespace std;
void mul( ll a[2][2] , ll b[2][2])
{
ll c[2][2];
for(int i = 0 ; i < 2 ; ++i ){
for(int j = 0 ; j < 2 ; ++j ){
c[i][j] = 0;
for(int k = 0 ; k < 2 ; ++k )
c[i][j] = (c[i][j] + (a[i][k]*b[k][j])%mod)%mod;
}
}
for(int i = 0 ; i < 2 ; ++i ){
for(int j = 0 ; j < 2; ++j )
a[i][j] = c[i][j];
}
}
ll fib( ll n)
{
if(n==0)
return 0;
if(n == 1)
return 1;
ll res[2][2]={{1,0},{0,1}} , mat[2][2] = {{1,1} ,{1,0}};
while(n)
{
if(n%2!=0)
mul(res,mat);
n = n >> 1;
mul(mat,mat);
}
return res[0][1];
}
int main()
{
int t;
cin >> t;
while(t--)
{
ll n , m;
cin >> n >> m;
ll ans = fib(m+2) - 1;
ans = ans - (fib(n+1)-1);
if(ans<0)
ans+=mod;
ans = ans%mod;
cout << ans << endl;
}
return 0;
}
No comments:
Post a Comment