Ref Link1
Ref Link2
#include <bits/stdc++.h>
using namespace std;
#define ULL unsigned long long
ULL mod = 1000000007;
void mul(ULL a[][2] , ULL b[][2])
{
ULL result[2][2];
memset(result , 0 , sizeof result);
for(int row = 0; row < 2; row++)
{
for(int col = 0; col < 2; col++)
{
for(int k = 0; k < 2; k++)
{
result[row][col] = (result[row][col] + (a[row][k] * b[k][col]) % mod) % mod;
}
}
}
for(int row = 0; row < 2; row++)
{
for(int col = 0; col < 2; col++)
{
a[row][col] = result[row][col];
}
}
}
ULL fastMatrixExpo(ULL n)
{
ULL fib[][2] = {{0, 1}, {1, 1}};
ULL temp[][2] = {{1, 0}, {0, 1}};
while(n)
{
if(n & 1)
{
mul(temp, fib);
}
mul(fib, fib);
n = n >> 1;
}
return temp[0][1];
}
int main()
{
int t;
ULL n, m;
cin >> t;
while(t--)
{
cin >> n >> m;
cout<<(fastMatrixExpo(m+2) - fastMatrixExpo(n + 1) + mod)%mod<<endl;
}
return 0;
}
Comments
Post a Comment