XidianOJ 1145 数学题

--正文

  矩阵快速幂,难点在如何构造(注意Sum(r) = Sum(r-1) + T(r))

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MOD 1000000007
typedef long long LL;
typedef struct {
    LL M[4][4];
} Matrix;

Matrix Ans,Base;
long long answer[5] = {1,2,3,6,11};
long long l,r;

Matrix multi(Matrix a,Matrix b){
    Matrix Tmp;
    int i,j,k;
    for (i=0;i<4;i++){
        for (j=0;j<4;j++){
            Tmp.M[i][j] = 0;
            for (k=0;k<4;k++){
                Tmp.M[i][j] = (Tmp.M[i][j] + a.M[i][k] * b.M[k][j]) % MOD;
            }
        }
    }
    return Tmp;
}
LL Fast_Mod(LL n){
    Base.M[0][0] = 1; Base.M[0][1] = 1; Base.M[0][2] = 1; Base.M[0][3] = 0;
    Base.M[1][0] = 1; Base.M[1][1] = 0; Base.M[1][2] = 0; Base.M[1][3] = 0;
    Base.M[2][0] = 0; Base.M[2][1] = 1; Base.M[2][2] = 0; Base.M[2][3] = 0;
    Base.M[3][0] = 1; Base.M[3][1] = 0; Base.M[3][2] = 0; Base.M[3][3] = 1;
    Ans.M[0][0] = 1; Ans.M[0][1] = 0; Ans.M[0][2] = 0; Ans.M[0][3] = 0;
    Ans.M[1][0] = 0; Ans.M[1][1] = 1; Ans.M[1][2] = 0; Ans.M[1][3] = 0;
    Ans.M[2][0] = 0; Ans.M[2][1] = 0; Ans.M[2][2] = 1; Ans.M[2][3] = 0;
    Ans.M[3][0] = 0; Ans.M[3][1] = 0; Ans.M[3][2] = 0; Ans.M[3][3] = 1; 
    while (n){
        if (n&1)
            Ans = multi(Ans,Base);
        Base = multi(Base,Base);
        n = n >> 1;
    }
    return (Ans.M[3][0] + Ans.M[3][1] + Ans.M[3][2] + 2*Ans.M[3][3]) % MOD;
}


int main(){
    while (scanf("%lld %lld",&l,&r) != EOF){
        LL ansa,ansb;
        if (l == 0) {
            ansa = 0;
        }
        else if (l < 6) {
            ansa = answer[l-1];
        } else ansa = Fast_Mod(l-2);
        if (r < 5){
            ansb = answer[r];
        }
        else ansb = Fast_Mod(r-1);
        //printf("%lld %lld\n",ansa,ansb);
        printf("%lld\n",(ansb+MOD-ansa)%MOD);
    }
    return 0;
} 

 

转载于:https://www.cnblogs.com/ToTOrz/p/6139213.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值