Codeforces718 C. Sasha and Array(线段树维护矩阵,矩阵快速幂求斐波那契数列,矩阵乘法结合律)

Posted live4m

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces718 C. Sasha and Array(线段树维护矩阵,矩阵快速幂求斐波那契数列,矩阵乘法结合律)相关的知识,希望对你有一定的参考价值。

题意:

解法:

斐波那契数列可以用矩阵快速幂求,线段树维护矩阵即可,
而矩阵满足乘法结合律,线段树每个节点维护的区间和,
当有laz的时候可以直接将区间和矩阵乘上laz矩阵.

复杂度O(n*log*(2^3))

code:

#include<bits/stdc++.h>
// #define SYNC_OFF
#define int long long
#define ll long long
#define ull unsigned long long
//fast-coding
#define ST(x) x.begin()
#define ED(x) x.end()
#define RST(x) x.rbegin()
#define RED(x) x.end()
#define CL(x) x.clear();
#define all(a,n) a+1,a+1+n
#define ff(i,n) for(ll i=1;i<=n;i++)
#define rff(i,n) for(ll i=n;i>=1;i--)
#define fff(i,n) for(ll i=0;i<n;i++)
#define rfff(i,n) for(ll i=n-1;i>=0;i--)
#define SC(x) scanf("%s",x)
#define SL(x) strlen(x)
#define pss(a) push_back(a)
#define ps(a) push(a)
#define SZ(x) (int)x.size()
#define pee puts("");
#define eee putchar(' ');
#define re readdd()
#define pr(a) printtt(a)
int readdd(){int x=0,f=1;char c=getchar();//
while(!isdigit(c)&&c!='-')c=getchar();
if(c=='-')f=-1,c=getchar();
while(isdigit(c))x=x*10+c-'0',c=getchar();
return f*x;}
void printtt(int x){if(x<0)putchar('-'),x=-x;//
if(x>=10)printtt(x/10);putchar(x%10+'0');}
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}//
int ppow(int a,int b,int mod){a%=mod;//
int ans=1%mod;while(b){if(b&1)ans=(long long)ans*a%mod;
a=(long long)a*a%mod;b>>=1;}return ans;}
bool addd(int a,int b){return a>b;}
int lowbit(int x){return x&-x;}
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
bool isdigit(char c){return c>='0'&&c<='9';}
bool Isprime(int x){
    for(int i=2;i*i<=x;i++)if(x%i==0)return 0;
    return 1;
}
void ac(int x){if(x)puts("YES");else puts("NO");}
//short_type 
#define VE vector<int>
#define PI pair<int,int>
//
using namespace std;
// const int mod=998244353;
const int mod=1e9+7;
const int maxm=1e5+5;
const int inv2=ppow(2,mod-2,mod);
//
struct Mat{
    int a[2][2];
    void init(){
        memset(a,0,sizeof a);
    }
    void init_e(){
        init();a[0][0]=a[1][1]=1;
    }
    bool is_e(){//判断是否是单位阵
        if(a[0][0]!=1||a[1][1]!=1)return 0;
        if(a[0][2]!=0||a[1][0]!=0)return 0;
        return 1;
    }
    Mat operator+(const Mat& b)const{
        Mat ans;
        ans.init();
        fff(i,2){
            fff(j,2){
                ans.a[i][j]=(a[i][j]+b.a[i][j])%mod;
            }
        }
        return ans;
    }
    Mat operator*(const Mat& b)const{
        Mat ans;
        ans.init();
        fff(k,2){
            fff(i,2){
                fff(j,2){
                    ans.a[i][j]=(ans.a[i][j]+a[i][k]*b.a[k][j])%mod;
                }
            }
        }
        return ans;
    }
};
Mat M_pow(Mat a,int b,int mod){
    Mat ans;
    ans.init_e();
    while(b){
        if(b&1)ans=ans*a;
        a=a*a;
        b>>=1;
    }
    return ans;
}
const Mat base={
    1,1,
    1,0,
};
struct Tree{
    Mat a[maxm<<2];
    Mat laz[maxm<<2];
    void pd(int k){
        if(laz[k].is_e())return ;
        a[k*2]=a[k*2]*laz[k];
        a[k*2+1]=a[k*2+1]*laz[k];
        laz[k*2]=laz[k*2]*laz[k];
        laz[k*2+1]=laz[k*2+1]*laz[k];
        laz[k].init_e();
    }
    void pp(int k){
        a[k]=(a[k*2]+a[k*2+1]);
    }
    void build(int l,int r,int k){
        laz[k].init_e();
        if(l==r){
            int x=re;
            a[k]=M_pow(base,x,mod);
            return ;
        }
        int mid=(l+r)/2;
        build(l,mid,k*2);
        build(mid+1,r,k*2+1);
        pp(k);
    }
    void upd(int st,int ed,Mat val,int l,int r,int k){
        if(st<=l&&ed>=r){
            laz[k]=laz[k]*val;
            a[k]=a[k]*val;
            return ;
        }
        pd(k);
        int mid=(l+r)/2;
        if(st<=mid)upd(st,ed,val,l,mid,k*2);
        if(ed>mid)upd(st,ed,val,mid+1,r,k*2+1);
        pp(k);
    }
    Mat ask(int st,int ed,int l,int r,int k){
        if(st<=l&&ed>=r){
            return a[k];
        }
        pd(k);
        int mid=(l+r)/2;
        Mat ans;
        ans.init();
        if(st<=mid)ans=ans+ask(st,ed,l,mid,k*2);
        if(ed>mid)ans=ans+ask(st,ed,mid+1,r,k*2+1);
        return ans;
    }
}T;
int n,m;
void solve(){
    n=re,m=re;
    T.build(1,n,1);
    ff(mm,m){
        int op=re,l=re,r=re;
        if(op==1){
            int x=re;
            Mat val=M_pow(base,x,mod);
            T.upd(l,r,val,1,n,1);
        }else if(op==2){
            Mat ans=T.ask(l,r,1,n,1);
            pr(ans.a[0][1]);pee;
        }
    }
}
void Main(){
    // #define MULTI_CASE
    #ifdef MULTI_CASE
    int T;cin>>T;while(T--)
    #endif
    solve();
}
void Init(){
    #ifdef SYNC_OFF
    ios::sync_with_stdio(0);cin.tie(0);
    #endif
    #ifndef ONLINE_JUDGE
    freopen("../in.txt","r",stdin);
    freopen("../out.txt","w",stdout);
    #endif
}
signed main(){
    Init();
    Main();
    return 0;
}

以上是关于Codeforces718 C. Sasha and Array(线段树维护矩阵,矩阵快速幂求斐波那契数列,矩阵乘法结合律)的主要内容,如果未能解决你的问题,请参考以下文章

codeforces CF718C Sasha and Array 线段树维护矩阵

Codeforces 718C. Sasha and Array(线段树)

codeforces 718 C&DC. Sasha and Array&D. Andrew and Chemistry

718C Sasha and Array

题解 CF718C Sasha and Array

题解 CF718C Sasha and Array