zoukankan      html  css  js  c++  java
  • luogu_1939 【模板】矩阵加速(数列)

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    using namespace std;
    typedef long long LL;
    const LL mod=1e9+7;
    int T;
    LL n;
    
    struct Matrix{
    	LL h[5][5];
    }a;
    
    int main(){
    	ios::sync_with_stdio(false);
    	cin>>T;
    	while(T--){
    		Matrix ans,t;
    		cin>>n;
    		ans.h[1][1]=1; ans.h[1][2]=1; ans.h[1][3]=1;
    		t.h[1][1]=1; t.h[1][2]=1; t.h[1][3]=0;
    		t.h[2][1]=0; t.h[2][2]=0; t.h[2][3]=1;
    		t.h[3][1]=1; t.h[3][2]=0; t.h[3][3]=0;
    		for(n-=3;n>0;n>>=1){
    			if(n&1){
    				Matrix x;
    				for(int i=1;i<=3;i++){
    					LL sum=0;
    					for(int j=1;j<=3;j++)sum+=ans.h[1][j]*t.h[j][i],sum%=mod;
    					x.h[1][i]=sum;
    				}
    				for(int i=1;i<=3;i++)ans.h[1][i]=x.h[1][i];
    			}
    			Matrix d;
    			for(int i=1;i<=3;i++)
    				for(int j=1;j<=3;j++){
    					LL sum=0;
    					for(int k=1;k<=3;k++)sum+=t.h[i][k]*t.h[k][j],sum%=mod;
    					d.h[i][j]=sum;
    				}
    			for(int i=1;i<=3;i++)
    				for(int j=1;j<=3;j++)t.h[i][j]=d.h[i][j];
    		}
    		cout<<ans.h[1][1]<<endl;
    	}
    	return 0; 
    }
    

      

      //结构体函数版:

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    using namespace std;
    typedef long long LL;
    const LL mod=1e9+7;
    int T;
    LL n;
    
    struct Matrix{
    	LL h[5][5];
    	void clear(){
    		memset(h,0,sizeof(h));
    	}
    	Matrix operator * (const Matrix& t) const{
    		Matrix d; d.clear();
    		for(int i=1;i<=3;i++)
    			for(int j=1;j<=3;j++){
    				LL sum=0;
    				for(int k=1;k<=3;k++)sum+=h[i][k]*t.h[k][j],sum%=mod;
    				d.h[i][j]=sum;
    			}
    		return d;
    	}
    	Matrix operator ^ (LL k) const {
    		Matrix ans(*this),t(*this),x(*this);
    		t.clear();
    		t.h[1][1]=1; t.h[1][2]=1; t.h[1][3]=0;
    		t.h[2][1]=0; t.h[2][2]=0; t.h[2][3]=1;
    		t.h[3][1]=1; t.h[3][2]=0; t.h[3][3]=0;
    		for(k-=3;k>0;k>>=1){
    			if(k&1)
    				for(int i=1;i<=3;i++){
    					LL sum=0;
    					for(int j=1;j<=3;j++)sum+=ans.h[1][j]*t.h[j][i],sum%=mod;
    					x.h[1][i]=sum;
    				}
    			for(int i=1;i<=3;i++)ans.h[1][i]=x.h[1][i];
    			t=t*t;
    		}
    		return ans;
    	}
    }a;
    
    int main(){
    	ios::sync_with_stdio(false);
    	cin>>T;
    	while(T--){
    		Matrix ans,t;
    		cin>>n;
    		ans.h[1][1]=1; ans.h[1][2]=1; ans.h[1][3]=1;
    		t.h[1][1]=1; t.h[1][2]=1; t.h[1][3]=0;
    		t.h[2][1]=0; t.h[2][2]=0; t.h[2][3]=1;
    		t.h[3][1]=1; t.h[3][2]=0; t.h[3][3]=0;
    		ans=ans^n;
    		cout<<ans.h[1][1]<<endl;
    	}
    	return 0; 
    }
    

      

  • 相关阅读:
    codeforces 439C 模拟
    codeforces 435B
    【WebVR】AFrame中的A-sky无法利用src指定路径显示全景图
    【UE4】添加头文件之后VS中UCLASS()报错问题解决办法
    【UE4】蓝图之间的通讯
    git中报unable to auto-detect email address 错误的解决办法
    2017ACM省赛总结与生涯回顾
    hihocoder#1121 : 二分图一•二分图判定
    hihocoder#1039 : 字符消除
    2048low版
  • 原文地址:https://www.cnblogs.com/codetogether/p/7577191.html
Copyright © 2011-2022 走看看