Problem - 3936 FIB Query(斐波那契恒等式 矩阵乘法 快速幂运算乘法)

转载于: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
斐波那契数列的矩阵快速幂实现方式如下所示: 首先,我们定义一个2阶矩阵: \[ M = \begin{bmatrix} 1\enspace 1\\ 1\enspace 0 \end{bmatrix} \] 然后,根据斐波那契数列的递推公式\[ F_n = F_{n-1} + F_{n-2} \],我们可以将这个公式抽象成矩阵运算的形式: \[ \begin{bmatrix} F_{n} \\ F_{n-1} \end{bmatrix} = M^{n-1} \cdot \begin{bmatrix} F_1 \\ F_0 \end{bmatrix} \] 其中,\[ F_1 \]和\[ F_0 \]分别表示斐波那契数列的初始值。 通过快速幂运算的思想,我们可以将指数为正整数的幂运算复杂度从\[ O(n) \]降低到\[ O(\log_2 n) \],而且矩阵乘法运算也符合结合律,因此可以使用矩阵快速幂来计算斐波那契数列。 代码实现如下: ```python import numpy as np def fibonacci_matrix_power(n, f1, f0): M = np.array([[1, 1], [1, 0]]) # 定义2阶矩阵M result = np.array([[f1], [f0]]) # 初始值矩阵 power = np.eye(2, dtype=int) # 幂运算的初始值为单位矩阵 while n > 0: if n % 2 == 1: power = np.dot(power, M) # 如果n是奇数,将M乘到结果矩阵上 M = np.dot(M, M) # 将M自乘,n每次右移一位 n //= 2 result = np.dot(power, result) # 最终的结果矩阵 return result # 返回斐波那契数列的第n项 # 测试示例 n = 10 f1 = 1 f0 = 0 fib_n = fibonacci_matrix_power(n, f1, f0) print(fib_n) ``` 这段代码通过矩阵快速幂的方式计算斐波那契数列的第n项,其中参数n为要计算的项数,f1和f0分别为斐波那契数列的初始值。运行结果为斐波那契数列的第n项的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值