Problem - 3936 FIB Query 斐波那契 ʕ •ᴥ•ʔ

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

题解: 经过推导 只需要求单个斐波那契值即可,因为n比较大,用矩阵快速幂

扫描二维码关注公众号,回复: 2826701 查看本文章

f[m+n-1]=f[m-1]*f[n-1]+f[m]*f[n];

f(4i-1)=f(2i+2i-1)=f(2i-1)*f(2i-1)+f(2i)*f(2i);

p[i]=f[4*i-1];

p[i]=f[2*i-1]^2+f[2*i]^2;

p[1]=f[1]^2+f[2]^2;

p[2]=f[3]^2+f[4]^2;

f[1]^2+f[2]^2+f[3]^2+...+f[n]^2=f[n]*f[n+1];

sum(p[n])=f[1]^2+f[2]^2+f[3]^2+f[4]^2+...+f[2*n-1]^2+f[2*n]^2;

sum(p[n])=f[2*n]*f[2*n+1];

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define ll long long
ll mod = 1e9 +7;
ll x[2][2],t[2][2];
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[j][0]+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;
	
}
void power(ll num)
{
	
	x[0][0]=1;
	x[0][1]=0;
	t[0][0]=1;
	t[0][1]=1;
	t[1][0]=1;
	t[1][1]=0;
	while(num)
	{
		if(num&1)
		mul(x,t);
		num>>=1;
		mul(t,t);
	}
	
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		
		ll l,r;
		cin>>l>>r;
		l--;
		ll sumr,suml;
		power(2*r);
		sumr=x[0][0]*x[0][1]%mod;
		power(2*l);
		suml=x[0][0]*x[0][1]%mod;
		ll ans=sumr-suml;
		cout<<(ans+mod)%mod<<endl;
		
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/henucm/article/details/81701932
fib