nyoj 301 递推求值


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn=100+5;
const int mod=1000007;
const int N=3;
struct Mat
{
    LL mat[N][N];
};
//矩阵相乘 
Mat mul(Mat a,Mat b)
{
    Mat ans;
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
        {
            ans.mat[i][j]=0;
            for(int k=0; k<N; k++)
            {
                ans.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                ans.mat[i][j]%=mod;
            }
        }
    }
    return ans;
}
//矩阵的n次方 
Mat quickPow(Mat a,int n)
{
    Mat ans=//单位矩阵 
    {
        1,0,0,
        0,1,0,
        0,0,1
    };
    while(n)
    {
        if(n&1)
            ans=mul(ans,a);
        n>>=1;
        a=mul(a,a);
    }
    return ans;
}
int main()
{	
	int t,a,b,c,n;
	scanf("%d",&t);
	Mat tem;
	Mat e=
    {
        0,0,0,
        1,0,0,
        0,0,1
    };
	int f[2];
	
	tem.mat[2][0]=1;
	while(t--){
		
		scanf("%d%d%d%d%d%d",&f[0],&f[1],&a,&b,&c,&n);
		if(n==1||n==2){
			printf("%d\n",(f[n-1]+mod)%mod);
		}
		else{
		e.mat[0][0]=b;
		e.mat[0][1]=a;
		e.mat[0][2]=c;
		
		tem.mat[0][0]=f[1];
		tem.mat[1][0]=f[0];
		Mat ans = mul(quickPow(e,n-2),tem);
		printf("%lld\n",(ans.mat[0][0]+mod)%mod);
		}
	} 
    	
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值