【维护斐波那契和 区间修改】

该博客探讨如何处理一个基于斐波那契数列的问题,其中需要对数列进行区间加操作并求解特定项的值。文章介绍了一种利用矩阵快速幂来维护斐波那契数列和的方法,并结合动态开点线段树维护差分以解决区间修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目意思 是给你一个数列为斐波那契数列的和
例如 S[1] = f[1] = 1 S[2] = f[1] + f[2] = 2
然后支持区间加操作
问你这个数列第k项是多少
首先我们要知道这个斐波那契数列的公式
[Fn+2Fn+1]  =  [1110]∗[Fn+1Fn]  [Fn+2Fn+1Fn+1Fn]  =  [1110]n+1 \left[ \begin{array}{c} F_{n+2}\\ F_{n+1}\\ \end{array} \right] \,\,=\,\,\left[ \begin{matrix} 1& 1\\ 1& 0\\ \end{matrix} \right] *\left[ \begin{array}{c} F_{n+1}\\ F_n\\ \end{array} \right] \,\, \\ \left[ \begin{matrix} F_{n+2}& F_{n+1}\\ F_{n+1}& F_n\\ \end{matrix} \right] \,\,=\,\,\left[ \begin{matrix} 1& 1\\ 1& 0\\ \end{matrix} \right] ^{n+1} [Fn+2Fn+1]=[1110][Fn+1Fn][Fn+2Fn+1Fn+1Fn]=[1110]n+1
知道了这个呢还要知道斐波那契数列的和是
斐波那契数列和:Sn  =  2an  +  an−1  −  1 =  an+1  +  an  −  1 斐波那契数列和:S_n\,\,=\,\,2a_n\,\,+\,\,a_{n-1}\,\,-\,\,\text{1 }=\,\,a_{n+1}\,\,+\,\,a_n\,\,-\,\,1 \\ :Sn=2an+an1=an+1+an1

证明就是第一个取一个1 减一个1
然后这样我们就能用矩阵快速幂维护和 区间加用动态开点线段树维护差分

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define int long long
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)
#define MOD 1000000007
#define M 1000000001

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
int n,tot = 1;
const int MAX_N = 1000025;
struct node{int l,r;int val;node(){l=r=0;val = 0ll;}}T[MAX_N<<2];
struct Matrix{int a[3][3];};
inline Matrix operator*(Matrix a,Matrix b){
	Matrix c;
	memset(c.a,0,sizeof(c.a));
	for(int i=1;i<=2;++i)for(int k=1;k<=2;++k)for(int j=1;j<=2;++j)c.a[i][j]+=a.a[i][k]*b.a[k][j]%mod;
	for(int i=1;i<=2;++i)for(int j=1;j<=2;++j)c.a[i][j]%=mod;
	return c;
}
inline Matrix operator^(Matrix a,int p){
	Matrix ret;
	ret.a[1][1]=ret.a[2][2]=1,ret.a[1][2]=ret.a[2][1]=0;
	while(p){
		if(p&1)ret=ret*a;
		a=a*a,p>>=1;
	}
	return ret;
}
inline void up(int rt){(T[rt].val = T[T[rt].l].val + T[T[rt].r].val)%=MOD;return ;}
void update(int rt,int l,int r,int x,int v)
{
    if(l==r)
    {
        (T[rt].val += v)%=MOD;
        return ;
    }
    if(x<=mid)
    {
        if(!T[rt].l) T[rt].l = ++tot;
        update(T[rt].l,l,mid,x,v);
    }
    else
    {
        if(!T[rt].r) T[rt].r = ++tot;
        update(T[rt].r,mid+1,r,x,v);
    }
    up(rt);
}
int query(int rt,int l,int r,int x,int y)
{
    if(!rt) return 0;
    if(x<=l&&r<=y)
    {
        return T[rt].val;
    }
    if(x>mid) return query(T[rt].r,mid+1,r,x,y);
    if(y<=mid) return query(T[rt].l,l,mid,x,y);
    return (query(T[rt].l,l,mid,x,mid) + query(T[rt].r,mid+1,r,mid+1,y))%MOD;
}
signed main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    scanf("%d",&n);
    Matrix tmp1,tmp2;
    tmp1.a[1][1] = 1,tmp1.a[1][2] = 1,tmp1.a[2][1] = 1,tmp1.a[2][2] = 0;
    tmp2.a[1][1] = 1,tmp2.a[1][2] = 0,tmp2.a[2][1] = 1,tmp2.a[2][2] = 0;
    while(n--)
    {
        int opt,x,y,v;
        scanf("%lld",&opt);
        if(opt==1)
        {
            scanf("%lld%lld%lld",&x,&y,&v);
            update(1,1,M,x,v),update(1,1,M,y+1,-v);
        }
        else
        {
            scanf("%d",&x);
            int ans = query(1,1,M,1,x);
            Matrix tmp = (tmp1^x)*tmp2;
            ans = (ans+tmp.a[1][1] - 1+MOD) %MOD;
            printf("%lld\n",ans);
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值