Problem Description
We all know the definition of Fibonacci series: fib[i]=fib[i-1]+fib[i-2],fib[1]=1,fib[2]=1.And we define another series P associated with the Fibonacci series: P[i]=fib[4*i-1].Now we will give several queries about P:give two integers L,R, and calculate ∑P[i](L <= i <= R).
Input
There is only one test case.The first line contains single integer Q – the number of queries. (Q<=10^4)Each line next will contain two integer L, R. (1<=L<=R<=10^12)
Output
For each query output one line.Due to the final answer would be so large, please output the answer mod 1000000007.
Sample Input
2
1 300
2 400
Sample Output
838985007
352105429
AC
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <map>
#include <bitset>
#include <set>
#include <string.h>
#include <cmath>
#include <queue>
#include <algorithm>
#define N 100005
#define P pair<int,int>
#define ll long long
#define lowbit(a) a&(-a)
#define mk(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
ll mod = 1e9 +7;
using namespace std;
// 矩阵相乘
void mul(ll a[][2], ll b[][2]) {
ll ans[2][2];
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
ans[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j];
}
}
a[0][0] = ans[0][0] % mod;
a[0][1] = ans[0][1] % mod;
a[1][0] = ans[1][0] % mod;
a[1][1] = ans[1][1] % mod;
}
ll x[2][2], t[2][2];
// 矩阵快速幂
void quick(ll num) {
x[0][0] = 0;
x[0][1] = 1;
t[0][0] = 0;
t[0][1] = 1;
t[1][0] = 1;
t[1][1] = 1;
while (num) {
if (num & 1) {
mul(x, t);
}
num >>= 1;
mul(t, t);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) {
ll l, r;
scanf("%lld%lld", &l, &r);
l--;
ll sumr, suml;
quick(2 * r);
sumr = x[0][0] * x[0][1] % mod;
quick(2 * l);
suml = x[0][0] * x[0][1] % mod;
ll ans = sumr - suml;
printf("%lld\n", (ans + mod) % mod);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return 0;
}