HDU 5950 Recursive sequence(矩阵)

来源:互联网 发布:微信团购源码 编辑:程序博客网 时间:2024/05/29 14:14







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
原创粉丝点击