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

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;
}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值