转载于:https://blog.csdn.net/henuyh/article/details/81517227
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
- 经过一下推导,只用求单个斐波那契值即可,因为n比较大,用矩阵快速幂
#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;
}
- 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
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77