HDU 5950 Recursive sequence(矩阵)







http://acm.split.hdu.edu.cn/showproblem.php?pid=5950









题目大意:


F(n) = F(n-1) + 2*F(n-2) + n^4






分析:

矩阵









AC代码:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
typedef long long LL;
const LL mod=2147493647;
using namespace std;
int n=7;
struct Matrix{
    LL a[7][7];
    void init(){
        memset(a,0,sizeof(a));
        for (int i=0;i<n;i++)
            a[i][i]=1;
    }
};
Matrix multiply(Matrix x,Matrix y){
    Matrix temp;
    memset(temp.a,0,sizeof(temp.a));
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            for (int k=0;k<n;k++){
                temp.a[i][j]=(temp.a[i][j]+x.a[i][k]*y.a[k][j]%mod+mod)%mod;
            }
        }
    }
    return temp;
}
Matrix qpow(Matrix M,LL k){
    Matrix temp;
    temp.init();
    while (k){
        if (k%2)
            temp=multiply(temp,M);
        k/=2;
        M=multiply(M,M);
    }
    return temp;
}
LL Power(LL a,LL b){
    LL temp=1;
    while (b){
        if(b%2)
            temp=(temp*a)%mod;
        b/=2;
        a=(a*a)%mod;
    }
    return temp%mod;
}
int main (){
    LL N,a,b,t;
    scanf("%lld",&t);
	while (t--){
		scanf ("%lld%lld%lld",&N,&a,&b);
		if (N==1){
			printf ("%lld\n",a%mod);
			continue;
		}
		else if (N==2){
			printf ("%lld\n",b%mod);
			continue;
		}
		Matrix M;
		memset(M.a,0,sizeof(M.a));
		M.a[0][0]=M.a[0][2]=M.a[0][6]=M.a[1][0]=M.a[2][2]=M.a[2][6]=M.a[3][3]=M.a[3][6]=M.a[4][4]=M.a[4][6]=M.a[5][5]=M.a[5][6]=M.a[6][6]=1;
		M.a[0][1]=M.a[4][5]=2;
		M.a[3][4]=M.a[3][5]=3;
		M.a[0][3]=M.a[0][5]=M.a[2][3]=M.a[2][5]=4;
		M.a[0][4]=M.a[2][4]=6;
		Matrix temp=qpow(M,N-2);
		LL ans=(b*temp.a[0][0]%mod+a*temp.a[0][1]%mod+16*temp.a[0][2]%mod+8*temp.a[0][3]%mod+4*temp.a[0][4]%mod+2*temp.a[0][5]%mod+temp.a[0][6]%mod)%mod;
		printf ("%lld\n",ans);
	} 
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值